Problems Generating Random Numbers


hi guys
im trying generate 3 different, random, numbers per cycle of code don't understand why i'm getting outputs i'm getting.  mind 3 of "tests" should give me same values. instead thy r different , unusable due duplicate values.
im more interested in knowing why not working think should being told how should have written code.



anyway, questions id answered are:

1) why dose "test 1" give same value 1st & 2nd variable? 
2) why dose "test 2" give same value variable? 
3) why 1st variable in "test 2" "0"?
4) why cant combine line 15 & 16? so: "ran[r] = random(0,255);" if do  combine them, crazy numbers!

thanks help

code: [select]
int r; //couter forloop random number genereator
int ran[]={};// array containing 3 random numbers
int redbri[] = {};
int greenbri[]={};
int bluebri[]={};
int prox;

void setup() {
serial.begin(9600);
randomseed(analogread(0)); // generater random value using noise analog read pin
}

void loop() {
for(r=0; r<=2; r++){ //loop generater 3 random numbers
prox = random(0,255);// generate random number
ran[r] = prox;  // stor random number in array
}

redbri[0]    = ran[0];  // assign random numbers neopixel value array
greenbri[0]  = ran[1]; 
bluebri[0]   = ran[2];

serial.println("     test 1");
serial.println(ran[0]);         //********test 1*********
serial.println(ran[1]);         //********test 1 ********
serial.println(ran[2]);         //********test 1*********

serial.println("     test 2");
serial.println(redbri[0]);     //********test 2*********
serial.println(greenbri[0]);   //********test 2 ********
serial.println(bluebri[0]);    //********test 2*********



serial.println("     test 3"); //********test 3*********
for(r=0; r<=2; r++){           
serial.println(ran[r]);
}

serial.println(" ");
delay (1000);
}

code: [select]
int ran[]={};// array containing 3 random numbers

nope.  array containing 0 random numbers.  forgot give size.  that's important.  add overflowing , stepping on memory belongs else. 

that creates undefined behavior, correct. 


Arduino Forum > Using Arduino > Programming Questions > Problems Generating Random Numbers


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts