简体   繁体   中英

C++ - rand() function not working, numbers do not go above 32,000

I am trying to create random numbers from 0 to around 200,000, however the values do not go above 33,000. Even if I try a different value they still do not go above 33k. I tried to seed and still nothing. Did I miss something when I learned about this function? Thanks.

srand(time(0));
for (int count = 190000; count < 200000; count++)
    cout << rand() % 200001 << " ";

Make your range of values independent of the range of values rand() returns.

int getMyRandomNumber()
{
    static int MY_RANGE_MAX = 200001;
    return (1.0*rand()/RAND_MAX)*MY_RANGE_MAX;
}

and use it as:

for (int count = 190000; count < 200000; count++)
    cout << getMyRandomNumber() << " ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM