Thread: help improving code

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    help improving code

    the random number is always 41

    Code:
    //*last edited 5/27/2009
    number guessing game
    By CJ*/
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
          int a;//to declare a a random number
          a = rand();
          int x;// used to compare to a
          
          cout<<"welcome to the number guessing game\n";
          
          if ( a < 50 ) { //gives you details about what the number (a) is
               cout<<"the number is less than 50\n";
               }
          if ( a > 50 ) {
               cout<<"the number is greater than fifty\n";
               }
          if ( a == 50 ) {
               cout<<"the number is fifty\n";
               }
          if ( a > 0 ) {
             cout<<"the number is greater than 0\n";
             }
         
          while ( x != a ) {
          
          cout<<"What do you think the number is?\n";//gives x which is compared to a 
          cin>> x;
          cin.ignore();
          
          if ( x == a ) {
               cout<<"hey thats it!\n";
               }      
          if ( x > a ) {
               cout<<"your too high\n";
               }
          if ( x < a ) {
               cout<<"your to low\n";
               cout<< a <<endl;
               }
               }     
               cin.get();
    }           
    //end of program
    Last edited by jamort; 05-27-2009 at 10:27 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes. If you don't change the random number seed (using srand), the random numbers won't change.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read about srand()

    Specifically, do something like this ONCE at the start of main
    srand( time(NULL) );
    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.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    106
    ok it works fine now....

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    another thing you might try, if you're running linux or some other system that implements it is the following:

    Code:
    #include <sys/types.h>
    #inlcude <sys/stat.h>
    #include <fcntl.h>
    
    unsigned int my_rand()
    {
      unsigned int number;
      int fd = open("/dev/random", O_RDONLY);
      read(fd, &number, sizeof(unsigned int));
      close(fd);
      return number;
    }
    this is likely to be a better source of random numbers than rand().

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    106
    Quote Originally Posted by Elkvis View Post
    another thing you might try, if you're running linux or some other system that implements it is the following:

    Code:
    #include <sys/types.h>
    #inlcude <sys/stat.h>
    #include <fcntl.h>
    
    unsigned int my_rand()
    {
      unsigned int number;
      int fd = open("/dev/random", O_RDONLY);
      read(fd, &number, sizeof(unsigned int));
      close(fd);
      return number;
    }
    this is likely to be a better source of random numbers than rand().
    Im stuck with vista until my brother gets me a copy of linux which is going to be soon; hes a developer ands making some special programs before he sends me a copy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Improving Code Interface
    By helloamuro in forum C Programming
    Replies: 20
    Last Post: 05-02-2008, 04:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM