Thread: Random Number Generation

  1. #1

    Question Random Number Generation

    This may seem dumb, but I'm a newbie both at game programming and at programming in general.

    To create stats for my game my program generates a string of random numbers (about 7 of them) all at once by calling the function RANDOM. The code looks like this:

    int RANDOM (int factor)
    srand ((unsigned)time (NULL));
    int rawNum = rand() % factor;
    int num = rawNum + 1;
    return num;

    Problem is, with my 1.2 GHz Athlon, all the numbers get generated
    within one second i.e. the seed for generating the numbers is constant. Is there any way to rectify this?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    You could look it up in the FAQ. In fact, I could post the link, but like you, I'm too lazy to look it up

    The srand call is correct. However, it has to be called once per program only, not once per random number. Remove it from your function and do it once on program startup.

    Call once on startup:

    srand ((unsigned)time (NULL));

    Call when you need a new random number:

    int Random( int dice )
    return ( rand() % dice ) + 1;

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generation
    By drdroid in forum A Brief History of
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM