Thread: rand();

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    rand();

    i know there is a way to make this function:
    Code:
    rand();
    return a completely random number using something from time.h but cant figure it out, anyone got a suggestion or an alternative?

  2. #2
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Code:
    srand((unsigned)time(0));
    Then just call rand(); and you can use % to limit it.

    I think it talks about this in the FAQ.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Everything you need to know about random numbers: FAQ
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    I just posted a program using the system clock as the Seed, then a random generated number as follows:

    Code:
    //SEED THE RANDOM NUMBER WITH SYSTEM CLOCK
    		srand(static_cast<unsigned int>(time(0)));
    
    //GENERATING A RANDOM NUMBER FROM 1 - 100
    		random_number = (rand() &#37; (100 - 1 + 1)) + 1;

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I heard somewhere that the highest-order bits are the "most random". If you really care about that, I think this does the trick:
    Code:
    int RandomNumber(int min, int max)
    {
    	static int seeded = 0;
    
    	if (seeded == 0)
    	{
    		srand(time(NULL));
    		seeded = 1;
    	}
    
    	return rand() / (double)RAND_MAX * (max - min + 1) + min;
    }
    Don't quote me on that... ...seriously

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For information about high order bits and most-randomness and all that, see Prelude's article: http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM