srand() not randomizing me?!

This is a discussion on srand() not randomizing me?! within the C Programming forums, part of the General Programming Boards category; Hi all. I decided to give C a go, and so I started to reproduce my very first QBASIC program ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    srand() not randomizing me?!

    Hi all. I decided to give C a go, and so I started to reproduce my very first QBASIC program - a simple guess the number game.
    Seemed to go pretty well, except I'm having troubles getting random numbers. I understand that I need to use srand() to initialize a seed, and its common to put time() in there. However, no matter what I do I cannot seem to get different results.

    Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void) {
    
        srand((unsigned)time(NULL));  // seed random number generator with time (to get pseudo random numbers)
        int randomnumber = (10*rand()/(RAND_MAX+1.0)); // get a number between 1 and 10
    
        int count = 0;
    
        for (int usernumber; usernumber != randomnumber;) {
            printf("Guess the number.\n");
    
            scanf("%d",&usernumber); // get the guessed number
            count++;
    
            if (usernumber == randomnumber) { // if they guessed it
                break;
            }
            if (usernumber < randomnumber) { // too low
                printf("Nope. Too low...try again. You have made %d guesses.\n", count);
            }
            if (usernumber > randomnumber ) { // too high
                printf("Nope. Too high... try again. You have made %d guesses.\n", count);
            }
            
            if (count > 4) { // if they havn't guessed it 5 times, give em a hint (tell them the number).
                printf("Hint: The number is %d\n",randomnumber);
            }
        }
    
        printf("You guessed the number in %d tries!", count); // final output.
    }
    I've tried using just srand(time()); and various combinations, but nothing.

    The strange thing is - and I think this is related to time() - is that at different times (like seperated by hours) I get different numbers. For example, right now the random number is 3. Every time I run it. But a few hours ago it was 4. Maybe this is due to the way I'm getting my random number? I'm not entirely sure on the way that code works.

    I'm using lcc-win32 to compile. It seems nice, I wish I could integrate jEdit with lcc's compiler (so I dont have to use lcc's IDE); are there any better options?

    Edit: I did my best to search the forum for this, but I could find no problem similar. If this is a common problem, I apologize, looks like you guys get a lot of repeat questions...
    Last edited by crummy; 02-11-2005 at 06:33 PM.

  2. #2
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        
        int usernumber; time_t seconds; 
        seconds = time(NULL);
        srand(seconds);  // seed random number generator with time (to get pseudo random numbers)
        int randomnumber = rand()%10;
    
        int count = 0;
    
        for (usernumber; usernumber != randomnumber;) {
            printf("Guess the number.\n");
    
            scanf("%d",&usernumber); // get the guessed number
            count++;
    
            if (usernumber == randomnumber) { // if they guessed it
                break;
            }
            if (usernumber < randomnumber) { // too low
                printf("Nope. Too low...try again. You have made %d guesses.\n", count);
            }
            if (usernumber > randomnumber ) { // too high
                printf("Nope. Too high... try again. You have made %d guesses.\n", count);
            }
            
            if (count > 4) { // if they havn't guessed it 5 times, give em a hint (tell them the number).
                printf("Hint: The number is %d\n",randomnumber);
            }
        }
    
        printf("You guessed the number in %d tries!", count); // final output.
    }
    Last edited by xxxrugby; 02-11-2005 at 06:52 PM.
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Code:
    int randomnumber = rand()%10;
    The OP wants a random number between 1 and 10, not 0 and 9. Obviously, adding 1 to the result of that will get you what you want. But you can't call rand() until after you call srand() if you want a random number.

    Code:
    {
      int randomnumber;
    
      srand(time(NULL));
      randomnumber = (rand()%10)+1;
    }
    That should get you what you want.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    16
    Thanks so much, itsme86! I owe you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomizing dealing program C
    By BSmith4740 in forum C Programming
    Replies: 2
    Last Post: 08-04-2008, 02:42 PM
  2. randomizing
    By zanderela in forum C Programming
    Replies: 2
    Last Post: 03-21-2008, 02:54 AM
  3. Randomizing link list.
    By Axel in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 11:03 AM
  4. randomizing?
    By cerin in forum C++ Programming
    Replies: 55
    Last Post: 02-03-2005, 06:58 PM
  5. Randomizing
    By Morph in forum C++ Programming
    Replies: 18
    Last Post: 01-26-2003, 06:36 PM

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