Thread: random numbers

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    Question random numbers

    i used this function to get a random number and now the random numbers are picked in the same order every time the program is run. How can i change the order each time?

    int randMid(int low, int high)
    {
    return low+rand()%(high-low+1);
    }

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    change the "seed" to the number generator (rand).
    the rand() function is actually a pseudo random number generator meaning that the you get the same sequence each time with the same seed number. This is very useful in some programming areas such as games.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    seed

    whats a seed

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    srand()

    Go to the FAQs and the function is explained there

  5. #5
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    the number that starts the "random" generator.
    for C/C++ it is
    srand(int seed);
    usually you use the system time as the seed so the program will run differently each time.
    Otherwise you can pick a seed number for predictability.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    ah

    i put srand(int time); in int main before the random number was chosen and i got a parse error

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    include time.h at the top and use this srand(time(NULL))

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