Thread: random number generator

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    random number generator

    Hi. I was wondering if I can make the pseudo-random rand() function more random?
    I'm generating a random number between 0 and 2 for 30 times:

    for(int i=1; i<=30; i++)
    notification_rand = int(3.0*rand()/(RAND_MAX+1.0));

    Also, is there a better way than using the current time as the seed, so the sequences generated are more different from each other?

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Hi. I was wondering if I can make the pseudo-random rand() function more random?
    I'm generating a random number between 0 and 2 for 30 times:
    You create your own 64 random bit generator by using several calls to rand.

    Also, is there a better way than using the current time as the seed, so the sequences generated are more different from each other?
    If you could find something more random? Some hardware chips might have a special way to get a random number seed.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Google for PRNGs (pseudo-random number generators). Some examples of different types are: linear congruential generator, and the mersenne twister (been a while since I've looked in much depth into this stuff, so its a little slow coming to my mind).

    Also, you might want to check into the DIEHARD tests.

    And, chapter 7 of Numerical Recipes, available here: http://library.lanl.gov/numerical/bookcpdf.html
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i had seen some applications which use the mouse movement values as a seed... i guess those will give better random values..

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Verbenaca

    Also, is there a better way than using the current time as the seed, so the sequences generated are more different from each other?

    Thanks
    Use a higer resolution timer ... I've run into problems using time(), especially if using a script to run a simulation for example. If the program return really fast, when your script runs the next iteration the seed will often be the same because time() is the same. It's resolution is only 1 second so often 2 or 3 iterations may complete in under a second giving you the same seed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 06:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM