I'm trying to program a little puzzle with 4 push buttons and a servo mounted to a little box. There are led's and a buzzer for user feedback and eventually there will be a reed switch to detect if the lid of the box is open or not but essentially the user needs to work out the sequence of the 4 push buttons to get the servo to unlock the box.
I thought I had things cracked but there is a strange thing going on with my randomisation script.....
My intention is that on power up, the box is locked and a random sequence is generated (eg 1234 or 4231 etc).... this remains constant until the code is cracked then another random sequence is generated and so on....
At first glance, all seemed to be fine BUT I noticed that every time the arduino is powered up, it is the same sequence generated.... in fact the once these are cracked, the first 4 sequences are identical....
Here is the code for the function that is called to randomise the sequence.
void randomise(){
 target[0]=random(1,5);
 target[1]=random(1,5);
 while(target[1]==target[0]){
 target[1]=random(1,5);
 }
 target[2]=random(1,5);
 while(target[2]==target[0]||target[2]==target[1]){
  target[2]=random(1,5);
 }
 target[3]=random(1,5);
 while(target[3]==target[0]||target[3]==target[1]||target[3]==target[2]){
  target[3]=random(1,5);
 }
}
If anyone can point out what I presume is an obvious school boy error, please let me know.
No, software random generators are really pseudorandom generators. Given the same starting 'seed' value they always generate the same sequence of numbers. This can be useful at times for testing purposes, etc. However most/many application one will create a new seed value before the first reference to it's use.
It's a difficult one - you need a source of entropy.
A very, very fast external clock and a series of digital reads.
A GM tube and a source of ionising radiation (a use for all those old smoke detectors?)