Only getting 0; random number generator

This is a discussion on Only getting 0; random number generator within the C Programming forums, part of the General Programming Boards category; I keep getting 0 (I think) for my random number generator. I'm programming a guessing game where a number between ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Only getting 0; random number generator

    I keep getting 0 (I think) for my random number generator. I'm programming a guessing game where a number between 100 and 999 is generated and the user guesses the number.

    Here is my generator code:


    srand(time(NULL));
    int N = 999;
    int random = 100 + rand() % N;

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    try it like this...
    Code:
    random = 100 + (rand() % N);
    Also if you are looking for a number betteen 100 and 999 your N value should be 899. (899 + 100 = 999)

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    try:
    Code:
    srand(time(0));
    int N = 999;
    int random = rand() % N + 100;

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,249
    What leads you to "think" you are always getting zero results?

    % operator is the same precedence as * or /, so the reordering or bracketing mentioned by Tater and Billy should not affect anything.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Quote Originally Posted by grumpy View Post
    What leads you to "think" you are always getting zero results?

    % operator is the same precedence as * or /, so the reordering or bracketing mentioned by Tater and Billy should not affect anything.
    well, what makes me think I'm getting zero is that it's a guessing game . Whenever I input 000 as a guess, it gives me 3 correct prompts, one for the hundreds place, one for the tens place, and one for the ones place. any other number yields nothing.

    And you are correct grumpy, Tater and Billy's corrections have not affected anything. But thanks for your attempt Tater and Billy
    Last edited by scatterbrain; 10-08-2011 at 09:19 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So... show us the rest of your code (in code tags please) lets see what's going on...

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,249
    Quote Originally Posted by scatterbrain View Post
    well, what makes me think I'm getting zero is that it's a guessing game . Whenever I input 000 as a guess, it gives me 3 correct prompts, one for the hundreds place, one for the tens place, and one for the ones place. any other number yields nothing.
    Well .... unless you provide a small sample of code that repeatably exhibits the behaviour, you're forcing us to play a guessing game about what the problem is.

    Sorry, but I have better things to do. Like clearing lint from my belly button.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Okay, I wanted to see if only minimal code was necessary for me to be helped before I dropped this on you guys...
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<math.h>
    
    
    main(){
    
    int hundreds, tens, ones, guess, guessnum, guesshundreds, guesstens, guessones;
    
    
    /*number generator*/
    srand(time(NULL));
    int N = 999;
    int random = 100 + rand() % N;
    
    /*guess an 3-digit integer > allow for only 10 guesses */
    for(guessnum=1; guessnum<=10; guessnum++){
        printf("Please make a guess: ");
        scanf("%d", &guess);
    
        
        
        guesshundreds=guess/100;
        guesstens=(guess/10)%10;
        guessones= guess%10;
    
        /*if the guess equals the random generated number, game is over*/
    if (guess==random)
        printf("You guessed right. It is %d", random);
    
        
        /*if you guess a correct number but not in the correct "place", you made a match */
    if ((guess)== guesshundreds || (guess)==guesstens || (guess)==guessones)
        printf("You made a match!");
    
        /*if you guess the correct number in the hundreds place, you've made a hit */
    if (guess== guesshundreds)
        printf("%d is a hit!", guesshundreds);
        /*if you guess the correct number in the tens place, you've made a hit */
    if (guess== guesstens)
        printf("%d is a hit!", guesstens);
        /*if you guess the correct number in the ones place, you've made a hit */
    if (guess== guessones)
        printf("%d is a hit!", guessones);
    
    }
    return(0);
    }
    So let's say it generates 556. So if someone guesses 335, they've made a hit but not a match (with the 5).

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    It looks like "mastermind" board game.

    Anyway, you have two problems: you need to compare the guess with the random number, not the guess with parts of the guess; you need to compare individual digits, so except for guess==random, all the other comparisons involving a direct comparison with guess are wrong.

    Consider guess = 123 and random = 145. guesshundreds would then be 1, but guess == guesshundreds would be false because 123 != 1.

    You might find this easier to handle if you store the numbers as strings instead of as integers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Finally solved the problem. Thanks laserlight, you were right. And thank you to everyone else... Now I'm not even sure if the number generator was malfunctioning before, but it works now.

    Code:
    #include<stdio.h>
    #include<time.h>
    #include<math.h>
    
    
    main(){
    
    int hundreds, tens, ones, guess, guessnum, guesshundreds, guesstens, guessones, randomhundreds, randomtens, randomones;
    
    
    /*number generator*/
    srand(time(NULL));
    int N = 999;
    int random = 100 + rand() % N;
    
    /*guess an 3-digit integer > allow for only 10 guesses */
    for(guessnum=1; guessnum<=10; guessnum++){
        printf("Please make a guess: ");
        scanf("%d", &guess);
    
        
        
        guesshundreds=guess/100;
        guesstens=(guess/10)%10;
        guessones= guess%10;
        
        randomhundreds=random/100;
        randomtens=(random/10)%10;
        randomones= random%10;
    
        /*if the guess equals the random generated number, game is over*/
    if (guess==random){
        printf("You guessed right! It is %d\n", random);}
    
        
        /*if you guess a correct number but not in the correct "place", you made a match */
    else if ((guess)== randomhundreds || guess==randomtens || guess==randomones)
        printf("You made a match!\n");
    
        /*if you guess the correct number in the hundreds place, you've made a hit */
    else if (guesshundreds== randomhundreds)
        printf("%d is a hit!\n", guesshundreds);
        
        /*if you guess the correct number in the tens place, you've made a hit */
    else if (guesstens== randomtens)
        printf("%d is a hit!\n", guesstens);
        
        /*if you guess the correct number in the ones place, you've made a hit */
    else if (guessones== randomones)
        printf("%d is a hit!\n", guessones);
    
    }
    return(0);
    }

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    If you want random numbers between 100 and 999 then "N" should be 900, not 999, not 899. That will force the random value to be between 0 and 899 which, when added to 100, will produce a final value between 100 and 999.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  3. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 06:34 AM
  4. random number generator
    By noodle24 in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2006, 10:41 AM
  5. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM

Tags for this Thread


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