Is it possible to generate random numbers without using randomSeed() and an analog pin?
TrueRandom library also uses Analog Pin 0.
https://code.google.com/p/tinkerit/wiki/TrueRandom
Any other way?
Thank you very much.
Is it possible to generate random numbers without using randomSeed() and an analog pin?
TrueRandom library also uses Analog Pin 0.
https://code.google.com/p/tinkerit/wiki/TrueRandom
Any other way?
Thank you very much.
You could seed the RNG with the time taken to press a key or some other user event.
Use "micros()" but remember it has granularity of 4.
Is it possible to generate random numbers without using randomSeed() and an analog pin?
Not using the random() function there isn't. You must use randomSeed().
You do not have to get a starting point for randomSeed() from an analog pin, and shouldn't as an unconnected analog pin's value is not random.
Where else may I get a starting point for randomSeed()? I wish to generate different values with random().
Reference mention this about random:
If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin.
Thank you.
This solution worked for me, but I'm afraid it will consume a bit more resources. I hope to use native Arduino libraries.
https://code.google.com/p/avr-hardware-random-number-generation/wiki/WikiAVRentropy
Can you have the user press a button to start the program ? If so, then use the millis() or micros() value at the time of the button press to seed randomSeed(). It will still not be entirely random but should differ for most runs of the program.
Thank you for the suggestion UKHeliBob. Although I wish for the random number to be generated just ONCE. And that is when the Arduino is powered up. The random number will be utilized just once also. To determine a certain LED lighting sequence. Any other solutions?
To clarify, I wish for the program to randomly pick between 4 LED lighting sequences on power up. It will go through the lighting sequence just once. A simple IF loop will do the trick. I just need a random number from 0 to 3 or 1 to 4 or something.
Nothing you have said would preclude the use of the method that I suggested. You are presumably going to run the program more than once so, power up the Arduino and have a button labelled START. It will be pressed at a different time after the Arduino is powered up each time that happens, hence the seed will be different each time, or largely so.
bdumaguina:
TrueRandom library also uses Analog Pin 0.
Google Code Archive - Long-term storage for Google Code Project Hosting.
Any other way?
TrueRandom is no better than just reading a floating pin (which is also unreliable). Stay away from it.
The reliable but not very sexy way to seed random...
Wire up a giger counter and use the time it takes from power up to the first click to seed the random number generator and the generate your random number in the range you want.
Or, ( not as good )
Use a photo resistor to give you a light reading and use that to seed.
you could spend a couple of bucks, add a clock module, you can now use the real time to generate your random number. this would also allow you to have certain time of day activate lights or you could add a photo sensor and have the lights go on and based on millis() use it for your random number.
jasit
The Entropy library seems to be interfering with my program. So..can't use that.
Thank you all for your suggestions. Highly appreciate it. My hardware has already been constructed. So giger counters or clock modules are not an option. (Should have considered this random number generation at program inception). Anyway, I will look into photo resistors. As they can easily be placed into the thing.
Personal Message from wanderson
I noticed your posting that you needed a different random seed value every time your sketch runs, but that neither the analogRead(0) nor my Entropy library was working for you. I put together an example sketch that uses the same basic method as my Entropy library, but gets rid of a lot of the overhead caused by the other library functions and the pool of random numbers it generates. This example will produce just one random seed value, and only takes about 0.5 second to run in the setup() function.
The example can be found herel This is a simple straightforward and relatively lightweight example of how to seed the internal PRNG function with a truly random value · GitHub
This solution worked for me. Thank you very much.