Random Number Generation with PIC

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Random Number Generation with PIC

Postby ABSF » Tue Feb 01, 2011 8:46 pm

ober WROTE:Just my sudden thought, kind of agree with generating the number from key press.

Though is not really random, but if the microcontroller is running at 20MHz, and each instruction is executed at 200ns. If changing the number at 7-segment take a few instructions, it will take around 1us to 2us. Basically no one cannot see and also predict the number that will appear on 7seg when it release the keypress.

Therefore it become random :)


Myke Predko has a smarter way to generate random number. Page 128 of my "Evil Genius" book has an example on how to generate random number using TMR0. The result is an integer between 0 and 255:

OPTION = 0b01001111; // Run TMR0 From PIC MCU Clock
;
;
; when random number is needed:
;
i = TMR0; // Save TMR0 Value
if (i > 100) // Convert TMR0 Value to Decimal
LCDWrite((i / 100) + '0', 1);
else // No Hundreds to Display
LCDWrite(' ', 1);
if (i > 10)
{
i = i % 100; // Not Greater than 10
LCDWrite((i / 10) + '0', 1);
}
else
LCDWrite(' ', 1);
LCDWrite((i % 10) + '0', 1);

As the program is copyrighted. I can only extract a portion out of the whole program.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Random Number Generation with PIC

Postby ABSF » Tue Feb 01, 2011 9:04 pm

Brian Griffin WROTE:
The random number part from the "Flickering Candle" experiment in his book is also very useful. He is using the Linear Shift Feedback Register (LFSR). The idea isn't all that new, but it's a pretty neat trick. :D


Yes I just found it in Exp 50 - The Pumpkin LED Display. But I dont quite understand how LFSR works.

int fourBitLFSR, sixBitLFSR;
int fourBitCount, sixBitCount;

fourBitLFSR = ((fourBitLFSR << 1) & 0x0F) +
((fourBitLFSR >> 3) ^
((fourBitLFSR >> 2) & 1));
fourBitCount = fourBitCount+ 1; // Keep Track of the Number of States

sixBitLFSR = ((sixBitLFSR << 1) & 0x3F) +
((sixBitLFSR >> 5) ^
((sixBitLFSR >> 4) & 1));
sixBitCount = sixBitCount+ 1; // Keep Track of the Number of States

PORTC = sixBitLFSR | fourBitLFSR; //Maximum number of LED on

Does "^" mean "to the power of" or "exponent of" ?
What does "|" mean ? Logical OR or AND?

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Random Number Generation with PIC

Postby Brian Griffin » Tue Feb 01, 2011 10:01 pm

Hello,

The LFSR is explained in a picture form. Since I do not have a picture, you need to imagine it:

Take an example, 8-bit number. Fill it with a number, any number: 0-255. Picture a strip with 8 boxes inside. Each bit correspond to the each box inside.

Then, take a bit, and another bit, example: the 5th and 6th bit, and XOR it. The result of XOR-ing the 5th and the 6th bit is then "fed" (hence the "feedback") back to the very first bit of the number, which is the bit 0th.

Afterwards, shift the 8-bit number to the right. The 5th and the 6th bit is XOR-ed and then bit 0th is overwritten by the result.

Repeat the process. Every shift gives a different number. The number is pseudo-random, which means the same number will appear again after a sequence, but it's not noticable if you don't really check on it.

A more different apporach is extending the number of gates, or chain-link it with more gates in between.

The ^ means bitwise XOR, and the | means bitwise OR.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: Random Number Generation with PIC

Postby pic16 » Fri Feb 04, 2011 9:34 pm

i hope someday, i could understand what you guys talking about PIC here...
i'm poor in english and just an electronic hobbyist.
pic16
Amateur
 
Posts: 135
Joined: Tue Jan 04, 2011 2:28 pm
Location: Earth


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 1 guest

cron