简体   繁体   中英

Why am I getting the same answer from random numbers?

Hi I've written a c++ program that uses the rand() function for random numbers and is giving me odd results. There's a lot more in the actual code but the basic idea of the code is similar to:

int main()
{
    srand(time(NULL));
    priority_queue<long long int> pQueueInts;
    const int TRIALS = 1000000;
    for (int i = 0; i < TRIALS; i++)
    {
        long long int holder = randomNumber();
        pQueueInts.push(holder);
    }

    cout << pQueueInts.top();
    for (int i = 0; i < TRIALS / 2; i++)
        pQueueInts.pop();

    cout << pQueueInts.top();

    for (int i = 0; i < (TRIALS / 2) - 1; i++)
        pQueueInts.pop();

    cout << pQueueInts.top();
}

long long int randomNumber()
{
    bool found = false;
    long long int counter = 0;
    while (found == false)
    {
        int roll = rand() % 500;
        if (roll == 1)
        {
            roll = rand() % 500;
            if (roll == 2)
                found = true;
        }
        counter++;
    }

    found = false;

    while (found == false)
    {
        int roll = rand() % 100;
        if (roll == 3)
        {
            roll = rand() % 250;
            if (roll == 4)
                found = true;
        }
        counter++;
    }

    found = false;

    while (found == false)
    {
        int roll = rand() % 530;
        if (roll == 5)
        {
            roll = rand() % 400;
            if (roll == 6)
                found = true;
        }
        counter++;
    }

    return counter / 3;
}

It runs and executes fine but if I say do 1M trials and find the largest counter result from that, then I exit the program and run it again but with 1000 trials, you would expect the largest counter from that sample to be smaller than the 1M trials, yet its the exact same number. If I then change the sample to 10M I might get a new high number, turn the program off again and rerun with 1000 trials and I may get the new high number from the 10M sample as the high for 1000 sample exactly which is insanely unlikely especially with my actual code where the result has much more variance.

I assume there's something going on with the rand() function or the c++ language that I don't understand that's causing this? Thanks for any enlightenment.

I'm not a statistician, so my calculation could be wrong, but the general idea should be right.

First, let's look at your randomNumber() function, and ignore how rand() generate numbers for now.

The first while loop, have a chance of 1/250000 going through. Which means, by average you have called rand() about 250000 times. (Notice again, I'm not that good with math probabilities).

Using the same logic, after doing the entire randomNumber() , you would have called rand() 487000 times. And if you have done randomNumber() 1000 times, you would have called rand() 487,000,000 times.

Now let's look at rand() . Based on couple sources:Rand Implementation OEIS - A096558 std::rand - cppreference Continuous call of rand() would actually generate a non-repeating list of 2^32 or RAND_MAX length. And it doesn't matter the seed you had, or srand(time(NULL)) , it will just be a rotated version of the same list.

Since the main function you have is basically just getting the max result of your randomNumber() , with 4*10^8 / 2^32, you would basically get the same 10 times. And with larger TRIALS , you would essentially getting the same answer even more time. And at a certain time, your answer will always be the same.

I don't have infinite power to run it, but here's what I got for running TRIAL = 10000 20 times:

958495, 141042, 2497
958495, 141042, 2497
958495, 141042, 2497
958495, 140987, 2497
958495, 140924, 2497
958495, 140924, 2497
958495, 140924, 2497
958495, 140911, 2497
958495, 140911, 2497
958495, 140911, 2497
958495, 140906, 2497
958495, 140796, 2497
958495, 140650, 2497
958495, 140650, 2497
958495, 140621, 2497
958495, 140586, 2497
958495, 140586, 2497
958495, 140586, 2497
958495, 140375, 2497
958495, 140271, 2497

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM