I had a welcome screen and a button press to start the game and used the millis() reading at the point of the button press to seed the random number generator and start the generation of the sequence. So you need something effectively 'random' to kick things off. Since the seeding happens at the same point in the code each time it runs, perhaps in setup(), it runs the RNG with the same value each time. It also doesn't have an onboard persistent clock, and so essentially resets/ restarts each time the unit is powered up. Pseudo meaning it isn't truly random, and is based on a formula and the previous value of some variable and/ or the machine state. The arduino, like every computer, uses a pseudo random number generator to generate random numbers. So for the first setting of the seed, analogread is definitely a better way to go. NB you don't want to use millis to set the seed on the first go as your startup times will be similar from one start to the next. So unless the user can time there input to the same millisecond you will get every changing random sequences.ĭo you need to do the above? Not really, but it can give a more random result. RandomResetCntr = random(50, 100) // or whatever counter range that works for you. Instead of using one of the analog pins (which can tend to return similar values) I use the timer to set the seed after some random number of user inputs. If you need a completely random and totally unpredictable random sequence. So, for the same seed values, you will always get the same sequence.Īs someone pointed out using one of the analog pins is a good way to get an initial random number and is usually good enough. The random number generation is the output of a calculation based upon the previous one generated - or in the case of the first one, the value passed to the set seed function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |