random number generator

bug13

Joined Feb 13, 2012
2,002
Hi guys

I need to generate a random number to seed the srand() in a pic right after power up in general (Most likely a pic18), how do you guys do it usually? I know the internal watch dog oscillator is not very accurate, can we do something with this??

I have tried reading a floating ADC pin, but it doesn't look very random to me. Maybe I didn't do it properly?? Do I need to connect this floating ADC pin to something to create more randomness??

Thanks guys!

WBahn

Joined Mar 31, 2012
29,469
You don't want the ADC input to saturate. Perhaps make a voltage divider with really high-valued resistors and then a relatively long trace to couple noise into it?

Record the lsb of your ADC output over lots of successive reads (thousands and thousands) and see how random they appear. If acceptably random, then just set one bit of your seed on each ADC read using the lsb of the read value. Any other process that makes the lsb of a read value unpredictable will work the same way.

BR-549

Joined Sep 22, 2013
4,928
Write the RTC to a RANDOMNUMBERVARIABLE. Take your second son's age....or your second wife's anniversary.......and multiply it by 109. Take that result and shift RANDOMNUMBERVARIABLE to the right with that number. Now subtract 3.

RANDOMNUMBERVARIABLE now contains your random number.

I do not make my living this way.

WBahn

Joined Mar 31, 2012
29,469
Write the RTC to a RANDOMNUMBERVARIABLE. Take your second son's age....or your second wife's anniversary.......and multiply it by 109. Take that result and shift RANDOMNUMBERVARIABLE to the right with that number. Now subtract 3.

RANDOMNUMBERVARIABLE now contains your random number.

I do not make my living this way.
That's a very good thing.

Let's say RANDOMNUMBERVARIABLE is a 32-bit signed value. That means that when you shift it right by 32 or more you get either 0 or -1 (all zeros or all ones). Now you subtract 3 and you get either -3 or -4. If it's an unsigned value, then you just always get -3.

Really good random number generator there, huh?

BR-549

Joined Sep 22, 2013
4,928
Pardon me. Rotate instead of shift.

Picbuster

Joined Dec 2, 2013
1,045
for microchip look as rand() and srand() functions.
Picbuster

WBahn

Joined Mar 31, 2012
29,469
Pardon me. Rotate instead of shift.
So why multiply by 109? Again, if we are talking about either a 16 bit or a 32-bit value this is the same as 13.

WBahn

Joined Mar 31, 2012
29,469
for microchip look as rand() and srand() functions.
Picbuster
He's trying to find a way to generate a reasonably random seed for srand().

The normal way for non-critical applications is to use the clock value. But if you don't have a real-time clock that returns the actual date/time data and rather just returns a counter value that starts when the system was powered up or reset (or, worse, when the program is launched), you tend to get values chosen predominantly from a rather small range.

BR-549

Joined Sep 22, 2013
4,928
WBahn....you are so serious. The best one can hope for is an un-certain/un-known number. The amount of un-certainty is not important.

If the value of the random is important......it's not random.

And a true random value could repeat. Therefore a truly random number is probably not wanted.

Right? Isn't there some kind of long lived math theory argument over obtaining randomness?

I have no idea of the function wanted. But with the way these micro-controllers are going.....it will be a standard function soon. With the amount of measurable physical and electrical data that can be IOT'ed at low power.........IOT networks won't be simple for long.

With that I will leave you experts at it.

DickCappels

Joined Aug 21, 2008
10,102
It depends on how close to random you want it to be. Quantum effects such as the noise from an avalanche ( such as a 6.2V "Zener") can produce close to random values if clocked by a clock source well isolated from the noise source. You need amplification and a comparator between the diode and the digital input at which the signal is sampled.

WBahn

Joined Mar 31, 2012
29,469
WBahn....you are so serious. The best one can hope for is an un-certain/un-known number. The amount of un-certainty is not important.

If the value of the random is important......it's not random.

And a true random value could repeat. Therefore a truly random number is probably not wanted.

Right? Isn't there some kind of long lived math theory argument over obtaining randomness?

I have no idea of the function wanted. But with the way these micro-controllers are going.....it will be a standard function soon. With the amount of measurable physical and electrical data that can be IOT'ed at low power.........IOT networks won't be simple for long.

With that I will leave you experts at it.
If you artificially preclude numbers from repeating, then you have reduced the uncertainty in the next number making it less random. You do NOT want this. There have been significant breaks into several systems because of traits such as this.

There are lots of tests for randomness that random number generators must pass depending on what they are being used for. A good random number generator for one application may be a lousy one for another.

MrChips

Joined Oct 2, 2009
29,792
TS is asking for a one-time seed after power-up, not a random number generator.

There are standard polynomials you can use for a random number generator.
For a one-time seed, use the milliseconds register from the RTC if one is available.

nsaspook

Joined Aug 27, 2009
12,254
Hi guys

I need to generate a random number to seed the srand() in a pic right after power up in general (Most likely a pic18), how do you guys do it usually? I know the internal watch dog oscillator is not very accurate, can we do something with this??
I usually use a Physical Unclonable Function to generate initial randomness when entropy sources are limited in small controllers.
https://www.microsemi.com/document-...martfusion2-libero-soc-v11-7-application-note
https://github.com/Tribler/tribler/issues/3064

For a PIC18 right after a cold power up the SRAM contains a random number of bits that flip state every power up before a possible run-time routine zeros out memory. To use this capability to generated random seeds in C you first need to link in the no-zeroing run-time function to leave memory untouched, then you should characterize the SRAM block into stuck one or zero bits vs changing bits with a few power resets. Once you have the only changing bits masked and stored you can use a hash function to generate a random seed(s) dynamically or store them in no-volatile memory for use with another program.

http://people.csail.mit.edu/rudolph/Teaching/Lectures/Security/Lecture-Security-PUFs-2.pdf

Last edited:

wayneh

Joined Sep 9, 2010
17,461
There are lots of tests for randomness that random number generators must pass depending on what they are being used for. A good random number generator for one application may be a lousy one for another.
My statistics professor in B-school was on a team that showed the Vietnam era draft lottery was not actually random, and changed it. Such things do have real-world implications.

WBahn

Joined Mar 31, 2012
29,469
TS is asking for a one-time seed after power-up, not a random number generator.

There are standard polynomials you can use for a random number generator.
For a one-time seed, use the milliseconds register from the RTC if one is available.
And what is the range of values that that milliseconds register is going to have in it each time the PIC is powered up? If it is reset to zero as part of the power-up sequence, then the range of values is likely to be quite small and highly nonuniform. Maybe that's good enough, but very possibly maybe it's not.

MrChips

Joined Oct 2, 2009
29,792
And what is the range of values that that milliseconds register is going to have in it each time the PIC is powered up? If it is reset to zero as part of the power-up sequence, then the range of values is likely to be quite small and highly nonuniform. Maybe that's good enough, but very possibly maybe it's not.
An RTC keeps track of real time and hence would be battery powered. The range of values would be 0-999 ms and is only reset when the seconds register overflows from 59 to 00.

WBahn

Joined Mar 31, 2012
29,469
An RTC keeps track of real time and hence would be battery powered. The range of values would be 0-999 ms and is only reset with the seconds register overflows from 59 to 00.
So that still leaves the question of whether a span of just 1000 is sufficient for the application. That's actually a very small span. But perhaps it's enough. If it were being used in an application in which a birthday attack were possible, then it could have real issues because you would expect to only need about 32 runs before you would expect to see two runs with the same seed.

WBahn

Joined Mar 31, 2012
29,469
Hi guys

I need to generate a random number to seed the srand() in a pic right after power up in general (Most likely a pic18), how do you guys do it usually? I know the internal watch dog oscillator is not very accurate, can we do something with this??

I have tried reading a floating ADC pin, but it doesn't look very random to me. Maybe I didn't do it properly?? Do I need to connect this floating ADC pin to something to create more randomness??

Thanks guys!
@bug13: I've been hoping to hear back from you with some details about what you are trying to do and what your requirements are for the randomness of this seed.

bug13

Joined Feb 13, 2012
2,002
You don't want the ADC input to saturate. Perhaps make a voltage divider with really high-valued resistors and then a relatively long trace to couple noise into it?

Record the lsb of your ADC output over lots of successive reads (thousands and thousands) and see how random they appear. If acceptably random, then just set one bit of your seed on each ADC read using the lsb of the read value. Any other process that makes the lsb of a read value unpredictable will work the same way.
I think I prefer this method.

When I tried reading a ADC, I was looking at the whole 10 bits data, and that wasn't random enough for me. But if I can construct any number with the lsb, I think that will be good enough for me. (I will do some test)

A bit more info about my application. we don't think I need a true random number (well, that's what I think anyway). I am working (planing and researching at this stage) on a multi-master wired bus system. Once a collision is detected, we need a way to backoff some random time, and try again.

I guess that lead to another question, will this rand() with a random seed good enough? I think it's good enough. But I would love to hear what you guys think. I am guessing to answer this question, you guys properly need more info. But I don't know what I don't know, so let me know what other info you need.

bug13

Joined Feb 13, 2012
2,002
Write the RTC to a RANDOMNUMBERVARIABLE. Take your second son's age....or your second wife's anniversary.......and multiply it by 109. Take that result and shift RANDOMNUMBERVARIABLE to the right with that number. Now subtract 3.

RANDOMNUMBERVARIABLE now contains your random number.

I do not make my living this way.
Sounds like a good idea, let me use this method to pick my Lotto number, I will send something to you if I hit the jackpot ;-)