Thread: Seeded rand problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    137

    Seeded rand problem

    I'm using this function to generate a random number:
    Code:
    int flip ();
    int flip () {
    	return 0;( (srand ( time (NULL) ) )%2);
       }
    However I get an error message saying "not an allowed type", any ideas how to fix?

    I have included time.h by the way.

    Thanks

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Call srand once to seed the random number generator...then call rand() to generate the random numbers

    The FAQ has a chapter on this...

    http://www.cprogramming.com/boardfaq.html#random

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You realize that this line only returns zero and nothing else ever is executed:

    return 0;( (srand ( time (NULL) ) )%2);

    You knew that, right?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Yeah, that was only in there while I was attempting to fix my problem. I often throw code around trying to see whats broken, it made sense at the time.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by crag2804
    Yeah, that was only in there while I was attempting to fix my problem. I often throw code around trying to see whats broken, it made sense at the time.
    So... are you sorted now or not?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    Yes thanks, I read the tutorials on the site to begin with but it didn't make it clear that you had to seed the rand () seperatly. Needless its sorted now, thanks all.
    http://uk.geocities.com/ca_chorltonkids

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference in Unseeded rand() and seeded rand()
    By relientk_man in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2005, 07:32 AM