Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   better randomization? (http://www.programmingforums.org/showthread.php?t=15579)

nnxion Apr 9th, 2008 5:22 PM

better randomization?
 
I wasn't sure if the randomization is decent on this. It seems to be around the same percentage every time. Which could mean that the theory is proving true, but I'm not quite convinced just yet. What do you guys think?

:

/*  ____________________________________________________
  ( Monty Hall Dilemma by Ruben/nnxion - Rubenisme.com )
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯  */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int chooseprize()
{
        int prize;
        prize = rand() % 3;
        return prize;
}

int chooserandom()
{
        int chosen;
        srand(time(NULL) + rand() % time(NULL));
        chosen = rand() % 3;
        return chosen;
}

int showbad(int prize)
{
        int n, bad;
        srand(time(NULL) + rand() % time(NULL));
        n = rand() % 2;

        switch(prize)
        {
                case 0: if(n == 0) bad = 1; else bad = 2; break;
                case 1: if(n == 0) bad = 0; else bad = 2; break;
                case 2: if(n == 0) bad = 0; else bad = 1; break;
        }

        return bad;
}

int main()
{

        int prize, chosen, bad, tried, tries, counter = 0;
        float percentage = 0.00;

        /* set number of tries */
        tries = 5000;

        /* take initial random seed */
        srand(time(NULL));

        for(tried = 0; tried < tries; tried++)
        {
                /* choose a door where the prize is behind */
                prize = chooseprize();

                /* choose a random door */
                chosen = chooserandom();

                /* show ANOTHER door with no prize */
                bad = showbad(prize);

                /* raise counter if it would have been good to change */
                if(chosen == bad || chosen != prize) counter++;
        }

        percentage = ((float)counter/(float)tries) * 100;
        printf("The number of times it would have been beneficial to change: %d\n", counter);
        printf("The number of tries = %d, the percentage is %.2f\n\n", tries, percentage);

}


Fall Back Son Apr 9th, 2008 5:43 PM

Re: better randomization?
 
You're confused by the logic of the project itself not by the program. For the Monty Hall problem, the percentage of the time it's beneficial to change choices is supposed to be 66% (I think). So if you're running a test an infinite numbers of times, the percentage should always come out to the same thing, theoretically. Statistically, you SHOULD be getting around the same percentage every time you run your program. There's nothing wrong with your results. The randomization is fine (for this project).

If you increase tries to 10,000 and run it a few times, you should find that the expected results are even closer together. Then run it on 20,000 tries. Etc. The results will be closer and closer together.

Sane Apr 9th, 2008 6:03 PM

Re: better randomization?
 
No need to seed the random number generator every time you call your functions.
This consistently shows 66% for me.

I replaced your chooserandom() function with your chooseprize() function.

By the way, chosen == bad is a redundant expression. Chosen will never be bad, since your showbad() function asserts that prize != bad.

:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int chooseprize()
{
    return rand() % 3;
}

int showbad(int prize)
{
    int n, bad;
    n = rand() % 2;
   
    switch(prize)
    {
            case 0: if(n == 0) bad = 1; else bad = 2; break;
            case 1: if(n == 0) bad = 0; else bad = 2; break;
            case 2: if(n == 0) bad = 0; else bad = 1; break;
    }
   
    return bad;
}

int main()
{

    int prize, chosen, bad, tried, tries, counter = 0;
    float percentage = 0.00;
   
    /* set number of tries */
    tries = 100000;
   
    /* take initial random seed */
    srand(time(NULL));
   
    for(tried = 0; tried < tries; tried++)
    {
            /* choose a door where the prize is behind */
            prize = chooseprize();
   
            /* choose a random door */
            chosen = chooseprize();
   
            /* show ANOTHER door with no prize */
            bad = showbad(prize);
   
            /* raise counter if it would have been good to change */
            if(chosen == bad || chosen != prize) counter++;
    }
   
    percentage = ((float)counter/(float)tries) * 100;
    printf("The number of times it would have been beneficial to change: %d\n", counter);
    printf("The number of tries = %d, the percentage is %.2f\n\n", tries, percentage);

    return 0;
   
}


nnxion Apr 9th, 2008 9:57 PM

Re: better randomization?
 
Quote:

Originally Posted by Sane (Post 143695)
No need to seed the random number generator every time you call your functions.

This is true, but I wanted to have more reliable randomness, which I thought would be achieved this way.

Quote:

Originally Posted by Fall Back Son
You're confused by the logic of the project itself not by the program.

Quote:

Originally Posted by Sane (Post 143695)
By the way, chosen == bad is a redundant expression. Chosen will never be bad, since your showbad() function asserts that prize != bad.

I was confused there for a while by the logic. That's the reason why I added the chosen == bad expression, while I was posting it here.

Thank you both for your input.

Ooble Apr 9th, 2008 11:50 PM

Re: better randomization?
 
If you seed the random number generator every time you call your functions, and each call is milliseconds apart, you're seeding it with a similar value and resetting it. Therefore, rand()'s return values will actually be less random, as they're all first values generated from very similar seeds.


All times are GMT -5. The time now is 4:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC