c++ - How to generate random numbers in a range? -
i'm studying arrays , can't figure out i'm doing wrong.
i need output array 8 random generated numbers between 5 , 25.
before down voting question : /, tried looking similar questions on stackoverflow of them contain use of algorithm's or different kinds of sort-techniques. cannot use technique's in answer. 'should' easier solve this.
where problem in code , why doesn't random number generate new number while i'm looping trough array?
int table[8]; int random_number= rand() % 25 + 5; (int i=0; i<8; i++) { table[i] = random_number; cout << table[i] << " "; }
as compile , run it, gives me 8 times same number, i'm letting array loop through each single index, while putting random number in it? on paper should work normally, right?
is there can explain did wrong , why loop not working correctly?
in c++11, random
facilities may do
std::default_random_engine engine; // or other engine std::mt19937 int table[8]; std::uniform_int_distribution<int> distr(5, 25); std::generate(std::begin(table), std::end(table), [&](){ return distr(engine); });
Comments
Post a Comment