random number generator

This is a discussion on random number generator within the C++ Programming forums, part of the General Programming Boards category; After searching the net for an hour to find a good random number generator, this is the best I found. ...

  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
    22,268
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 03:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 06:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21