The application will allways execute the same way so the random seed is always the same.
An often used initializer for randomSeed is analogRead(A0) with A0 connected to nothing. It is still not really random (it will fail on real random tests) but for many applications it is random enough.
Another option is to use a button and use micros()/4 to initialize the random seed if the key is pressed and/or released.
robtillaart:
An often used initializer for randomSeed is analogRead(A0) with A0 connected to nothing. It is still not really random (it will fail on real random tests) but for many applications it is random enough.
Another option is to use a button and use micros()/4 to initialize the random seed if the key is pressed and/or released.
can you show me the code please as im new to arduino thanks
what i have wrong in this code as always the same led is going on
Several things. You should be generating two random numbers - one for the pin and one for the state - not three.
int randPin = random(11, 14); // Generate value from 11 to 13
int randState = random(0, 2); // Generate value from 0 to 1
digitalWrite(randPin, randState); // Set the pin to the state
Seeding the random number generator would be useful, too.
You generally seed the random number once (outside of the loop). But, that shouldn't be causing you a problem since you are seeding the random generate with a diferent value each time.
I agree that it makes more sense to randomly choose the HIGH/LOW (1/0) state randomly, rather than choosing the pins randomly.
I don't think you need a random number for the pin at all... You can simply write random data to ALL of the pins/LEDs every time through the loop, to pop-up a random pattern each time through the loop (if that's what your're trying to do).
Unless, you wanted to change only one pin each time through the loop. Then, you might want to randomly choose the one-pin to be "active" each time. If you do that, you'll only get a change about half the time, randomly...
P.S.
I dont know what your goal is, but once you get this working you might want to make one more enhancement...
Since there are 8 different possible "patterns", you are going to get a "repeat" about once per second (randomly)... i.e. you'll get the same pattern twice in a row... so on-average, you'll only get about 9 "changes" per second (instead of the expected 10), and it's going to "look like" a glitch or a pause. If that's a problem, you might want to look for that and skip the delay if you get the same pattern twice in a row.
If there are any other "invalid" states like all-on, or all-off, you can check for that too, and start the loop over if you get something invalid.