Random number on fresh boot (Aruino Nano)

Hi!

What I want to do is use a Nano to create a random delay between 1-10 seconds.
This is a battery powered project, so the Nano will only be powered on, decide the delay, then be powered off.

Three questions:

  1. Is it even worth using the random function, since we will power off immediately after selecting a number?

  2. Would it be the same as just reading a spare ADC and scaling that reading to give me a delay between 1 and 10?

  3. How random would an analog read on a spare pin on the Nano actually be? I'd imagine it probably settles around a certain range and stays there.

Thanks for any input

Example of how to obtain a truly random number upon startup (source):

long randNumber;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
  randNumber = random(300);
  Serial.println(randNumber);
  delay(50);
}

Hi Power_Broker.

I really appreciate the reply, however I already read that source. It's the reason I have the questions I have.

Thanks,
JW

If you've already seen the example, what are you confused about? Does it not do what you want to do? You can edit it for your purpose like this (minus the powering down part):

long randNumber;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
  delay(random(1, 300));
}

void loop() {
  // do things
}

My questions?

Jiggawhat:
My questions?

Asking those questions doesn't make sense considering you already know a perfect solution to the problem, but here we go:

Jiggawhat:

  1. Is it even worth using the random function, since we will power off immediately after selecting a number?

That's up to you and how you want your project to work. However, using the random function doesn't cost any money to use, so why not? :slight_smile:

Jiggawhat:
2. Would it be the same as just reading a spare ADC and scaling that reading to give me a delay between 1 and 10?

That's not a good way to do it - the tutorial code has the better way.

Jiggawhat:
3. How random would an analog read on a spare pin on the Nano actually be? I'd imagine it probably settles around a certain range and stays there.

Somewhat random due to electromagnetic interference, digital noise inherent to the MCU circuitry, noise due to externally attached circuitry, ground bounce, etc., but it will not reliably give you the range of randomness you're looking for - the tutorial code will, though.

Power_Broker:
Somewhat random due to electromagnetic interference, digital noise inherent to the MCU circuitry, noise due to externally attached circuitry, ground bounce, etc., but it will not reliably give you the range of randomness you're looking for - the tutorial code will, though.

If the analogRead settles in to some value then the seed is the same and the number generated will be the same every time. If there is enough entropy in the analogRead to get a different seed every time then there is enough entropy to just use that as the random number. By calling the random function you aren't adding any additional randomness.

Delta_G:
If the analogRead settles in to some value then the seed is the same and the number generated will be the same every time. If there is enough entropy in the analogRead to get a different seed every time then there is enough entropy to just use that as the random number. By calling the random function you aren't adding any additional randomness.

Sort of, but that requires using the map() functionality while also tuning the min/max expected ADC value. It just doesn't make sense to me why you would do this.

It's less work and more reliable to just use the seed and random functionality already built into the Arduino environment instead of hacking your own way.

How about creating a random number or seed from LSBs of successive analogReads?

the Nano will only be powered on, decide the delay, then be powered off.

Is there any opportunity for user input, such as pressing a button, during this process ?

UKHeliBob:
Is there any opportunity for user input, such as pressing a button, during this process ?

I know what you're thinking, and no unfortunately there isn't
I have no inputs at all. I'm open to adding something, like an amplifier picking up noise to try and catch a more random input from the ADC though.

I have no inputs at all.

You must have at least one analogue input if you are considering reading the ADC

What will be turning the Nano on and off and what are you going to do with the delay period once it is decided ?

  1. How random would an analog read on a spare pin on the Nano actually be? I'd imagine it probably settles around a certain range and stays there.

That's true and you can test that to see how much "noise" you are getting...

If you add an "antenna" (a "long" unshielded wire) to the analog input you can pick-up electro-magnetic radiation from the 50/60Hz power lines all-around you. (Or if you have a built-in power supply you may be able to get an AC voltage from the transformer-output.) That's not truly-random but it's variable and it's uncorrelated with the start-up time of the processor. Or, you can build an analog random noise generator. Somewhere I read that something like that is used in casinos.

It usually comes-down to, "How random" does it need to be?" In most applications you just need "unpredictable" or hard-to-predict results and usually you CAN get "random enough".

...You can get into crazy-philosophical discussions about, "Does true-randomness exist?" :stuck_out_tongue: With enough knowledge could someone (or some machine) predict the results? Can God predict the results? But I just ask, "Is it random enough?" or "Is it unpredictable enough?"

I know what you're thinking, and no unfortunately there isn't
I have no inputs at all. I'm open to adding something

If it's THAT important you can add a button for "human randomness timing". (i.e. FORCE a button-press by holding-up everything in a loop until the button is pushed to seed the random number generator, etc.)