I'd like to be able to create new random numbers while the program is running. I've recently learned that Arduino will create the same number order unless changing the seed. So I'm using analogRead(0) to get a unique seed (I think I'm explaining that correctly), which mostly works. But this only provides unique random numbers with a restart. Is there a way to get unique random numbers, while the program is still running?
I'm wanting to recreate the Simon Says game, where in round one, one random light blinks and the user has to push the corresponding button, but then in round two, a new series of random numbers is created, so that two lights blink and wait for the user to push the corresponding lights. This requires new random numbers each round.
Use the value of micros() as the parameter for randomSeed() before generating the random sequence. As the user will take a varying amount of time to respond the value of micros() will vary each time
This appears to be working. I do have one question though. If I understand correctly, code in void setup() only runs once at the beginning, whereas the void loop() code runs repeatedly. So how does changing the parameter in the setup get accessed from commands in the loop? Below, you'll see that I included micros() in the randomSeed in the Setup but I get new random numbers every time the button is pressed. The code is working (thank you so much!), but I'd also like to understand if, it's easy to explain. Thanks again!
My suggestion was to use micros() in randomSeed() before each new round is started in the game. The value of micros(), and hence the value used in randomSeed() will vary each time the game is played due to the varying time that the user takes to respond when pressing buttons to follow the Simon sequence
You will always get a sequence of the same numbers given the same initial seed. What you are doing is to create a random seed, this means you will get a sequence of numbers you haven't seen before.
There is no need to do that, just once in the setup function will do fine as long as the initial seed is random and of sufficient precision. The time between a user pressing a key, in micro seconds is a great way to do things.
Using a floating analogue input is not a very good source of a random seed, as the range of numbers is so small. That is between 0 and 1023, so the chances of a repeat seed are high. Especially as it is unlikely that you will get an even distribution over the whole range.
Without a user input then you would need some other source of a random event, like the time between clicks on a Geiger counter measuring background radiation.
In the days before micro computers I even made a random source by soaking some cotton wool in a silver nitrate solution and applying a voltage across it. This caused small dendrites of silver to start growing between the two electrodes. When one of these dendrites makes a connection between positive and negative it causes a short across the supply and this acts like a fuse and breaks the dendrite. You can monitor the resulting current pulse from the cell, and the time between these is your random number.
As long as the initial random seed was indeed random, then your question does not make much sense. Are you saying that you get a random sequence in the first round, but then the 2nd and subsequent rounds always produce an identical sequence? I'm assuming you are not somehow resetting the arduino between rounds.