Does rand() have some kind of numerical limit? - c++

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.

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).

Related

Dice roll - random numbers are not generating correctly C++

The code below print same random numbers all the time, I think the rand() is not working properly. Please help on this code:
#include <iostream>
int main() {
for (int i=0; i < 100; i++) {
int die1 = (rand() % 6) + 1;
std::cout << "Generated random number: " << die1 << std::endl;
}
return 0;
}
A few issues (aside from your malformed main prototype which you ought to fix).
Unless you tell it otherwise, rand() is seeded with an initial value of 1. Use srand to change that. Using the system clock time is idiomatic. Then at least your output will vary.
Taking modulus 6 will introduce statistical bias unless the generator's periodicity is a multiple of 6, which is unlikely. You will notice that effect for such a small modulus. Use a division-based approach with RAND_MAX instead: rand() / (RAND_MAX / 6 + 1) is no more of an abuse of rand() than rand() itself is an abuse of uniformity!
rand() does not have particularly good statistical properties. Consider using the Mersenne Twister generator that's now part of the C++ standard library. For a casino-quality generator, you'd probably have to resort to using external hardware.
Whatever you adopt, you can always run a chi square test for uniformity against your sample, too see if it has adequate statistical properties.
There is a 16% probability that you will get the same number twice in a row. rand() always return the same sequence for the same seed. What you are seeing is not necessarily incorrect. If you remove the %, what responses do you see? Are you seeing the same numbers?
Try calling srand((unsigned) time(&t)) before your while loop and see the results. They should be different from one execution run to the next.

C++ Random Number Check for Very Small Probabilities

I have a very small probability of an event occurring (of order 1e-5) and am trying to use a uniform random number to test for success. As the probability drops to around 1e-4 the fraction of successes no longer matches the probability in the test code below.
How can I get an accurate check for success with such small probabilities? I tried using other random number generators but all the suggestions I found were for C++11 which I am not using. Many thanks in advance!
#include <cstlib>
#include <iostream>
#include <cmath>
double Prob, rand_num, frac_success;
int num_success, num_tries;
Prob = 1e-4;
num_tries = 1e8;
num_success = 0;
for (int i=0; i<num_tries; i++) {
rand_num = (double) rand() / RAND_MAX; // random number between 0 and 1
if (rand_num < Prob) {
num_success ++; // Record success
}
}
frac_success = double(num_success) / double(num_tries);
cout << Prob << endl << frac_success << endl;
The fraction of successes is roughly equal to Prob when Prob = 1e-3, but for Prob = 1e-4 it is always greater than 1.2e-4. The discrepancy gets worse with lower probabilities, and does not seem to be fixed by increasing the number of tries.
EDIT:
As said by DiJuMx and Stefano Sanfilippo it seems rand() simply isn't a good enough generator to use. I decided to change to C++11 so I could use the uniform_real_distribution which solves the problem (this meant changes to other non-C++11 code but there was happily less to change than I expected).
This sounds like your value of RAND_MAX is too small.
Consider the fact that rand() returns an integer between 0 and RAND_MAX. If you divide this number by RAND_MAX then, besides 0, the smallest number you can get is 1.0/RAND_MAX.
When RAND_MAX is 32767, the smallest value is 3e-5. Whereas, on my machine, RAND_MAX is 2147483647, so the smallest value is 4e-10.
Alternatively, look at Stefano's answer about using C++ specific libraries.
rand() is a very bad RNG, it would be ok just for implementing tic-tac-toe, but not for any serious business.
If you can't use C+11 random module, you can still take advantage of Boost.Random, which works with C++03 too. Browse the generators page and look for the best suit.
First, you have to consider that your estimator comes with a certain error. I didnt find a good link, in short it is:
H = success / trials // your estimator
E(H) = p // the expectation value is the real probability
Var(H) = p(1-p)/n // variance of your estimator
This alone would suggest that you should get better results for smaller probabilities.
However, as suggested in the other answers, you should use a proper random number generator.
The rng should produces each possible outcome with same probablity (if its uniform). Lets say for a moment RAND_MAX=3. If we run it often enough, each possible value will occur with the same frequency and we get the same result as if we used each value only once. Now consider
for (int i=0;i<4;i++){std::cout << (double)i/3 << std::endl;}
this will produce
0
0.333333
0.666667
1
This will give reasonable results for not too small probabilities (eg. when trying to find p=0.5 by chance you can find the exact value). However, when you try to find a small probability, the result will be too big.
The effect is the same when RAND_MAX=32767, it just shows up for smaller probabilities (somewhere around p < 1/RAND_MAX). Actually I dont know if this can be fixed by simply dividing by RAND_MAX+1 but here is a video that quite nicely dramatizes and explains the problems with rand().
If you suspect your random number generator has a bias you can check it by running it a large number of times and generating the frequency distribution.
Try it with different seeds and see if the bias holds.
If it does have a steady bias, record the distribution and use this to overcome the bias.

Improving the quality of random number generation in Qt 5.3

I am currently implementing a random number generator in Qt5.3 as part of genetic algorithm experiments. I have tried several methods but the best seems to be:
// Seed the random generator with current time
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
And then this funtion to generate the random numbers:
int MainWindow::getRandomNo(int low, int high)
{
return qrand() % ((high + 1) - low) + low;
}
Because of the nature of these experiments, the random nature of these numbers is important. Is there a way to improve the quality of the random number samples? Upon statistical analysis, the Qt random number generator exhibits typical patterns that are found in older systems of random number generation.
The method used above relies on the current time as a seed for the number generator. Is there a way to improve the seed so that the random sequences are less prone to patterns? I would be extremely grateful for any help.
Use MT.
You can get an implementation here:
http://www.agner.org/random/
http://de.wikipedia.org/wiki/Mersenne-Twister
boost_random
I ran into the same problem years ago in a delphi software, and switching to MT sovled my problem. But check the list in the boost docu for further detailed information about the differences between RNG algorithms.
Adjusting your seed won't really effect the quality of the numbers generated, just the particular order of the numbers generated. You will need to use a better algorithm to generate your random numbers.
In addition, the way you are using the generated numbers is slightly biased. With your getRandomNo function, there will be a slight bias towards smaller numbers. For example if qrand returns a value in the range 0..2^32-1, and you have low=0 and high=2^32-2, then using % as you do, will mean that 0 will be returned (approximately) twice as often as any other number.
An improvement would be to try something like this:
Let n be a positive integer where you want a random integer in the range 0..n-1, let m be the smallest power of 2 greater than or equal to n.
unsigned int myrand( unsigned int n, unsigned int m )
{
unsigned int i = qrand() % m; /* or (qrand() & (m-1)) */
while ( i >= n )
{
i = qrand() % m;
}
return i;
}
This will be slower, but the expected number of iterations is 2. Also, if you are using the same range multiple times, you can pre-compute m.
an amendment to dohashi's answer can be to take m as the first prime number greater than or equal to n

Generate uniform random number in open interval

I cannot find a way to generate random number from uniform distribution in an open interval like (0,1).
(double)rand()/RAND_MAX;
will this include 0 and 1? If yes, what is the correct way to generate random number in an open interval?
Take a look at std::uniform_real_distribution! You can use a more professional pseudo random number generator than the bulit-in of <cstdlib> called std::rand(). Here's a code example that print outs 10 random numbers in range [0,1):
#include <iostream>
#include <random>
int main()
{
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(0.0,1.0);
for (int i=0; i<10; ++i)
std::cout << distribution(generator) << endl;
return 0;
}
It is very unlikely to get exactly zero. If it is very important for you to not to get 0, you can check for it and generate another number.
And of course you can use random number engine specified, as std::mt19937(that is "very" random) or one of the fastest, the std::knuth_b.
I haven't written C++ in ages but try the following code:
double M = 0.00001, N = 0.99999;
double rNumber = M + rand() / (RAND_MAX / (N - M + 1) + 1);
I haven't programmed in C++ for a number of years now, but when I did the implementation of rand was compiler specific. Implementations varied as to whether they covered [0,RAND_MAX], [0,RAND_MAX), (0,RAND_MAX], or (0,RAND_MAX). That may have changed, and I'm sure somebody will chime in if it has.
Assume that the implementation is over the closed interval [0,RAND_MAX], then (double)(rand()+1)/(RAND_MAX+2); should yield an open interval U(0,1) unless RAND_MAX is pushing up against the word size, in which case cast to long. Adjust the additive constants if your generator covers the range differently.
An even better solution would be to ditch rand and use something like the Mersenne Twister from the Boost libraries. MT has different calls which explicitly give you control over the open/closed range of the results.
Given uniform distribution of a RNG with closed interval [a, b], the easiest method is to simply discard unwanted values an throw the dice again. This is both numerically stable and practically the fastest method to maintain uniformity.
double myRnD()
{
double a = 0.0;
while (a == 0.0 || a == 1.0) a = (double)rand() * (1.0 / (double)RAND_MAX);
return a;
}
(Disclaimer: RAND_MAX would have to be a power of two and < 2^52)

rand() gives still the same value

I noticed that while practicing by doing a simple console-based quiz app. When I'm using rand() it gives me the same value several times in a row. The smaller number range, the bigger the problem is.
For example
for (i=0; i<10; i++) {
x = rand() % 20 + 1;
cout << x << ", ";
}
Will give me 1, 1, 1, 2, 1, 1, 1, 1, 14, - there are definetely too much ones, right? I usually got from none to 4 odd numbers (rest is just the same, it can also be 11, 11, 11, 4, 11 ...)
Am I doing something wrong? Or rand() is not so random that I thought it is?
(Or is it just some habit from C#/Java that I'm not aware of? It happens a lot to me, too...)
If I run that code a couple of times, I get different output. Sure, not as varied as I'd like, but seemingly not deterministic (although of course it is, since rand() only gives pseudo-random numbers...).
However, the way you treat your numbers isn't going to give you a uniform distribution over [1,20], which I guess is what you expect. To achieve that is rather more complicated, but in no way impossible. For an example, take a look at the documentation for <random> at cplusplus.com - at the bottom there's a showcase program that generates a uniform distribution over [0,1). To get that to [1,20), you simply change the input parameters to the generator - it can give you a uniform distribution over any range you like.
I did a quick test, and called rand() one million times. As you can see in the output below, even at very large sample sizes, there are some nonuniformities in the distribution. As the number of samples goes to infinity, the line will (probably) flatten out, using something like rand() % 20 + 1 gives you a distribution that takes very long time to do so. If you take something else (like the example above) your chances are better at achieving a uniform distribution even for quite small sample sizes.
Edit:
I see several others posting about using srand() to seed the random number generator before using it. This is good advice, but it won't solve your problem in this case. I repeat: seeding is not the problem in this case.
Seeds are mainly used to control the reproducibility of the output of your program. If you seed your random number with a constant value (e.g. 0), the program will give the same output every time, which is useful for testing that everything works the way it should. By seeding with something non-constant (the current time is a popular choice) you ensure that the results vary between different runs of the program.
Not calling srand() at all is the same as calling srand(1), by the C++ standard. Thus, you'll get the same results every time you run the program, but you'll have a perfectly valid series of pseudo-random numbers within each run.
Sounds like you're hitting modulo bias.
Scaling your random numbers to a range by using % is not a good idea. It's just about passable if your reducing it to a range that is a power of 2, but still pretty poor. It is primarily influenced by the smaller bits which are frequently less random with many algorithms (and rand() in particular), and it contracts to the smaller range in a non-uniform fashion because the range your reducing to will not equally divide the range of your random number generator. To reduce the range you should be using a division and loop, like so:
// generate a number from 0 to range-1
int divisor = MAX_RAND/(range+1);
int result;
do
{
result = rand()/divisor;
} while (result >= range);
This is not as inefficient as it looks because the loop is nearly always passed through only once. Also if you're ever going to use your generator for numbers that approach MAX_RAND you'll need a more complex equation for divisor which I can't remember off-hand.
Also, rand() is a very poor random number generator, consider using something like a Mersenne Twister if you care about the quality of your results.
You need to call srand() first and give it the time for parameter for better pseudorandom values.
Example:
#include <iostream>
#include <string>
#include <vector>
#include "stdlib.h"
#include "time.h"
using namespace std;
int main()
{
srand(time(0));
int x,i;
for (i=0; i<10; i++) {
x = rand() % 20 + 1;
cout << x << ", ";
}
system("pause");
return 0;
}
If you don't want any of the generated numbers to repeat and memory isn't a concern you can use a vector of ints, shuffle it randomly and then get the values of the first N ints.
Example:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//Get 5 random numbers between 1 and 20
vector<int> v;
for(int i=1; i<=20; i++)
v.push_back(i);
random_shuffle(v.begin(),v.end());
for(int i=0; i<5; i++)
cout << v[i] << endl;
system("pause");
return 0;
}
The likely problems are that you are using the same "random" numbers each time and that any int mod 1 is zero. In other words (myInt % 1 == 0) is always true. Instead of %1, use % theBiggestNumberDesired.
Also, seed your random numbers with srand. Use a constant seed to verify that you are getting good results. Then change the seed to make sure you are still getting good results. Then use a more random seed like the clock to teat further. Release with the random seed.