Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Go to the back of the beyond, then turn left. We're two streets from the middle of nowhere.
    Posts
    4

    Random Numbers

    Problem: Acquiring a random integer number between 1 and 100. (rand() % 100) + 1 has failed. Suggestions?

    Story: I'm trying to write a simple guessing game; I think of a number, an integer, betwixt 1 and 100, while you try to guess it. I use (rand() % 100) + 1 to get the (supposedly) random number. Over the dozen or so times I've done it, I have consistently gotten 84 as the secret number. By my calculations, that's roughly a chance of 1 in 10^24; one in a million billion billion. Clearly, there's a problem somewhere. My full code follows.
    Code:
    #include <stdio.h>
    main () {
    	int guess, high, low;
    	high = 100;
    	low = 0;
    	char line[80];
             printf("I'll think of a number and you try to guess it.\n");
    	printf("If you get it right, you get a prize. Come on, friend. ");
    	while (1) {
    		int secret = rand() % 100 + 1;
    		printf("Enter your guess.\n");
    		printf("Hint: It's between %d and %d\n", low, high);
    		(void)fgets(line, sizeof(line), stdin);
    		(void)sscanf(line, "%d", &guess);
    		if (guess == secret) {
    			printf("Bingo!\n");
    			break;
    		}
    		if (guess > secret) {
    			high = guess;
    			printf("Sorry, too low.\n");
    		} else {
    			low = guess;
    			printf("Sorry, too high.\n");
    		}
    	}
    	return(0);
    }

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Random numbers in computers aren't actually random, they are generated using a starting number called seed and then does som arithmetic on it to get a new value. Your program doesn't change the seed so the program will use the same value over and over and thus always producing the same result.
    You can get a nice new seed by putting srand(time(0)) before using the rand() function. Don't forget to #include <time.h> either.
    [edit] And don't put the srand() inside of the loop, it should only be done once, so put it before your loop.

    Using the time as a seed is good because time is always changing so your seed will always be different when running the program.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Go to the back of the beyond, then turn left. We're two streets from the middle of nowhere.
    Posts
    4
    Ah! Of course. I knew about the seed issue, but I didn't see the meaning. Thank you kindly. I shall fix that immediately.

Popular pages Recent additions subscribe to a feed

Similar Threads

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