# Thread: Help with C Monte Carlo

1. ## Help with C Monte Carlo

i guys,

I'm trying to make a simple Monte Carlo simulation for a class. It requires that I make a roulette game with random numbers.

So far:

There needs to be 1000 games max. You can bet \$1 on the game and continue playing until you loose (black and red are wins) .

The statistics are:
Black 0-0.4737
Red Same
Green 0 to .0526.

Because rand() only uses integers. I multiplied the probabilities times 1000.

My code is as follows:
Code:
```#include <stdio.h> //std input and output
#include <conio.h>
#include <math.h> //math library
#include <stdlib.h> //standard libarary

int main (void)
{
printf("Welcome To The Roulette Wheel Simulator. \n");
int counter = 0; //count the number of games
int cash = 0; //count the number of \$1 you win.
int p = 0; //probability is an integer here to allow for rand.
double probability = 0; //hasn't been used yet.
int games = 0;
//pseudo-random number generator
while (counter <1000) {
counter = counter + 1;
p = rand()%4737+1000;
}
while(p > 526 && counter <1000) //continue to loop until a loss
{
counter = counter + 1; //increment counter.
cash = cash + 1; //add to cash.
}
if(p <= 526)
{
printf("Once you kill a cow, you gotta' make a burger. _GaGa_ \n You lost at %d games\n You won \$%d \n", &counter, &cash);
printf ("I knew you'd steal all my honey, you selfish (imagination here). _Beyonce_ \n Press Any Key to Exit says Homer\n");
}
getch();
}```
It complies but never prints anything.

2. Code:
```          while (counter <1000) {
counter = counter + 1;
p = rand()%4737+1000;
}```
This keeps anything from being done. First of all, this loop ensures that counter will be 1000 once the loop is over, which means that the "while(p > 526 && counter <1000)" loop will never run. Also, because of the assignment to p, namely the random number + 1000, p is always going to be greater than 1000, so "if(p <= 526)" will always be false.

3. Code:
```#include <stdio.h> //std input and output

#include <math.h> //math library
#include <stdlib.h> //standard libarary

int main (void)
{
printf("Welcome To The Roulette Wheel Simulator. \n \n");
long cash = 0; //count the number of \$1 you win.
int p = 0;  //probability is an integer here to allow for rand.
double probability = 0; //hasn't been used yet.
int games = 0;
//pseudo-random number generator
int counter;
for(counter = 0; counter < 1000; counter++)
{
int p = 0;
p = rand()%4737;

if(p >= 526  && counter < 1000) //continue to loop until a loss
{
cash = cash + 1; //add to cash.
}
if(p <= 526)
{
printf(" You lost at %d games\n \n You won \$%d \n \n", &counter, &cash);
printf ("Press Any Key to Exit\n");
break;
}

}
getch();
}```
Tried to fix it, still not working. thankx for the help

4. Code:
```#include <stdio.h> //std input and output
#include <math.h> //math library
#include <stdlib.h> //standard libarary

int main (void)
{
printf("Welcome To The Roulette Wheel Simulator. \n \n");
long cash = 0; //count the number of \$1 you win.
int p = 0;  //probability is an integer here to allow for rand.
double probability = 0; //hasn't been used yet.
int games = 0;
//pseudo-random number generator
int counter;
for(counter = 0; counter < 1000; counter++)
{
int p = 0;
p = rand()%4737;

if(p >= 526  && counter < 1000) //continue to loop until a loss
{
cash = cash + 1; //add to cash.
}
if(p <= 526)
{
printf(" You lost at %d games\n \n You won \$%d \n \n", &counter, &cash);
printf ("Press Any Key to Exit\n");
break;
}
}
getch();
}```
1. Learn indentation - SourceForge.net: Indentation - cpwiki

2. && counter < 1000 will never happen, given the for loop has the same test.

3. >= 526 and <= 526 overlap at 526. It would have been better to use an else

4. &counter, &cash - you have to pay attention to what you pass to printf/scanf.
printf(" You lost at %d games\n \n You won \$%ld \n \n", counter, cash);

5. Originally Posted by Salem
[code]
2. && counter < 1000 will never happen, given the for loop has the same test.
[/FONT]
Surely you must mean it WILL ALWAYS HAPPEN, BECAUSE it is inside the loop, therefore the loop condition is still true. It is useless, but in no case false.

6. > It is useless, but in no case false.
Yes

7. ## Thankx

I cleaned up the code and indentation. The problem was the algorithm itself. The code now runs fine; however, the rand() seems to generate the same set of numbers, causing the same number of wins and losses each time. I tried printf on p and it keeps generating the same "random" numbers.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int
main (void)
{
int p = 0;
int losses = 0;
int wins = 0;
int counter_Black = 0;
int counter_Red = 0;
int counter_Green = 0;
int counter = 0;
for (counter; counter < 1001; counter++)
{
p = rand () % 10000;
if (p <= 4736)
{
counter_Black = counter_Black + 1;
wins = wins + 1;
}
if (p > 4736 && p <= 9473)
{
counter_Red = counter_Red + 1;
wins = wins + 1;
if (p > 9473 && p <= 10000)
counter_Green = counter_Green + 1;
losses = losses + 1;
}
}
printf ("You won \$%d after %d games.\nYou had %d losses. \n \n ", wins,
wins, losses);
getch ();
}```