Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 2 months ago.
Random number in C.
Using rand() function does not give me random number. If i reset the board, it will always return me the same number. Putting srand(time(NULL)) did not help. Is there any way to get random number?
Question relating to:
5 Answers
9 years, 2 months ago.
https://developer.mbed.org/questions/2886/Why-is-the-rand-function-not-the-least-b/
Try this link it may help :)
9 years, 2 months ago.
Hello,
STM32F4xx devices have a true random generator :-)
9 years, 2 months ago.
It would be nice if there was an mbed random function (or the C one being somehow overloaded, which should be possible I guess), which depending on the chip would use for example a hardware random number generator.
For your chip, I think adding your Time(NULL) doesn't work because the RTC is also reset when you reset the chip, so it always returns zero. But if you switch to latest mbed lib (or mbed-src, dunno if it is in mbed yet), I believe the RTC is not reset anymore upon a chip reset, so then it might properly seed your random number. Alternatively you can do a bunch of AD conversion for example, and use the noise from the LSB.
9 years, 2 months ago.
If you have a analog input free, you could use the code below to generate a random number.
It uses the two least significant bits of the analog signal to create a random number. You have to leave the analog pin unconnected. And I can tell you, it won't start every time at the same number ;)
AnalogIn analog (p20); unsigned int random_generator (void) { unsigned int x = 0; unsigned int iRandom = 0; for (x = 0; x <= 32; x += 2) { iRandom += ((analog.read_u16() % 3) << x); wait_us (10); } return iRandom; }
9 years, 2 months ago.
Andy is correct. Try this in MBED:
int a = rand();
It may not be the best random number generator (I would need to check), but it should suffice for most purposes. The time you need to be very careful is if you are writing a cryptography algorithm.
An ADC would be *very* slow in comparison and possibly not random.
It would be more random than an unseeded random number generator. At the very least you need something to seed it with something random.
posted by 20 Oct 2015It depends on what you connect to the ADC of course. A white noise generator I accept would be the ideal, but you will have the performance hit of performing an ADC.
posted by 20 Oct 2015Just about any ADC will have some noise on the least significant bit or two unless the inputs are tightly clamped to one of the rails.
Lets say that with a floating input the ADC you have a 90% chance of getting 0 and a 10% chance of getting a 1 on the LSB (I'd expect it to be closer to 50-50) then I make it about 10,000 possible values you could get by using 32 ADC reads. (crude back of the envelope number assuming you get 3 1's, it will be more random than that)
If you use that as your seed for the rand function then you're going to get a fairly random result.
If you need more randomness generate a second number from the ADC and xor the two together, that should roughly square the number of permutations. Repeat (with diminishing returns) until you reach the randomness required. The lower the probability of getting a 1 the more loops needed for a given level of randomness.
mbed will normalize the ADC output to 16 bits but as long as we wrap things around when we do a shift we don't need to know where the LSB is and so it remains hardware independent.
Untested (and uncompiled) code to perform an ADC size independent seed of the random number generator.
AnalogIn noise(pin something); uint32_t seed =0; const int loops = 1; // increase if LSB has a bias. for (int i=0;i<(32*loops);i++) { seed ^= noise.read_u16(); if (seed & 1<31) { // shift left and wrap. seed <<= 1; seed |= 1; } else seed <<= 1; } srand(seed);