Random numbers problem

This is a discussion on Random numbers problem within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a random number between 1 and 100, but it keeps coming up with the number 42. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Random numbers problem

    I'm trying to create a random number between 1 and 100, but it keeps coming up with the number 42. I've tried everything to try and fix it but nothing will work.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    
    using namespace std;
    
    int number;
    int guess;
    int correct = 0;
    
    int main()
    
    {
    	number = (rand()%100) + 1;
    
    	do
    	{
    		cout << "Enter your guess at the number";
    		cin >> guess;
    
    		if (guess < number)
    		{
    			cout << "Your guess is to low" << endl;
    		};
    
    		if (guess > number)
    		{
    			cout << "Your guess is to high" << endl;
    		};
    
    		if (guess == number || guess == 0)
    		{
    			cout << endl << "Well done your guess is correct!";
    			correct ++;
    		};
    
    	}while (correct < 1);
    
    		return 0;
    		
    }
    I have tried various "#include" statements that I have found in examples, but still none of them work.

    Anyone help?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Why have you placed semi-colons at the end of your if statement blocks?

    Code:
    int number;
    int guess;
    int correct = 0;
    You have made these global variables, they really should be local, IE: place them inside
    the main function

    Like salem said, you need to use srand to seed the number to get a random output.

    Read Salems link but remember to add these headers in your file:

    Code:
    #include <cstdlib> // for rand
    #include <ctime> // to seed the number ie, system clock
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308

    Smile

    Quote Originally Posted by ManiacBR
    I'm trying to create a random number between 1 and 100, but it keeps coming up with the number 42. I've tried everything to try and fix it but nothing will work.
    Ah, well at least it is giving you the ultimate answer

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm trying to create a random number between 1 and 100, but it keeps coming up with the number 42.
    That's really funny.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with random numbers
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2007, 11:30 AM
  2. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 06:49 PM
  3. Random Problem?
    By dizz in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2002, 05:00 PM
  4. random double numbers
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 05-18-2002, 12:26 AM
  5. Promlems with Random Numbers
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 03-20-2002, 08:46 PM

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