c++ - function returns all the same random number -
c++ - function returns all the same random number -
this question has reply here:
how generate different random numbers in loop in c++? 12 answersi have function in c++, returns random float number between 2 numbers. here code:
float randfloatnumbetween(float lower, float upper) { srand((unsigned)time(null)); homecoming lower + ((float) rand() / rand_max) * (upper - lower); }
when phone call function parameters lower 0.0 , upper 10.0, returns numbers like:
1.00503555 1.01846383 1.03463534 1.05263234 1.06461514
how can create function returns different numbers before , after comma? numbers like:
5.00503555 3.01846383 1.03463534 4.05263234 8.06461514
obviously need alter seed value each time phone call srand
. based on comments you've left doesn't appear problem, needs pointed out.
some random number generators (i know microsoft's rand
one) have correlation between initial seed , first number generated. since time doesn't alter fast, numbers seeding tend correlated well.
the best way around utilize improve random number generator such c++11 ones in <random>
header: http://en.cppreference.com/w/cpp/numeric/random or predecessors in boost library.
otherwise create sure generate couple of throw-away random numbers after seed it.
c++ random
Comments
Post a Comment