Thread: rand()?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    Question rand()?

    I am making an AI for my ttt game... and im making a test to see if rand() really works... but i run this code...
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(int argc, char *argv[])
    {
      int test;
      
      test = rand()%5;
      cout << test << "\n";
      
      system("PAUSE");	
      return 0;
    }
    and i always get 1!!!!! why doesnt it work!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Re: rand()?

    You can add this for more checking :
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(int argc, char *argv[])
    {
      int test = 0;
      int test1 = 3;
      test = rand()%5;
      test1= rand()%5;
    
      cout << test << "\n"; 
      cout << test1<< endl;
      system("PAUSE");	/// COMMENT THIS 
      return 0;
    }
    tell us what happend.
    C++
    The best

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    100
    ok... i see what happends... but either im stupid or this doesnt help me get a random number between 1 and 5.

  4. #4
    You didn't seed your random numbers

    You must call this before you do random numbers

    srand(time(NULL));

    - OR -

    srand(unsigned(time(NULL)));

    you should also include ctime for C++, or time.h if you are using C

  5. #5
    Here's some code for you to use

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(int argc, char *argv[])
    {
      int test = 0;
      int test1 = 3;
      test = 1 + (rand()%5);
      test1= 1+ (rand()%5);
    
      cout << test << "\n"; 
      cout << test1<< endl;
      system("PAUSE");	/// COMMENT THIS 
      return 0;
    }
    You must add 1, because the rand() starts at 0. So without the +1 you are saying random number between 0 and 4. You'll learn, almost everything in C++ starts at 0.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    100
    ^%*$^%*($%@@$^#^~!~~!!!!!!!
    i just deleted my tic tac toe game... it took hours to do!!!

    Ohh well... thanks anyway... ill use rand for something else

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: rand()?

    Originally posted by elfjuice
    Rand()?
    Maybe you should search the board first before asking something.
    There are a lot threads about the rand function.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    100
    i tried that before i posted... but i didnt get an answer

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i just deleted my tic tac toe game... it took hours to do!!!

    What compiler?
    If you are using a compiler with an IDE, you may have a backup file. Check for a file with a .bak extension.

  10. #10
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    rand() is a psuedo-random number generator. It will produce the same sequence of "random" numbers with a given seed.
    It is common to use time() as the seed number for srand() which is used to seed the number generator.

    Without seeding srand() you'll get 41 for your first number from rand(). 41 % 5 = 1.

    /* edited: better algorithm for random numbers in link below */
    Last edited by taylorguitarman; 06-07-2002 at 04:49 AM.
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  11. #11
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    What is the difference between rand and random? And which is the best to use?

  12. #12
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM