Arduino Forum

Using Arduino => Programming Questions => Topic started by: Chaso on Jun 12, 2013, 05:13 am

Title: randomseed problem
Post by: Chaso on Jun 12, 2013, 05:13 am
Am trying to "verify" a randomseed example from " arduino programming notebook" Brian W Evans.Page 28. The example code is ;


int randNumber;
int led=6 ;
void setup() {}
void loop ()
{
  randomSeed(millis());
randNumber  = random( 255);
  analogWrite(led, randNumber);
  delay(500);
}

This will not "verify", I get the error message "randNumber was not declared in this scope.
would someone like to recommend a tutorial on this subject,ie random numbers. Thanks, Chaso.
Title: Re: randomseed problem
Post by: el_supremo on Jun 12, 2013, 05:18 am
Compiles for me with Arduino 1.0.5

Pete
Title: Re: randomseed problem
Post by: Naruto128 on Jun 12, 2013, 05:19 am
????


this code if it works !!
Title: Re: randomseed problem
Post by: TanHadron on Jun 12, 2013, 05:20 am
Works fine for me as is as well.
Title: Re: randomseed problem
Post by: Chaso on Jun 12, 2013, 05:51 am
Hi Pete, Could the fact I am using  Arduino ERW 1.0.4. make any difference ?  C
Title: Re: randomseed problem
Post by: PaulS on Jun 12, 2013, 01:09 pm
randomSeed() is meant to be called once, to seed the random number generator. It is not meant to be called on every pass through loop().

So, it should be moved to setup(). At which point, you might as well omit it, since the time returned by millis() will be consistent every time you start the Arduino, meaning that you will always get the same set of random numbers.
Title: Re: randomseed problem
Post by: dannable on Jun 12, 2013, 02:52 pm
If you want a different random number sequence each time it runs then you should add the line

Code: [Select]
randomSeed(analogRead(0));

(or similar) in the setup() section. This uses a random value read from an analog pin to seed the random number generator. If you want to repeat the same sequence for testing, seed it with a fixed value.

From http://arduino.cc/en/Reference/RandomSeed (http://arduino.cc/en/Reference/RandomSeed)
Title: Re: randomseed problem
Post by: el_supremo on Jun 12, 2013, 06:46 pm
It should work with 1.0.4.

What is ERW ?

Pete
Title: Re: randomseed problem
Post by: Chaso on Jun 24, 2013, 02:12 am
Back again to solve my unsolved problem.
Have tried the two suggestions from #5 and #6, but still will not "verify". Error message " random number was not declared in this scope" ?
What randnumber ? and where to declare it ? Surely randnumber is 255 and declared in scope.
Oh,bye the way, Arduino "ERW" 1.0.4. is the title at the very top of every sketch page.
Would love to solve this randonSeed problem.  C.
Title: Re: randomseed problem
Post by: el_supremo on Jun 24, 2013, 02:21 am
You should post your problem in this thread:
http://forum.arduino.cc/index.php?topic=118440.0 (http://forum.arduino.cc/index.php?topic=118440.0)


Pete
Title: Re: randomseed problem
Post by: PaulS on Jun 24, 2013, 05:42 am
Quote
Would love to solve this randonSeed problem.

It's not a randomSeed() problem. The code in the original post compiles with Arduino 1.0.5.
Title: Re: randomseed problem
Post by: KeithRB on Jun 24, 2013, 03:40 pm
Check your spelling very carefully. Especially if you re-typed the code into the forum.
Title: Re: randomseed problem
Post by: wildbill on Jun 24, 2013, 04:46 pm

Check your spelling very carefully. Especially if you re-typed the code into the forum.


Better yet, paste your code. Without seeing it, I'll guess that you're typed the variable with a difference in upper/lower case.
Title: Re: randomseed problem
Post by: Chaso on Jun 30, 2013, 01:00 am
Hi Guys, sorry I am late getting back to you, have had problems login in to forum. Thankyou, Keith RB and wildbill, you got it right for me, I had " randomNumber",  instead of "randNumber". Thanks for your help.
                                                                                                                                                                                                                                                                                                Chaso.