Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 1:05 AM.

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