Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Random number generator generating low numbers more frequently than high numbers C++

So, I have made a program that simulates things and in it I noticed that the c++ function rand() seemed to generate low numbers too often, so I tried to test it.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <time.h>
#include <cfloat>
#include <iomanip> 

using namespace std;

int main(){
    srand(time(NULL));
   
    int qwerty=0;
    for(int i=0; i<10000000;i++){
        if(rand()%10000<2800){
            qwerty++;
        }
    }
    cout << qwerty << endl;
    return 0;
}

If I ran the file with this "for tester" in it I would get consistently a number near 3400000, or 34%, which is near to the 34% I had seen appear inside my real program, the problem is that the output should be near 2800000 or 28%.

I then tried to run this "for tester" on a new project(the same I wrote here) where only the libraries and the srand(time(NULL)) were present, same output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I then tried to copy this file inside an online compiler, this time instead of 3400000 I got the correct number 2800000.

I can’t find why this is happening, anyone who knows?

Additional info:
I am using dev-c++ as a IDE with the TDM-GCC 4.9.2 64bit release and the ISO C++11, If I take the executable generated by my computer and run it in another one I get the same 34% result, Windows 10 is the operating system. This problem happens also if I use different numbers.

>Solution :

For a uniformly distributed random variable E in the open interval [0, 32767]
the probability of mod(E, 10000) < 2800 is around 34%. Intuitively you can think of mod(E, 10000) < 2800 as favouring the bucket of numbers in the range [30000, 32767]: that bucket modulo 10000 is always less than 2800. So that has the effect of pushing the result above 28%.

That’s the behavior you are observing here.

It’s not a function of the quality of the random generator, although you would get better results if you were to use a uniform generator with a larger periodicity. Using rand() out of your C++ standard library is ill-advised as the standard is too relaxed about the function requirements for it to be portable. <random> from C++11 will cause you far less trouble: you’d be able to avoid explicit % too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading