cancel
Showing results for
Did you mean:

# random number

Associate II
Posted on January 15, 2013 at 15:42

Hi,

I use STM32F107.

I have to generate a random number. I don't see how to do it :\

Anybody for help ?

#random-number #rng
9 REPLIES 9
Guru
Posted on January 15, 2013 at 16:04

I don't think they contain a random number generator, so you'll need to use a pseudo-random generator and seed it from some unpredictable source. The core cycle counter is somewhat predictable, perhaps you have some other inputs, or user interactions you can use?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Associate II
Posted on January 15, 2013 at 16:54

You can save the seed to the flash and then at startup read the saved seed from flash.

This way you will have new random numbers after each power off/on.

Associate II
Posted on June 07, 2013 at 14:49

Yes I have some inputs and I can use the HMI !

But how implement this function in the software? what I need to do it?

Associate II
Posted on June 07, 2013 at 15:51

A very simple and crude RNG is to perform a multiplication and addition, but truncating the high order bits.  Not the best but it usually yields a fairly high Chi-square statistic.  Not reccomended for gaming equipment.

The original Microsoft 4K Basic circa 1975 used this for the RND() function, except it was floating point.

The F1 series doesn't have hardware RNG.  The F2/F4 does have a 32 bit RNG unit.

uint32 seed

seed = seed + 271828192

seed = seed * 314159

After a few iterations the seed will overflow, which is what you want.  Be sure to multiply by an odd number so the low order bit varies.  You can improve on it by extracting bits in the middle of seed for the actual random number, since the low order bit is predictable.  Intitalize the seed from some real time event at startup, like the time (to the microsecond) to the first operator button press.

Jack Peacock
Associate III
Posted on June 07, 2013 at 16:33

my 'standard' means of generating a random number is to have a free running timer and reading it when some external event (keypress, serial byte arraving, ...) happens

Erik
Associate II
Associate III
Posted on June 08, 2013 at 17:09

and, again we see the male cow manure of calling pseudorandom random

Associate III
Posted on June 08, 2013 at 20:15

my favorite episode re the above (assuming pseudorandom is random) is this

J1708 specifies that, in case of a collision, a random delay is to be inserted before attempting the transmission again. Two J1708 devices used the same so called random routine and kept colliding for a whole weekend before I stopped them.
Associate II
Posted on December 16, 2013 at 06:12

This code uses hardware on the stm32f103 to generate true random numbers without having to add any new hardware: http://www.gniibe.org/memo/development/gnuk/rng/pqrng