Thread: Random number generator

  1. #1
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18

    Angry Random number generator

    I and my friend are having problems with random numbers.
    If we use time(NULL) to seed the random number, the random number appears to be the same many times but chances after a while.
    Ex. 49, 49, 49, 68, 68.
    And it allways increments the number like this: 23, 33, 40, 54 and so one.
    When it comes to the number that is inserted as the maximum value it starts over. Like this: 70, 89, 99, 4, 10 ...
    We could use the GetTickCount() to seed the random number but the value increments then too and that is not pure random.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Post the code!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    FAQ: http://www.cprogramming.com/boardfaq.html#random

    Or search the board

    B.t.w. It's almost impossible to make the numbers pure random.

    If you are using linux: http://nodevice.com/sections/ManIndex/man1271.html

  4. #4
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18
    Here is the code:
    Code:
    int random(int low, int high)
    {
    srand(time(NULL));
    return low + rand()%(high-low+1)
    }
    And with GetTickCount() it is almost the same, you just replace the
    srand(time(NULL)) with srand(GetTickCount()).

    B.T.W. What is the best solution?

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should only seed the random generator once per program:
    Code:
    int random(int Range)
    {
       return rand() % Range;
    }
    
    int main()
    {
       srand(time(NULL));
       cout << random(10);
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18

    Smile

    Ok, I will test that as soon as I can. I'm in school right now so I will post if it worked when I come home.

  7. #7
    Registered User Caze's Avatar
    Join Date
    Nov 2002
    Posts
    18
    It works better now.
    Thanks Magos and Monster for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM