random - Why C++ std::rand() has pattern? -


all

i coming simple question. please read following codes first

int main() {     vector<double> arr1, arrr2;     arr1.resize(120);     arr2.resize(120);     for(int = 0; < 120; i++)     {         double d1 = rand() % 100;         d1 = d1/double(100);         arr1[i] =d1;          double d2 = rand() % 100;         d2 = d2/double(100);         arr2[i] =d2;      }     return 0; } 

in above code, generate 2 random arrays (without seed). saved these arrays in file , plot them below:

enter image description here image, found these 2 random series have similar trend in changing number. e.g. these 2 series go , down (x=57) (x=60), , (x=82) (x=86), , etc..

may know if mislead visualization, or there correlation exist?

many in advance.

best wishes

long

you doing number of things wrong.

the number 1 thing doing wrong use rand(). there plenty of excellent random number generators out there. rand() oftentimes doesn't fall in "good" category.

the next thing doing wrong converting integer result rand() double via

double d1 = rand() % 100; d1 = d1/double(100); 

this discards high level bits result of rand(). random number generators can have problems low order bits. bad random number generators such typical implementations of rand() guarantee case. instead divide largest possible result rand(), rand_max (cast double).

the next thing doing wrong using same pseudorandom sequence generate 2 supposedly uncorrelated random number sequences. it's better use 2 different generators this. can't rand(). use better prng.

the final thing doing wrong seeing pattern none may exist. need use more powerful techniques such statistical correlation eyes. or use better prng, 1 has been tested in kinds of ways ensure results satisfy various tests of "randomness".


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -