Home > OS >  Why in CPP in some system the RAND_MAX is set to 32K while in others it is 2147483647
Why in CPP in some system the RAND_MAX is set to 32K while in others it is 2147483647

Time:11-30

In my CPP system whenever I generate a random number using rand() I always get a value between 0-32k while in some online videos and codes it is generating a value between 0-INT_MAX. I know it is dependent on RAND_MAX. So it there some way to change this value such that generated random number are of the range 0-INT_MAX Thanks in advance for the help :)

#include<bits/stdc  .h>
using namespace std;

int main(){
    srand(time(NULL));
    for(int i=1;i<=10;i  ){
        cout << rand() << endl;
    }
}

I used this code and the random number generated are 5594 27457 5076 5621 31096 14572 1415 25601 3110 22442

While the same code on online compiler gives 928364519 654230200 161024542 1580424748 35757021 1053491036 1968560769 1149314029 524600584 2043083516

CodePudding user response:

Yes. Don't use rand(). There are many reasons to not use rand(), not the least of which is that it is one of the two worst random number generators ever widely distributed. (The other is RANDU.) Don't use rand(). Ever.

Look for random() and arc4random() on your system. If you don't find those, then use the PCG source code here.

  • Related