Thread: random number generator

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    random number generator

    After searching the net for an hour to find a good random number generator, this is the best I found. From what I understand it works off of time as seconds and minutes change, but I would like to dive in further to understand the purpose of these 5 items...

    srand
    rand
    time
    NULL
    <ctime>


    Code:
    
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    
    {
    srand(time(NULL));
    cout << rand();
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian_Jones
    After searching the net for an hour to find a good random number generator, this is the best I found.
    Poor thing After you have read through the article that I linked to below, and have experimented with this a little more, take a look at what is offered by Boost.Random.

    Quote Originally Posted by Brian_Jones
    From what I understand it works off of time as seconds and minutes change
    That refers to the way that the PRNG is seeded, not the way that the PRNG actually works. The standard library's pseudorandom number generator's algorithm depends on the implementation.

    Quote Originally Posted by Brian_Jones
    but I would like to dive in further to understand the purpose of these 5 items...

    srand
    rand
    time
    NULL
    <ctime>
    srand is used to seed the PRNG, rand is used to get a random number in the range [0, RAND_MAX], std::time(NULL) is used to get the current time, NULL is a macro for zero, and <ctime> is the standard header for std::time. I suggest that you read Prelude's article 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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Thank you!

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