Thread: Random number generator

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    16

    Random number generator

    Hi guys.

    I am making a program for a Roulette game I am designing on C.
    This program must:

    Read 8 input numbers, where each pair of numbers represent a single bet number and money amount. The pair of input numbers for one bet is repeated 4 times to represent a player making 4 bets.

    I will need to Use the rand() function to generate random numbers that simulate winning roulette
    numbers.

    This is what I have so far:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    int main (void)
    
    
    {
        int betmode;
        int balance;
        int straightbet;
        int cashbet;
        int number;
    
    
    
    
    
    
        //welcome and tittle
    printf("******************************************************************\n");
    printf("******************************************************************\n");
    printf("Welcome to my Roulette program.");
    printf("\n******************************************************************\n");
    printf("******************************************************************\n");
    
    
        //Instructions for the game
    printf("\nEntering the casino, you have $100\n");
    printf("There are 2 types of bets you can make\n\n");
    printf("1) Straight-up bet: Choose a single number. High risk, high payout! (35 to 1)\n\n");
    printf("2) Red or black: Choose a colour.(1 to 1)\n Red numbers are  1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36\n Black numbers are 2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35\n\n");
    
    
        //Select game mode:
    printf("\nPlease select your preferred type of bet");
    scanf("%d", &betmode);
    balance=100;
    
    
    
    
    //Loop to keep game going as long as you have cash
    while (balance>0)
    {
        if (betmode==1)       //Straight bets
    
    
            printf("\nGoing for the big money, eh?\n Enter a number between 1-36 you'd like to bet on> ");
            scanf("%d", &straightbet);
            printf("Now how much money are you willing to risk? Payout is 35 to 1> ");
            scanf("%d", &cashbet);
            printf("\nYou have bet $%d on the number %d", &cashbet, &straightbet);
            printf("\nPress ENTER to spin the roulette. Keep your fingers crossed!");



    And after this last line I need the program to generate 4 random numbers from 1-36.

    Can anyone help me out here please?

    p.s. I have to Use the rand() function to generate random numbers that simulate winning roulette numbers.

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, so write
    number = rand() % 36 + 1;


    > printf("\nYou have bet $%d on the number %d", &cashbet, &straightbet);
    Watch your use of & when using printf and scanf.
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you need to at least post code that will compile if you want anything like a definitive answer.

    *shrug*
    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.

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    16
    Random number generator-untitled-jpg

    This what it compiles into p.p

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Salem wasn't asking for the output of a program run. He was asking for the complete (compilable) program so that we can compile and run it ourselves, or at least so we can see everything that you're doing. Otherwise it's difficult to say what's going wrong.

  6. #6
    Registered User
    Join Date
    Jan 2017
    Posts
    16
    Got it to work now, thoughts ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    int main (void)
    
    
    {
    int betmode;
    int straightbet, cashbet;
    int number;
    int balance;
    char colorbet, R, B;
    int i, n;
    
    
    
    
    
    
        //welcome and tittle
    printf("******************************************************************\n");
    printf("******************************************************************\n");
    printf("Welcome to my Roulette program.");
    printf("\n******************************************************************\n");
    printf("******************************************************************\n");
    
    
        //Instructions for the game
    printf("\nEntering the casino, you have $100\n");
    printf("There are 2 types of bets you can make\n\n");
    printf("1) Straight-up bet: Choose a single number. High risk, high payout! (35 to 1)\n\n");
    printf("2) Red or black: Choose a colour.(1 to 1)\n Red numbers are  1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36\n Black numbers are 2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35\n\n");
    
    
        //Select game mode:
    printf("\nPlease select your preferred type of bet:");
    scanf("%d", &betmode);
    balance=100;
    
    
    
    
    //Loop to keep game going as long as you have cash
    
    
    
    
        if (betmode==1)       //Straight bets
    
    
            printf("\nGoing for the big money, eh?\n Enter a number between 1-36 you'd like to bet on> ");
            scanf("%d", &straightbet);
            printf("Now how much money are you willing to risk? Payout is 35 to 1> ");
            scanf("%d", &cashbet);
            printf("\nYou have bet $%d on the number %d", cashbet, straightbet);
            printf("\nGoodluck. Here are the Results\n");
    
    
    
    
     n = 4;
    
    
       /* Intializes random number generator */
       srand((unsigned) time(NULL));
    
    
       /* Print 5 random numbers from 0 to 49 */
       for( i = 0 ; i < n ; i++ )
       {
          printf("%d\n", rand() % 36);
       }
    if (number==straightbet)
                      {balance += (cashbet*35);
                       printf("Winner Winner! You now have $%d in your pocket", balance);}
                    else
                       balance -= cashbet;
                       printf("Sorry. You've lost $%d. Your new balance is $%d", cashbet, balance);
    
    
    }]

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    My thoughts are that you are still not bothering to show all of your code.

  8. #8
    Registered User
    Join Date
    Jan 2017
    Posts
    16
    thats all ive done mate

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    In that case my thoughts are that you should assign your random number to "number" and that you should put the following if statement in the for loop so that it checks every number.

    And you should clean up your code, indent and space it properly, get rid of all the blank lines. It makes it easier for others to read and helps you to see what belongs to what (the block structure).

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    To be honest, you don't use braces when you should. This is a problem, because you also don't use good indent style. Once it's straightened out, you can see that the majority of output and other stuff happens whether you are in the right bet mode or not. You also print a message like you lost whether you did or not.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main (void)
    {
        int betmode;
        int straightbet, cashbet;
        int number;
        int balance;
        char colorbet, R, B;
        int i, n;
    
    
        //welcome and tittle
        printf("******************************************************************\n");
        printf("******************************************************************\n");
        printf("Welcome to my Roulette program.");
        printf("\n******************************************************************\n");
        printf("******************************************************************\n");
    
    
    
    
        //Instructions for the game
        printf("\nEntering the casino, you have $100\n");
        printf("There are 2 types of bets you can make\n\n");
        printf("1) Straight-up bet: Choose a single number. High risk, high payout! (35 to 1)\n\n");
        printf("2) Red or black: Choose a colour.(1 to 1)\n Red numbers are  1,3,5,7,9,12,14,16,18,19,21,23,25,27,30,32,34,36\n Black numbers are 2,4,6,8,10,11,13,15,17,20,22,24,26,28,29,31,33,35\n\n");
    
    
    
    
        //Select game mode:
        printf("\nPlease select your preferred type of bet:");
        scanf("%d", &betmode);
        balance=100;
    
    
    //Loop to keep game going as long as you have cash
    
    
    
    
    
    
    
    
        if (betmode==1)       //Straight bets
            printf("\nGoing for the big money, eh?\n Enter a number between 1-36 you'd like to bet on> ");
        scanf("%d", &straightbet);
        printf("Now how much money are you willing to risk? Payout is 35 to 1> ");
        scanf("%d", &cashbet);
        printf("\nYou have bet $%d on the number %d", cashbet, straightbet);
        printf("\nGoodluck. Here are the Results\n");
    
    
        n = 4;
    
    
        /* Intializes random number generator */
        srand((unsigned) time(NULL));
    
    
    
    
        /* Print 5 random numbers from 0 to 49 */
        for( i = 0 ; i < n ; i++ )
        {
            printf("%d\n", rand() % 36);
        }
        if (number==straightbet)
        {
            balance += (cashbet*35);
            printf("Winner Winner! You now have $%d in your pocket", balance);
        }
        else
            balance -= cashbet;
        printf("Sorry. You've lost $%d. Your new balance is $%d", cashbet, balance);
    
    
    
    
    }]
    See how for if (and in one place, else) the next line is indented? That is the default behavior for if-else: the next line is all that is impacted by the decision. If you have multiple lines of code that should run if something is true, or lines that should run only if something is false, then you need to put those lines in a big curly brace block. One opening brace for where the block starts and one closing brace for where the block ends.

    I have a hard time believing that the compiler didn't complain about the stray ] in your program on the last line, so that might have been a copying error, but if it's in the source, remove it.

    One of the biggest problems with betting is that nothing is saved to number. You print four random numbers and do nothing else with them. They aren't saved anywhere. The easiest change would be to write:
    Code:
    for (i = 0 ; i < n ; i++)
    {
       number = rand() % 36 + 1;
       printf("%d\n", number);
       if (number == straightbet)
       {
          balance += (cashbet * 35);
          printf("Winner winner! You now have $%d in your pocket!\n", balance);
       }
       else
       {
          balance -= cashbet;
          printf("Sorry, you lost $%d. Your new balance is $%d.\n", cashbet, balance);
       }
    }
    Now you can process all four bets.

    I hope this shows you indent style matters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Generator Stuck on 1 Number?
    By Dollydaydream in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2013, 08:43 AM
  2. Only getting 0; random number generator
    By scatterbrain in forum C Programming
    Replies: 10
    Last Post: 10-11-2011, 06:24 AM
  3. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  4. random number generator
    By begginer in forum C Programming
    Replies: 1
    Last Post: 02-23-2011, 08:52 AM
  5. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM

Tags for this Thread