Thread: problem with random number generator

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    11

    problem with random number generator

    Beginner alert! ^^

    I've got the following piece of extremely simple code and I'm not sure exactly why it keeps evaluation to '41'.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
        int main(void){
            
        int random;
        
        random = rand();
        printf("%d\n", random);
        
        system("pause");
        
        return 0;
        
    }
    Any help is appreciated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By definition, rand() is predictable -- every time you run the program, you get the same sequence of numbers.

    If that's not what you want, you need to use srand() to start the seed somewhere different; the usual method is some line of code morally equivalent to
    Code:
    srand(time(NULL));
    (That's why you included the time.h header here.)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Because the random numbers aren't really random. Each time you start the program you will get the same sequence of numbers. Kinda predictable and kinda nice for debugging purposes should you need to recreate conditions that might have blown things up.

    You need to call some function such as seed (look it up) and give it some number that's guaranteed to always be different each time you run your program. Such as the timestamp.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to give the program a different random seed to start off from. If you imagine that you have a deck of cards, that you shuffle into exactly the same order each time, then you can still make the card that you deal first be "random" by splitting the deck at a different time.

    Random number generators will generate the same sequence of numbers each time, given in identical start point (seed). However, since the numbers repeat only after MANY (thousands or millions) of numbers, it's random enough for most purposes. What we need to do is just find a good way to start at a different place each time.

    Most people find it sufficient to do:
    Code:
    srand((int)time(NULL));
    which will give you a different starting point as long as you give it sufficent time between each run (more than 10 seconds or so).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    11
    So how exactly would this look like in a piece of code? Setting an integer to a random number that is, through the use of srand.

    Code:
    random = srand((int)time(NULL));
    ... is giving me errors.

    Edit:
    That is, inserting it exactly in to the original post I made.
    Last edited by Niss; 10-01-2008 at 05:41 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Niss View Post
    So how exactly would this look like in a piece of code? Setting an integer to a random number that is, through the use of srand.

    Code:
    random = srand((int)time(NULL));
    ... is giving me errors.

    Edit:
    That is, inserting it exactly in to the original post I made.
    That's because you are trying to get a number out of a void function. srand() is a function that picks where in the long sequence of random numbers you start. It does not give you anything back, and you then call rand() to get a random number. And you do not call srand() again - at least not unless you want to start over in the random sequence, and you will have to be very good at generating a good seed if you want that to be any better than the rand() function gives already.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    11
    Got it! Thanks a million

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. help with random number generator
    By Brimak86 in forum C Programming
    Replies: 3
    Last Post: 01-12-2008, 09:37 PM
  3. How exactly does the random number generator work?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 12:46 AM
  4. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM
  5. Random Number Generator
    By atif in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2002, 05:11 AM