random number generator

Thread Starter

bug13

Joined Feb 13, 2012
2,002
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.
I need something good enough for a multi-master back-off when a collision is detested in a wire system.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
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.
Most of the PIC I use don't have a RTC
 

MrChips

Joined Oct 2, 2009
34,839
If you want to use the ADC reading, then simply reverse the order of the bits.

For collision delay, you can back-off by a value determined by the unit's priority and the number of attempts, plus a random amount.
In other words, a low-priority device has to wait for a longer length of time which increases with every subsequent unsuccessful access.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
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
Properly more than what I need, but it's very interesting, I think I will keep this in mind and look into it a bit more when I have some spare time. Thanks for the info.
 

Thread Starter

bug13

Joined Feb 13, 2012
2,002
It looks like I get better randomness with the last 2 bits than the last bit in a 10 bit ADC in an Arduino Nano. (I know I said I am using a PIC, but I got an Arduino Nano now, and it's quick).

here is my code:
Code:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(analogRead(0) & 0b11);
}
Capture.PNG
 

John P

Joined Oct 14, 2008
2,063
If events on your network aren't generated by automatic processes which work the same way every time, you can use a timer to generate a random number. Just count microseconds since the processor started, or time between packets sent over the network, or something like that. But this only works if operation can genuinely vary from one occasion to another, which might fail if the whole setup is essentially running at a constant speed.
 

MrChips

Joined Oct 2, 2009
34,839
Again, I think all of this is overkill. Pick any random number and use that as the seed. Any subsequent call to rnd() will give another random number. This is not going to have any issues with the randomness of the delay time if you do as I proposed in post #23. That is, the random time delay is only an additional portion to the total back-off delay. The delay increases if there are further collisions. This is how CSMA/CD ethernet works.
 

WBahn

Joined Mar 31, 2012
32,893
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.
You probably don't need a random seed at all. Each device has a unique address of some type, such as a MAC, right? Just use that as your seed. You could probably use the same seed for all of the devices since each device is going to experience a different history collisions detected. They will quickly get out of sync and if they are out of sync by just one call to rand(), they should work fine. You can make this happen quicker by simply advancing the PRNG each time you transmit a packet.
 

panic mode

Joined Oct 10, 2011
5,008

cqexbesd

Joined Dec 16, 2017
6
I also needed random numbers on an MCU (AVR in my case). Luckily the randomness didn't have to be great but I couldn't use e.g. a clock as the seed because startup timing was quite deterministic and it was at startup that I needed my first random value (and I didn't have an RTC).

I ended up reading my seed from the EEPROM. After seeding the PRNG I wrote a random number back to the EEPROM to be the seed at next power on.

It worked well enough for my purposes.
 

panic mode

Joined Oct 10, 2011
5,008
i agree with WBahn, each devide will have something unique. even if it wasn't initial usually seed can be RTC value or really just about anything (constant too...). if using constant as a seed, you may want to save last value and use it as next seed.

generating pseudo random numbers from that point can be as simple or complicated as you like. there is tons of PRNG examples, usually using masking and rotation...
unless you are developing something really secure, PRNG will do just fine.
 

Hymie

Joined Mar 30, 2018
1,347
Many, many years ago I wrote a program to generate random numbers – the core of the generator produced a binary number 1 or 0 (randomly).

Then say you wanted a random number between (including) 1 and 10, the program would use 4 bits (of randomly generated 1s and 0s) to generate a number from 0 to 15 (if the generated number was >10 it would be discarded).

Of course the randomness still depended on how random the core generator functioned in producing a 1 or a 0.
 

Sensacell

Joined Jun 19, 2012
3,785
I use a simple RC network on an IO pin.

Set the pin high, start a really fast counter.
Set the pin to input mode, poll it until it's low, latch the counter.
Make sure the RC time constant greater than 3 or 4 roll-over periods of the timer.

If it's a crappy 20% capacitor with a horrible tempco and the counter is running at many MHz, it's a pretty good random.
 
Top