# Generate random number, display on screen and require number input for lock

Hello, I am wondering if it is possible to program an Arduino to generate a random, 4 digit number, display that number on a screen of some sort, and also have that number be necessary to open a lock. If at all possible, I would like to have it do this on a button press.

I have done the research to find the generate random number code, which is awesome. I think that the code I would need is this:

``````long randNumber;

void setup() {
Serial.begin(9600);

// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
}

void loop() {
// print a random number from 0 to 9999
randNumber = random(9999);
Serial.println(randNumber);

delay(50);
}
``````

Is that correct? I am pretty new to this stuff, I have been working through a kit I bought and I am really excited to learn more.

Dylan Fleming

Is that correct?

Does it do what you want/expect when you run it ?
Do you want 9999 ever to be generated ?

UKHeliBob:
Does it do what you want/expect when you run it ?
Do you want 9999 ever to be generated ?

Unfortunately, I am not near my arduino in order to test it out, at current. I will be able to test it later today. I am mostly reaching out in order to get a starting point. I am relatively new to most of these things.

As for 9999 being generated, if it is, fine. If it is not, that is also fine. I take it by your question that I need to change the parameters of my coding,

randNumber = random(10000);

Is that more what I should be doing?

Thanks for the response!

``````randNumber = random(10000);
``````

That should give you a number between 0 and 9999 which is what you are aiming for as I understand it.

Keep in mind, as I have found out, random() is not very random.

Your code will generate a new number every 50ms or so. To do what you describe you need to generate one number, save it then compare it to what is entered.

Ditch the delay as soon as you can. Delay is bad.

Something that might be of interest, if you let your code keep generating random numbers, but don't use them until the user enters something that means a new random number is required, then you are adding an new source of randomness that will make the numbers more random. You are making the random number dependent on when the user does whatever it is they do to get the number, rather than just getting the next number in the sequence. When the user does something is pretty much a random event that you cannot predict.

[EDIT]
I was typing this while Danger was typing his response. I didn't set out to reply to him; I didn't know he was going to say that. However, you might view the above as my reply.

PerryBebbington:
Your code will generate a new number every 50ms or so. To do what you describe you need to generate one number, save it then compare it to what is entered.

Ditch the delay as soon as you can. Delay is bad.

Something that might be of interest, if you let your code keep generating random numbers, but don't use them until the user enters something that means a new random number is required, then you are adding an new source of randomness that will make the numbers more random. You are making the random number dependent on when the user does whatever it is they do to get the number, rather than just getting the next number in the sequence. When the user does something is pretty much a random event that you cannot predict.

[EDIT]
I was typing this while Danger was typing his response. I didn't set out to reply to him; I didn't know he was going to say that. However, you might view the above as my reply.

If I'm following your method correctly, that's pretty much how I wrote my random function in a "for fun" sketch.