Pages: [1]   Go Down
Author Topic: Random sequence generation  (Read 827 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/Random
Particularly the bit about using a random seed.
Some use a floating analogue input.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16520
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again for the help and guidance on this one.

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

randomSeed(analogRead(0));

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?)
Logged

"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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
Logged

Rob Tillaart

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24370
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

micros might be better.
Logged

"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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12465
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

definitely smiley
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: