Continue to Site

How to generate random number in PIC?

Status
Not open for further replies.

jcding1980

Member level 1
Member level 1
Joined
Jan 9, 2005
Messages
36
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
Singapore
Activity points
393
Hi.

Anyone knows how to generate random number in PIC? I am thinking of using timer to increase a counter and from the counter value, I do few operations to scramble up the value in counter. Does it cause the number to be random?

Or someone got better ways or ideas to do it?

Thanks.
 

You can use some polinom to generate RND number (always change starting value => something like CRC).

Mr.Cube
 

Look here for a simple example:
**broken link removed**

best regards
 

Interestig approach of generating random numbers using external "white noise" generator is presented here:

**broken link removed**
 

Do you need real random number or just pseudo-random?

For second case you can use LFSR to generate long sequence of looks-like-random number. Good idea is to get longer LFSR than required range of random numbers. For example, get 32-bit LFSR, shift it every time for new number for 32 bits and get low 16-bit as a random.

For first case you will need external hardware to be connected to your uC. Check this age for details:
h**p://wezm.net/pic/proj_rand.html
 

I designed a lottery number generator. I set the TIMER1 to run freely and when I push a button I read the value in the registers (from 0 to 65535) and I divide this by a factor to obtain the final number within the desired range, and show it on displays. Since the counter never stops, the user that pushes the button don´t know or don´t have control over the counting, even because the counting is very fast.
 

Hi.

Actually generating pseudorandom number is sufficient in my code. But the problem is how to test whether the number generated is considered random enough?

Added after 3 minutes:

Hi rkodaira.

The method you used to generate random number is quite similar to what I did currently. But is it really random enough?

So far from my code I think it is quite random. But I only sampled for few tens of number generated. Please comment.

Thanks.
 

jcding1980 said:
Actually generating pseudorandom number is sufficient in my code. But the problem is how to test whether the number generated is considered random enough?

There is one very nice set of randomness tests. It is called DieHard. You can freely download it for different platforms here: **broken link removed**
 

IanP said:
Interestig approach of generating random numbers using external "white noise" generator is presented here:

h**p://willware.net:8080/hw-rng.html

Like IanP said, white noise is the ONLY reliable source for random values. Everything else is not really random.
 

traxonja said:
Like IanP said, white noise is the ONLY reliable source for random values. Everything else is not really random.

This is like: hey, guys! 2+2=4! White noise is random by its nature. Moreover, ANY true random source produces white noise! The main problem is where to get this source of TRUE white noise. And how to evaluate that this white noise is really "white" (truly random).

Anyway, looks like pseudo-random generator with good randomness is OK for jcding1980's project. For this purpose I would suggest to join several LFSRs to Stop-and-Go scheme or to use Gollman scheme. They are used in stream ciphers, so their randomness is pretty good.
 

jcding1980 asked:

>Hi rkodaira.

>The method you used to generate random number is quite similar to what I did >currently. But is it really random enough?

>So far from my code I think it is quite random. But I only sampled for few tens of >number generated. Please comment.

Hi jc !

If you consider that the generation is "controlled" by the user, it is not pure random. But I think that no human bean can push the button to obtain a desired number ou perceive a mechanism or timing do to so, because the counting is so fast.
Besides that, when you divide the counting by a number I must recognize that some numbers may have different probabilities to occur, but at a minimum level of difference.
I used this method because it is simple and does not need any additional hardware nor very complex algorithms in the program.
I have tested my generator and it is able to generate all numbers from 0 to 99 (when a repetition occurs, it starts again, I put flags in eeprom to mark those numbers already generated).
 

what do you think to consider logistic maps? that are cahotic function? perhaps you could consider difference between two different(pseudorandom) maps.

it's only an idea I don't know if it gives really random numbers.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top