C++ Random Function: Generate Uniformly Distributed Numbers

  • Context: C/C++ 
  • Thread starter Thread starter omri3012
  • Start date Start date
  • Tags Tags
    C++ Function Random
Click For Summary

Discussion Overview

The discussion revolves around generating uniformly distributed random numbers in C++, particularly in large ranges. Participants explore various methods and libraries to improve upon the standard rand() function, which is noted for its limitations in generating high-quality random numbers.

Discussion Character

  • Technical explanation, Debate/contested, Exploratory

Main Points Raised

  • Omri expresses a need for a random number function that can generate uniformly distributed numbers over a large span, specifically mentioning the limitations of rand().
  • One participant notes that rand() generates numbers within a limited range (0 to MAX_RAND) and suggests searching for better pseudorandom number generator libraries.
  • Another participant proposes dividing the range into zones and using rand() to select a zone, followed by generating a number within that zone, although this method is questioned for its effectiveness.
  • A later reply cautions that using zones may not work correctly due to potential correlations between consecutive pseudorandom numbers.
  • One participant suggests that calling rand() twice and combining the results could be an alternative approach, while also recommending the Boost library for better random number generation.
  • Another participant strongly criticizes the standard rand() function, recommending the Mersenne twister generator as a superior option, with links to implementations available.
  • One participant emphasizes that boost::random is a robust choice for serious applications, although it requires some initial learning to use effectively.

Areas of Agreement / Disagreement

Participants generally agree on the inadequacies of the standard rand() function and suggest alternative methods and libraries. However, there is no consensus on the best approach, with multiple competing views on how to generate random numbers effectively.

Contextual Notes

Some limitations include the dependence on the quality of the random number generator used and the potential issues with correlation in pseudorandom sequences. The discussion does not resolve the effectiveness of the proposed methods.

omri3012
Messages
60
Reaction score
0
Hallo,

I'm looking for a Random function in c++. i tried to use in the function rand()% but
it does not generate a truly uniformly distributed random number in the span (since my span is large. 100000*100000). in other word it dosent generate a random number in equal probability in lage dimentions.
if you know a function or a way that i could generate random number in large span it would
be very helpful.

Thanks,

Omri
 
Technology news on Phys.org
That's because rand() generates numbers from 0..MAX_RAND range (and I think MAX_RAND is by default 32767). I remember seeing libraries for better pseudorandom number generators, shouldn't be difficult to google.
 
Last edited:
you could divide your range into 32767 zones and use rand() first to get a zone, then again to get a number within the zone. etc.
 
That may not work correctly for pseudorandom numbers, as bits in two consecutive numbers can be correlated.
 
harborsparrow said:
you could divide your range into 32767 zones and use rand() first to get a zone, then again to get a number within the zone. etc.
Which is roughly equivalent to calling rand() twice, multiplying the first one by RAND_MAX and then adding them.

If you need super random numbers you might want to look at a better rand library such as boost rand
Otherwise calling rand twice and shift+add might be ok
 
The standard rand() function is really bad and you'll run into problems very quickly (even for non-scientific applications like games).

An easy solution is to use a Mersenne twister generator instead. (There are some ready to use implementations linked at the bottom of the wiki page).
 
rand() is terrible and should not be used for anything even halfway serious. If you need a robust, fast, and accurate C++ random number generator you don't need to look any further than boost::random. You'll need to spend half an hour reading the docs in order to use it, but once you do it's a snap.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
22
Views
5K
  • · Replies 31 ·
2
Replies
31
Views
5K
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K