Go Down

Topic: random numbers run dumb (Read 268 times) previous topic - next topic

Geek Emeritus

I have my project announce that it is starting up by playing one of 15 files in a folder. it used to play all 15 files. then, it did not.

I wrote a sketch to determine the randomness of the numbers. regardless of how I derive my random seed, I get one of two random numbers, 1 or 8

analogRead(A0) was a bust. I tried SerialRead of the GPS output. sketch and results as noted below:

Code: [Select]
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial3.begin(9600); // GPS on Serial3
}

void loop()
{
  // put your main code here, to run repeatedly:
  //pinMode(A0, INPUT); int randomNumberSeed=abs((analogRead(A0)-450)); does not work
  int randomNumberSeed = (Serial3.read());
  randomSeed(randomNumberSeed);
  Serial.print("Random Number Seed "); Serial.print(randomNumberSeed);
  Serial.print(" Resulting Random Number "); Serial.println(random(1, 15));
}


result:

Code: [Select]
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 50 Resulting Random Number 1
Random Number Seed 51 Resulting Random Number 8
Random Number Seed 48 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 50 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 68 Resulting Random Number 1
Random Number Seed 42 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 53 Resulting Random Number 8
Random Number Seed 48 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 54 Resulting Random Number 1
Random Number Seed 54 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 36 Resulting Random Number 1
Random Number Seed 71 Resulting Random Number 8
Random Number Seed 80 Resulting Random Number 1
Random Number Seed 82 Resulting Random Number 1
Random Number Seed 77 Resulting Random Number 8
Random Number Seed 67 Resulting Random Number 8
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 57 Resulting Random Number 8
Random Number Seed 48 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1


is this just my MEGA gone stupid?
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

TheMemberFormerlyKnownAsAWOL

#1
Jan 23, 2021, 08:21 pm Last Edit: Jan 23, 2021, 08:28 pm by TheMemberFormerlyKnownAsAWOL
Seed once, in setup ()
Just make sure there's something to read, otherwise you're always going to seed with -1.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

alto777

#2
Jan 23, 2021, 08:52 pm Last Edit: Jan 23, 2021, 08:53 pm by alto777
Seed the random number generator then take about 50 stabs and throw them away, you may find some better variation in the 51st generated number than you have in the first.

Too lazy to try myself, just an idea.

Edit: Well not lazy, just not in front of an Arduino just now.

a7

aarg

How about this?
Code: [Select]
long seed;
while (not Serial3.available() ) { seed++; }
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

david_2018

#4
Jan 23, 2021, 09:06 pm Last Edit: Jan 23, 2021, 09:08 pm by david_2018
I have my project announce that it is starting up by playing one of 15 files in a folder. it used to play all 15 files. then, it did not.
The obvious question is what were you doing in the past that worked, and what have you changed since then.

How many of the analog ports are unused?  Do a combination of multiplying, adding, and subtracting of several ports and you should get a fairly random number.

abdelhmimas

I have my project announce that it is starting up by playing one of 15 files in a folder. it used to play all 15 files. then, it did not.

I wrote a sketch to determine the randomness of the numbers. regardless of how I derive my random seed, I get one of two random numbers, 1 or 8

analogRead(A0) was a bust. I tried SerialRead of the GPS output. sketch and results as noted below:

Code: [Select]
void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial3.begin(9600); // GPS on Serial3
}

void loop()
{
  // put your main code here, to run repeatedly:
  //pinMode(A0, INPUT); int randomNumberSeed=abs((analogRead(A0)-450)); does not work
  int randomNumberSeed = (Serial3.read());
  randomSeed(randomNumberSeed);
  Serial.print("Random Number Seed "); Serial.print(randomNumberSeed);
  Serial.print(" Resulting Random Number "); Serial.println(random(1, 15));
}


result:

Code: [Select]
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 50 Resulting Random Number 1
Random Number Seed 51 Resulting Random Number 8
Random Number Seed 48 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 50 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 68 Resulting Random Number 1
Random Number Seed 42 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 53 Resulting Random Number 8
Random Number Seed 48 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 54 Resulting Random Number 1
Random Number Seed 54 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 36 Resulting Random Number 1
Random Number Seed 71 Resulting Random Number 8
Random Number Seed 80 Resulting Random Number 1
Random Number Seed 82 Resulting Random Number 1
Random Number Seed 77 Resulting Random Number 8
Random Number Seed 67 Resulting Random Number 8
Random Number Seed 44 Resulting Random Number 1
Random Number Seed 49 Resulting Random Number 8
Random Number Seed 57 Resulting Random Number 8
Random Number Seed 48 Resulting Random Number 1
Random Number Seed 44 Resulting Random Number 1


is this just my MEGA gone stupid?
Can you try the following:

long randNumberSeed;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
  randNumberSeed= random(300);
  Serial.println(randNumberSeed);
 
}

TheMemberFormerlyKnownAsAWOL

Why so wordy?
Code: [Select]
void loop() {
  Serial.println(random(300));
}
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

alto777

#7
Jan 23, 2021, 10:33 pm Last Edit: Jan 23, 2021, 10:51 pm by alto777
OK, so I did that thing I advised, seed the generator and then ignore 50 values…

I get more just two output values, so.

Obvsly identical seeds reach identical 50th values, but different seeds leading to 1 or 8 don't end up reaching the same value.


Code: [Select]
void setup()
{
Serial.begin(9600);
}

/* the OP's seeds */
int  seeds[] = {
44, 50, 51, 48, 49, 50, 49, 44, 44, 44,
68, 42, 44, 53, 48, 44, 44, 44, 54, 54,
49, 36, 71, 80, 82, 77, 67, 44, 49, 57,
48, 44, 0};

void loop()
{
int randomNumberSeed;
long kk;

unsigned char rr = 0;

while (randomNumberSeed = seeds[rr++]) {

randomSeed(randomNumberSeed);
  Serial.print("Seed "); Serial.print(randomNumberSeed);
  Serial.print(" 1st =  "); Serial.print(random(1, 15));

  for (unsigned char tt = 0; tt < 50; tt++) kk += random();

  Serial.print(" 51st =  "); Serial.print(random(1, 15));
  Serial.println("");
}


for (; ; );
}


HTH

Edit: I also thought of this instead of the toss 50 idea:

Code: [Select]

int kk;
do kk = random(23);
while ((kk < 1) || (kk > 15));


Which also helps.
a7



[/code]

Geek Emeritus

I get more randomness in the random number test sketch if I change

Code: [Select]
int randomNumberSeed = (Serial3.read());

to

Code: [Select]
int randomNumberSeed = (Serial3.read() & analogRead(A0));

but when I put that in the big code, I'm bak to hearing files 1 or 8 on restart
All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

aarg

Quote
when I put that in the big code
The problem is in some code you didn't post, not the code you did post?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Erik_Baas

This code gives me at least some "randomness" after each reboot, by adding all six analog inputs:

Code: [Select]

long randomNumberSeed;

void setup() {
  Serial.begin(115200);
  for (int i = A0; i <= A5; i++) {
    randomNumberSeed += analogRead(i);
  }
  randomSeed(randomNumberSeed);
  Serial.println(randomNumberSeed);
}

void loop() {}


NB.: randomNumberSeed must be declared as long!

gfvalvo

#11
Jan 24, 2021, 02:25 am Last Edit: Jan 24, 2021, 02:27 am by gfvalvo
Since you have a GPS, why not generate a random seed with the time (seconds) of the position fix?

You could even put that in the upper 8 bits of the seed and the lower part of micros() in the lower 24 bits.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Geek Emeritus

#12
Jan 24, 2021, 03:18 am Last Edit: Jan 24, 2021, 03:22 am by Geek Emeritus
as the poet said, nothings ever easy as it seems.

it started out as a doorbell. then it became an alarm too, because that's just a doorbell that rings when the button is pushed by a motion sensor. the alarm sounds are audio files, so I want confirmation that the audio works on startup. these startup announcements are mostly lines from Star Trek TOS. if you restart repeatedly,trying things out, the same audio file over and over grates on peoples nerves. ergo, 15 files so Scotty, Kirk, Spock and Checkov break up the monotony.

I can't use time data because the GPS parser has not started in setup. randomSeed( time_t (t)); doesn't work, randomSeed(now()); doesn't work.

experience has shown that if I post the whole 10k, works only on a mega, with particular I2C backpacks, program someone squawks about "do you expect us to troubleshoot this Russian novel of a program..." or somebody nitpicks "why did you do this the way you did this on line 482..." looking in the datalogger. so, no, I won't post the whole monstrosity

so I wrote a test program that proves my point, which is precisely what people here tell people to do here after they post 10k .INOs.



All PMs will be deleted unopened due to arrogant argumentative pot stirring Malfoys.
 If you have not read "How to use this forum - please read", in particular: "7. If you are posting code or error messages, use "code" tags":  expect rude responses

DVDdoug

#13
Jan 24, 2021, 04:07 am Last Edit: Jan 24, 2021, 04:16 am by DVDdoug
Just try
Code: [Select]

randomSeed(analogRead(A0));

There is usually enough noise on the floating analog input to get some variation.

And you can improve the randomness/unpredictability if you add an "antenna" to the analog input (a wire or a coil of wire several inches long, open at the other end).   

Another "trick" that you can sometimes use depending on the user interface is to run random() in a loop during setup until somebody presses a button.   Then, next time you grab a "real" random number, you are starting from an unknown/unpredictable place in the pseudo-random sequence.   If you can do that, the seed isn't so important.


Go Up