0

If I try to do something like:

srand(time(NULL));
for(int i = 0; i < 10000; i++){
   float x = rand() % 1000000000000;
   output_file << x << endl;
}

I seem to only get numbers for x that are less than 100000. Does rand() have some kind of limit that prohibits it from exceeding this amount? Is there some way around this (specifically for what I'm trying to rand() in the code above)?

EDIT: Just realized the limit is set by RAND_MAX. Still looking for a way around this.

  • 3
    Yes, `RAND_MAX` and ``. – chris Jan 06 '14 at 01:33
  • @chris Thanks, just saw it in the STL definition. – user3150601 Jan 06 '14 at 01:34
  • 2
    If C++11 is an option then using [std::uniform_real_distribution](http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution) from the random header should help. – Shafik Yaghmour Jan 06 '14 at 01:35
  • 2
    The way around it is to use something other than `rand()`. – jwodder Jan 06 '14 at 01:35
  • 2
    `RAND_MAX` doesn't *set* the limit, it tells you what it is. And for the record, if `int` is 32 bits on your platform (which it is on every platform I've used), 1,000,000,000,000 is way too big of a number for it (the max on the average platform is 2,147,483,647). – Cornstalks Jan 06 '14 at 01:39
  • You could use a different random number generator, or you could bitshift multiple `rand()`s and XOR them together: `int biggerRand = rand() ^ (rand() << 8) ^ (rand() << 16) ^ (rand() << 24);`. This is just a passing thought, so I don't guarantee that it's without flaws. – Cornstalks Jan 06 '14 at 01:43
  • @Cornstalks I'm using float. – user3150601 Jan 06 '14 at 01:47
  • @user3150601: then cast it to float? Or were you talking about something else? – Cornstalks Jan 06 '14 at 01:47
  • @Cornstalks sorry, I was referring to your first comment that said 10^12 was too big for an int. I'm going to try your solution now. – user3150601 Jan 06 '14 at 01:49
  • 1
    @user3150601: But it does matter, seeing as `rand()` returns an `int`. It doesn't matter that you're assigning it to a `float`, you're modding an `int` with 10^12, which is... puzzling. – Cornstalks Jan 06 '14 at 01:51

1 Answers1

2

It's not SET by RAND_MAX, that's just there so you know what it is. The rand generator can't be adjusted.

If you need a random number generator with a wider range, you're going to have to find it elsewhere. Among other things, the boost libraries do have a 'Random' component, which might prove helpful (I have not looked at it).

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
  • So what prohibits the random generator from exceeding the amount RAND_MAX? Why is it built this way? – user3150601 Jan 06 '14 at 01:37
  • 2
    @user3150601 It's built to be fast, reliable and portable. All 3 of those considerations mean limitations. STL and stdlibc were never built to provide a solution for every problem, just to solve most 'standard' issues. For special problems, get special solutions, and your problem is most certainly special. – Niels Keurentjes Jan 06 '14 at 01:38
  • I'd guess because of limits of the data in whatever datatype it returns – BWG Jan 06 '14 at 01:49
  • @user3150601: Well there has to be _some_ limit. – Lightness Races in Orbit Jan 06 '14 at 02:00
  • Algorithms for rand() are discussed in [this question](http://stackoverflow.com/questions/1026327/what-common-algorithms-are-used-for-cs-rand) – Michael Kohne Jan 06 '14 at 02:16
  • @user3150601: Legacy, mostly. Niels's answer is rather wrong. The implementation is NOT portable ( `RAND_MAX` varies across platforms), it's not reliable (in particular, many implementations are outright bad, using an LCG in which the lower bits are hardly random at all). It's usually quite fast, yes, since it's not that random. And you don't need a special solution, there's a newer but completely standard `` header. – MSalters Jan 06 '14 at 09:18