Pages: [1]   Go Down
Author Topic: randomseed problem  (Read 893 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 48
Posts: 1662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Compiles for me with Arduino 1.0.5

Pete
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 328
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

????


this code if it works !!
Logged

Offline Offline
God Member
*****
Karma: 25
Posts: 529
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Works fine for me as is as well.
Logged

NZ
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Pete, Could the fact I am using  Arduino ERW 1.0.4. make any difference ?  C
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, England, (dis)UK
Offline Offline
God Member
*****
Karma: 16
Posts: 706
Quick, chuck it in the bin before the boss finds out...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want a different random number sequence each time it runs then you should add the line

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

Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield

Offline Offline
Edison Member
*
Karma: 48
Posts: 1662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It should work with 1.0.4.

What is ERW ?

Pete
Logged

NZ
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 48
Posts: 1662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should post your problem in this thread:
http://forum.arduino.cc/index.php?topic=118440.0


Pete
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50157
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 33
Posts: 1468
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3727
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

NZ
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: