Random numbers not random?

This is a discussion on Random numbers not random? within the C Programming forums, part of the General Programming Boards category; Ok so I have my random generator set within a loop that runs 10 times for my game. How come ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    Question Random numbers not random?

    Ok so I have my random generator set within a loop that runs 10 times for my game. How come every time it loops back it generates the same random numbers for each loop. I want it to play 10 DIFFERENT games, by different I mean the players dice should not roll the same values each time.

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> 
    
    int main()
    {
        int diceI, diceII, S, dicesum, iwin=0, ilose=0, iwinII=0, iloseII=0, iloseIIpoints=0, iwinIIpoints=0;
        int l;
        int pointin, points=0;
        
        printf("CRAPS\n");
        printf("INSTRUCTIONS: Press S then Enter to begin the game.\n\n");
        
        scanf("%d", &S);
        
        for(l = 0; l < 10; l++)
        {
        
        printf("Start of game %d\n", l);
        
        srand(time(NULL));
          diceI = rand()%(6 - 1 + 1) + 1;
          printf ("You rolled a %d ", diceI);
          diceII = rand()%(6 - 1 + 1) + 1;
          printf ("and a %d. \n", diceII);
        
        dicesum = diceI + diceII;
        pointin = dicesum;
        
        
        if (dicesum == 7) {
            printf("You win!\n\n");
            iwin = iwin + 1;
            }
        else if (dicesum == 11) {
            printf("You win!\n\n");
            iwin = iwin + 1;
            }
        else if (dicesum == 2) {
            printf("You lose.\n\n");
            ilose = ilose + 1;
            }
        else if (dicesum == 3) {
            printf("You lose.\n\n");
            ilose = ilose + 1;
            }
        else if (dicesum == 12) {
            printf("You lose.\n\n");
            ilose = ilose + 1;
            }
        else
        {        
            while (1) {
            diceI = rand()%(6 - 1 + 1) + 1;
            printf ("You rolled a %d ", diceI);
            diceII = rand()%(6 - 1 + 1) + 1;
            printf ("and a %d. \n", diceII);
            dicesum = diceI + diceII;
            
            points = dicesum + points;
            
            if (dicesum == 7) {
                iloseIIpoints = pointin + points;
                printf("You lose. You have earned %d points.\n\n", iloseIIpoints);
                iloseII = iloseII + 1;
                break;
                }
            else if (points >= pointin) {
                iwinIIpoints = points + pointin;
                printf("You win! You have earned %d points.\n\n", iwinIIpoints);
                iwinII = iwinII + 1;
                break;
                }
            else continue;
            
            }
                
        }
        
        }
        
        printf("On your first roll you won %d times.\n", iwin);
        printf("On your first roll you lost %d times.\n", ilose);
        printf("On your second roll you won %d times and earned %d points.\n", iwinII, iwinIIpoints);
        printf("On your second roll you lost %d times and earned %d points.\n", iloseII, iloseIIpoints);
        
        return 0;
    }
    Last edited by yacek; 10-08-2010 at 07:33 PM.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    srand(time(NULL));
    you should only do this once, try moving it outside of the loop
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Quote Originally Posted by ಠ_ಠ View Post
    you should only do this once, try moving it outside of the loop
    Thanks ಠ_ಠ! Works now.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
        srand(time(NULL));
          diceI = rand()%(6 - 1 + 1) + 1;
    Yes, you should move the seed out of the loop.
    Also (rand() % 6) + 1 ; should be adequate. The extra really doesn't do anything.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    isn't this expressioin going to give you the same value 6?

    6 - 1 + 1 == 6 ??

    [EDIT] Perhaps CommonTater had already pointed that out [/EDIT]

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Quote Originally Posted by ssharish2005 View Post
    isn't this expressioin going to give you the same value 6?

    6 - 1 + 1 == 6 ??

    ssharish
    Yep, changin' it now.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Cool, got the code to work now. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 01:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21