Thread: Help with C Monte Carlo

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Question 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.
    Last edited by shaunmikex; 03-20-2010 at 06:00 PM. Reason: Add More Info/ Formatting

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    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. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Salem View Post
    [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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It is useless, but in no case false.
    Yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    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 ();
    }

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monte Carlo reliability program - SegFault :(
    By Kibble Fat in forum C Programming
    Replies: 7
    Last Post: 12-15-2009, 02:41 PM
  2. A new random number generator!
    By Sebastiani in forum General Discussions
    Replies: 19
    Last Post: 07-30-2009, 03:27 PM
  3. finding area of unit circle by monte carlo..
    By niceguy in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 01:32 PM
  4. Problem with Monte Carlo(integration by rejection)
    By dionys in forum C Programming
    Replies: 9
    Last Post: 04-07-2004, 09:53 AM
  5. monte carlo methods
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2001, 11:29 PM