Thread: Random Number Generator

  1. #16
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    True provided the implmentation your using has a linear-congruential generator (which in hindsight, it probably is, so you're right about that). Not necessarily true for other types of PRNGs, though.

    The following usually works well for the 1...n range:

    1 + (int)(n * rand() / (RAND_MAX + 1.0));
    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.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Originally posted by Cat
    Zach, Stoustrup says exactly the opposite in The C++ Programming Language, 22.7. He recommends another method:
    int( (double(rand()) / RAND_MAX) * n)

    for numbers from 0 to n-1. Note this does have a miniscule but nonzero chance (0.00000005% for 31 bits of randomness) of returning n itself, so a serious application would have to take this into account.
    It doesn't look like another method to me. It looks like the same method. I am obviously missing something.

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