-1

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

273K
  • 29,503
  • 10
  • 41
  • 64
  • 5
    Why, in the year of our Lord two thousand twenty two, are you using `rand()` in your C++ code? – sweenish Nov 30 '22 at 03:45
  • Just want to know the reason behind this behaviour of different RAND_MAX ranges in different systems – Kritik Manral Nov 30 '22 at 03:49
  • 2
    The fact that it can differ should be enough. Without looking at the standard, a very educated guess is that it's implementation defined. There isn't a further explanation required. – sweenish Nov 30 '22 at 03:54
  • 2
    [RAND_MAX](https://en.cppreference.com/w/cpp/numeric/random/RAND_MAX) – Eljay Nov 30 '22 at 03:57
  • 2
    "So is there some way to change this value". `RAND_MAX` is not a configuration setting. It tells you what already happened (in the implementation of `rand()`). It's like saying, "the ingredients panel of this snack says it has 200 calories. How can I change it to 100 calories?" – Raymond Chen Nov 30 '22 at 03:59
  • "but due to some time constraints while online contest" -- if some contest forces you to write this sort of code, it might be wise to avoid it. You're likely to just learn bad habits. – Dan Mašek Nov 30 '22 at 04:02

2 Answers2

2

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.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Yup I always use the random() for generating the random numbers but due to some time constraints while online contest I tried using this code then noticed this behaviour. So just need to know the reason behind this. – Kritik Manral Nov 30 '22 at 03:52
  • 2
    It's debatable which is worse, `rand()` or [`randu`](https://en.wikiquote.org/wiki/RANDU). Knuth felt pretty strongly about the latter, he described it as "[truly horrible"](https://en.wikipedia.org/wiki/RANDU). – pjs Nov 30 '22 at 03:57
  • I disagree with the premise of "Don't use `rand()`. Ever.". I certainly wouldn't use it production code where I care about properties (distribution, etc) of the random numbers produced. But `rand()` is easy to set up and use, and much more convenient than alternatives (for example, when producing a small number of random values to test other code, and the actual inputs to that other code don't matter). – Peter Nov 30 '22 at 05:30
  • I'm missing how `rand()` is much more convenient, or even slightly more convenient than `random()`. Is it that there are two fewer characters to type? – Mark Adler Nov 30 '22 at 05:41
  • @MarkAdler -- `rand()` is much more convenient because when I call it in my code it compiles. If I call `random()` it doesn't. – Pete Becker Nov 30 '22 at 14:27
0

rand() is a very old function, going back to the earliest days of C. In those early days, an INT_MAX of 32k was common and well justified. Surely it's easy to see that RAND_MAX > INT_MAX doesn't make any sense.

As for why some compilers have updated their RAND_MAX in the intervening years and some have not, I would guess that it's down to a commitment to backwards compatibility. I know that I've been personally bitten by the 32k limit in Microsoft's compiler. But Microsoft has long been a champion of backwards compatibility, so I'll forgive them on this one.

Since C++11 there's a bunch of new random number functions that have been introduced. They're better than the old rand() in almost every way, except perhaps for ease of use.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622