Thread: random number generation

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    24

    random number generation

    These are two programs for generating random 0's and 1's
    The essential logic is the same the first one
    has the code in main whereas the second one has in the function
    randgen().
    The first is generating the correct output whereas the second is giving only zeroes.Why the discrepancy?
    PROGRAM 1
    Code:
    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    int main()
    {
    int i;
    time_t seconds;
    time(&seconds);
    srand((unsigned int)seconds);
    for(i=0;i<100;i++)
    {
    printf("%d",rand()%2);
    }
    }


    PROGRAM 2
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int randgen()
    {
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    return rand()%2;
    }
    int main()
    {
    int i=0;
    for(i=0;i<100;i++)
    printf("\n %d",randgen());
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should seed the random number generator only once.

    Incidentally, you could consider reading about using rand() and pseudorandom number generators in general.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well in the second one, you're calling srand() with a constant, so naturally rand() is always going to produce the same sequence.

    Time might seem a fluid thing to you, but to the couple of mSec it takes this program to run (if that), it looks pretty close to being a constant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    @
    Code:
    laser light
    
    Code:
    You should seed the random number generator only once
    If i am not wrong the reason for manual seeding if i may call is to choose 'numbers of random sequence' i.e. a different starting value in a sequence will lead to different set of numbers of the same sequence {a(n)}.So using 'time' which is constantly variable, i intended to generate different seeds in the second one case unlike the first one where the seed is a constant . So there is no hard and fast rule that seeding has to take place once.Please correct me if i am wrong. I think Salem's explanation of the discrepancy is correct wherein he points out that the time is almost a constant resulting in the generation of same initial value every time. Anyway both of you thanks for the links and explanations.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you were running some simulation repeatedly by seeding from a list of pre-determined seeds, then of course you would seed more than once. In this case, you are obtaining a seed in a rather raw form from time(), so you should seed only once. Even so, I would suggest using the suggestion of time_seed() from the eternallyconfuzzled.com link on using rand().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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 Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM