Generation of random numbers

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Generation of random numbers

2,502 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kacso on Fri Sep 14 10:53:38 MST 2012
How can I generate random numbers which will be different after every restart of MCU? Or how can I get new seed after every restart?

I now only for: srand(unsigned)time(NULL));
but it don't work on LPC.
0 项奖励
回复
3 回复数

2,272 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kacso on Sat Sep 15 04:37:29 MST 2012
Thanks, I will try with systick timer or get a seed from postion which I get from accelerometer.
0 项奖励
回复

2,272 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Fri Sep 14 16:28:32 MST 2012
AFAIK there is no way to get a genuine random number from a uC unless of course it has TRNG hardware.

One trick is to read an unconnected analog input and accumulate bit one into a word of the size you require. You should also apply "whitening" to the bits as you get them to even out the clumping you tend to get.

This works pretty well, if you graph the results it's still not truly random but pretty good.

NOTE: Probably depends on the actual ADC hardware, I've not tried this on an LPC.

If your project has any human interface you can use that, say grab the systick timer when a button is pressed etc.
0 项奖励
回复

2,272 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by MikeSimmonds on Fri Sep 14 16:26:32 MST 2012
[SIZE=2][FONT=Tahoma]One thought that springs to mind is to save the last random number generated to one of the RTC general purpose battery backed up registers; assuming that your design includes a battery for the RTC.

That way, at each reboot your pseudo-random generator will carry on from where it left off. I.e. the random number sequence will 'start' with a new number at every reset.

If you don't have a battery, then you are stuffed and will have to base the seed on something that [I]is[/I] inherantly random from off-chip.

If your application includes any user intervention (or comms etc.), you could get the delay in the finest granularity you can manage from initial setup to the first external input and derive your seed therefrom. (The external input is the off-chip random kick-start.)

You will have to assess how 'random' the external event actually is and decide if it has an acceptable variation.

Regards, Mike [/FONT][/SIZE]
0 项奖励
回复