about srand()

This is a discussion on about srand() within the C Programming forums, part of the General Programming Boards category; Hello, Anyone can help me how to use srand() in generate random number ? And how exactly do?...

  1. #1
    hei
    hei is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    about srand()

    Hello,

    Anyone can help me how to use srand() in generate random
    number ? And how exactly do?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    Example

    /* RAND.C: This program seeds the random-number generator
    * with the time, then displays 10 random integers.
    */

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

    void main( void )
    {
    int i;

    /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
    srand( (unsigned)time( NULL ) );

    /* Display 10 numbers. */
    for( i = 0; i < 10;i++ )
    printf( " %6d\n", rand() );
    }


    Output

    6929
    8026
    21987
    30734
    20587
    6699
    22034
    25051
    7988
    10104

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    and another thing , you are supposed to call srand just once before making calls to rand() , dont call srand before every call to rand in the hope of getting better random numbers

  4. #4
    hei
    hei is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    Ok, it's fine.
    Thank you. ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use srand() twice in a function?
    By Jake.c in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 11:51 AM
  2. srand() in .Net
    By Sad Programmer in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 05:01 PM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand()... possible reasons for failure
    By lightatdawn in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21