probability - Random Number Cheat? -



probability - Random Number Cheat? -

for purpose of stochastic simulation, next algorithm suffice produce 1 1000000 pseudorandom decimal numbers of same quality simple rand() command you'd find in computer languages? premise of algorithm utilize 10 quality decimal pseudorandoms , expand them 1 1000000 quality decimal pseudorands.

please note next algorithm , not real code.

double rands[10] = {rand()}; /// initialize vector of 10 quality pseudorands [0,1] double expandedrands[1000000] = {0}; /// initialize vector of size 1 1000000 for(int = 0; < 10; i++) { for(double j = 0; j < 100000; j++) /// j goes 0 1 hundred one thousand { expandedrands[(100000 * i) + j] = rands[i] * abs((j - 0.5)/ 1000000); } }

edit: realize human beingness @ numbers generated algorithm , know follow pattern, real question stochastic simulation work same way if fed these numbers rather 1 1000000 rand() numbers.

your algorithm not generate uniform distribution.

expandedrands[(100000 * i) + j] = rands[i] * (j / 100000);

first, each initial random value 𝑖 generate 100,000 values in range [0,𝑖). skews distribution toward lower values.

furthermore, every value in final info generated 1 of initial 10 values, , evenly spaced. leaks quite bit of info observers , means they'll able guess more values in final array pretty likelihood of making right guesses.

presumably need stretch 10 calls rand() 1,000,000 quality random numbers because rand() slow (and generates random info in return). under these circumstances utilize results of rand() nil more seed good, deterministic prng.

some code, including c++ facilities implementing idea:

// initialize vector of 10 quality pseudorands [0,rand_max] int rands[10]; for(int = 0; < 10; ++i) { rands[i] = rand(); } std::seed_seq seeds(begin(rands), end(rands)); // seed_seq c++ , performs standard rng 'warm-up' sequence // in other languages you'll implement warm-up sequence yourself. std::mt19937 eng(seeds); // mt19937 implementation of standard rng. // seed_seq ensures initial state producing random bits // can utilize whatever standard prng algorithm meets quality/performance/size needs // example, if need faster , smaller state utilize linear congruential engine such minstd_rand0 std::uniform_real_distribution<double> dist(0.0, 1.0); // c++ object takes random bits , produces random values distribution. // there many different algorithms doing double expandedrands[1_000_000]; for(int = 0; < 1_000_000; ++j) { expandedrands[i] = dist(eng); }

expandedrands contains 1 1000000 values uniformly distributed in range [0.0, 1.0). given same initial 10 random values same 1000000 output values, , difference in input should produce quite different output.

if you're stretching rand()'s results because need more parallelizable serialized calls rand(), can utilize 10 rand() calls generate seed sequence, , utilize seed several independent prng engines run on different cores or in independent instances of gpgpu kernel (if can implement prng , distribution in cuda or whatever).

int rands[10]; (int = 0; < 10; ++i) { rands[i] = rand(); } std::seed_seq seeds(begin(rands), end(rands)); std::mt19937 eng[10]; (int = 0; < 10; ++i) { eng.seed(seeds); } // engines can used on independent threads.

p.s. know code pseudo-code, i've seen error in c fair bit, in case wrote code way due same misconception c:

double rands[10] = {rand()};

the initializer in c not execute look 10 times , initialize each element different value. happens in c that, when there fewer initializers elements in array, initializers there assigned corresponding elements (first initializer first element, sec initializer sec element, etc.) , rest of elements 0 initialized. example:

int x[10] = {0};

will initialize whole array zeros, but:

int x[10] = {1};

will initialize first element 1 , rest zero.

random probability random-sample stochastic

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -