Go Down

### Topic: Random sequence generation (Read 2543 times)previous topic - next topic

#### DaveyK

##### Jun 18, 2011, 11:31 pm
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.
Code: [Select]
`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.

Dave

#### AWOL

#1
##### Jun 18, 2011, 11:35 pm
http://arduino.cc/en/Reference/Random
Particularly the bit about using a random seed.
Some use a floating analogue input.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### DaveyK

#2
##### Jun 18, 2011, 11:58 pm
AWOL,

Thanks for the nudge in the right direction.

So random isn't random then by default..... well it is but always in the same random pattern.... interesting.

Anyway the random sequence generation in my program is now working, thanks again.

#### retrolefty

#3
##### Jun 19, 2011, 06:04 am
Quote
So random isn't random then by default

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.

Lefty

#### DaveyK

#4
##### Jun 21, 2011, 10:43 am
Thanks again for the help and guidance on this one.

I've followed the tutorial on the link posted and used the:

Line to get the result I wanted.....

... not a problem this time but is there any other ways to achieve this if for example I didn't have a spare analogue port?

Dave

#### AWOL

#5
##### Jun 21, 2011, 11:01 am
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?)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### robtillaart

#6
##### Jun 21, 2011, 11:42 am

Just ask the user to press a start button, the millis() will provide quite an acceptable seed for the random function.

Check the old forum for random excersises - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294239019 -

Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### AWOL

#7
##### Jun 21, 2011, 11:44 am
micros might be better.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### robtillaart

#8
##### Jun 21, 2011, 11:46 am
definitely
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up