![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2006
Posts: 203
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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;
}
__________________
Waterloo's Canadian Computing Competition (CCC) - Stage 2 Problems, Solutions, and Test Data Last edited by Sane; Apr 9th, 2008 at 5:22 PM. |
|
|
|
|
|
#4 | |||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Re: better randomization?
Quote:
Quote:
Quote:
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 |
|||
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Complete Randomization | Megabyte | Visual Basic | 5 | Nov 19th, 2007 5:31 PM |