i want generate 2-d array of zeroes , ones 25% zeroes , 75% ones.
i know using rand()%2 function how limit zeroes 25 percent of array?
you initialize every element of array following:
int zeroor1() { // generates static std::default_random_engine gen; static std::uniform_int_distribution<int> dist(0,3); return (dist(gen) % 4) >= 1; } because dist(gen) % 4 produces 1 of [0, 1, 2, 3] equally likely, (dist(gen) % 4) >= 1 evaluate true 75% of time , false 25% of time.
note: doesn't guarantee perfect 25/75 distribution other answers, works array sizes. i'd need more information application decide if enough or not suitable.
Comments
Post a Comment