C++ Random Function: Generate Uniformly Distributed Numbers

In summary, the conversation discusses the use of the rand() function in C++ and how it may not generate truly uniformly distributed random numbers in large dimensions. The suggestion is made to use a better pseudorandom number generator, such as the Mersenne twister or the boost rand library. It is emphasized that the standard rand() function should not be used for serious applications.
  • #1
omri3012
62
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 probabilty 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
  • #2
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:
  • #3
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.
 
  • #4
That may not work correctly for pseudorandom numbers, as bits in two consecutive numbers can be correlated.
 
  • #5
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
 
  • #6
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).
 
  • #7
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.
 

Related to C++ Random Function: Generate Uniformly Distributed Numbers

What is a C++ random function?

A C++ random function is a built-in function that generates a random number in a given range. It is used in programming to add an element of unpredictability to a program.

How do I use the C++ random function to generate a uniformly distributed number?

To generate a uniformly distributed number in C++, you can use the rand() function from the library. This function returns a random integer between 0 and RAND_MAX (a constant defined in ), which can then be scaled and shifted to fit your desired range.

What is the range of numbers that can be generated using the C++ random function?

The range of numbers that can be generated using the C++ random function depends on the data type used. For example, if you use the rand() function with the int data type, the range will be from 0 to RAND_MAX (typically 32767). However, if you use the long data type, the range will be much larger.

Can the C++ random function generate non-uniformly distributed numbers?

By default, the C++ random function generates uniformly distributed numbers. However, it is possible to modify the function to generate non-uniformly distributed numbers. This can be done by using different techniques, such as scaling and shifting, or using other functions like rand() and srand() in combination.

Are the numbers generated by the C++ random function truly random?

The numbers generated by the C++ random function are not truly random, as they are based on a deterministic algorithm. However, they are unpredictable and appear random enough for most practical purposes in programming.

Similar threads

  • Programming and Computer Science
Replies
1
Views
768
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
Replies
12
Views
854
  • Programming and Computer Science
Replies
31
Views
4K
  • Programming and Computer Science
Replies
1
Views
717
Back
Top