pass time(0) to srand() when generating random numbers.

Discussion in 'C Programming' started by Intaek LIM, Oct 31, 2003.

  1. Intaek LIM

    Intaek LIM Guest

    generally, we use srand(time(0)) to generate random numbers.
    i know why we use time(0), but i can not explain how it operates.

    first, see example source below.

    ---------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    printf("This will generate 5 random numbers.\n\n");

    srand(time(0));

    int i;
    for(i=0; i<5; i++)
    {
    printf("generated %d\n", rand());
    }

    return 0;
    }
    ---------------------------------------------

    this is an idiom of generating random numbers, i think.
    and a different source below operates correctly too.

    ---------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    printf("This will generate 5 random numbers.\n\n");

    int seed = time(0);
    srand(seed);

    int i;
    for(i=0; i<5; i++)
    {
    printf("generated %d\n", rand());
    }

    return 0;
    }
    ---------------------------------------------

    but, following source produces incorrect results.
    (returns the same values everytime)


    ---------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    printf("This will generate 5 random numbers.\n\n");

    // seed is the return value for time(0)
    // i wrote it by my hand. :-(
    int seed = 1067589554;
    srand(seed);

    int i;
    for(i=0; i<5; i++)
    {
    printf("generated %d\n", rand());
    }

    return 0;
    }
    ---------------------------------------------

    what is the difference of these three sources?
    plz tell me something who knows it.
     
    Intaek LIM, Oct 31, 2003
    #1
    1. Advertising

  2. In article <>, Intaek LIM wrote:
    [cut]
    > // seed is the return value for time(0)
    > // i wrote it by my hand. :-(
    > int seed = 1067589554;
    > srand(seed);


    The time() function returns a value that represents the current
    time. Seeding the pseudo random generator with time(0) will
    ensures that you get a different pseudo random list of numbers
    each time you run the program.

    Hard coding the seed ("1067589554" in your code) ensures that
    you get the *same* pseudo random list of numbers each time you
    run your program.

    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. August1
    Replies:
    4
    Views:
    7,770
    August1
    Dec 8, 2003
  2. August1
    Replies:
    0
    Views:
    7,567
    August1
    May 16, 2004
  3. Replies:
    4
    Views:
    369
    Marcus Kwok
    Oct 19, 2005
  4. Chelong
    Replies:
    11
    Views:
    1,276
    James Kanze
    Jul 10, 2007
  5. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,183
    Nick Keighley
    Oct 18, 2011
Loading...

Share This Page