Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2008, 4:22 PM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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);

}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 9th, 2008, 4:43 PM   #2
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 203
Rep Power: 2 Fall Back Son is on a distinguished road
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.

Last edited by Fall Back Son; Apr 9th, 2008 at 4:57 PM.
Fall Back Son is offline   Reply With Quote
Old Apr 9th, 2008, 5:03 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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;
    
}

Last edited by Sane; Apr 9th, 2008 at 5:22 PM.
Sane is offline   Reply With Quote
Old Apr 9th, 2008, 8:57 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Re: better randomization?

Quote:
Originally Posted by Sane View Post
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 View Post
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.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 9th, 2008, 10:50 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Complete Randomization Megabyte Visual Basic 5 Nov 19th, 2007 5:31 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:03 AM.

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