Thread: Two questions regarding srand

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    Two questions regarding srand

    Hi everyone,

    From jumping into C++, there is this code:

    Code:
    #include <ctime>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int randRange (int low, int high)
    {
        // we get a random number, get it to be between 0 and the difference
        // between high and low, then add the lowest possible value
        return rand() % ( high - low ) + low;
    }
    
    int main ()
    {
        srand( time( NULL ) ); //warning here - explained below
        for ( int i = 0; i < 1000; ++i )
        {
            cout << randRange(4, 10) << '\n';
        }
    }
    I understand, without srand, the numbers always output the same (I tested). The book says this:

    C++ has two functions, one for setting a random seed, and another for generating random numbers using the seed:
    then...

    As it turns out, rand will return a value between 0 and a constant called RAND_MAX (which will be at least 32767)
    My questions are:

    1) If rand() is going to generate a number up to (at least) 32767, what's the point of the seed? What does it do? The book doesn't explain this.

    2) I get a warning 'Implicit conversion loses integer precision:time_t (aka 'long') to 'unsigned int' on line 16. I have no idea what this means. I tried using time_t instead but, although the warning vanishes, the numbers output are not random.

    Thanks for any input.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    A good PRNG eventually navigates through all possible valid state.

    It can easily be argued that a PRNG is essentially giving you a window, a few bits, into a view of a massive number while the algorithm navigates the massive number.

    The seed, following the same idea, governs where the algorithm starts processing bits in the massive number.

    So, if you like, you can view the seed as deciding from which digit to start reading a, for example, ten billion digit number. (The size of the number depends on the algorithm.)

    Soma

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Something like this?

    srand = 16,867,565,532,618,582,454,987,265

    So on first run:

    srand = 16,867,565,532,618,582,454,987,265

    54,987 being returned as the random number from rand(). And on the next run:

    16,867,565,532,618,582,454,987,265

    8675 being returned as the random number from rand(). And so on...

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    No.

    The abstraction I'm suggestion that you use to consume PRNG plays to three parts.

    Code:
    BIG NUMBER (The sample value of all possible state mutations strung together.) := 0123456789
    SEED (The starting point in reading all sample values.) := 2
    RESULT-0 (The sample value, which is the pseudo-random value, the interface returns.) := 2
    RESULT-1 (The sampling algorithm has mutated the current state.) := 3
    RESULT-2 (The sampling algorithm has mutated the current state.) := 4
    The "BIG NUMBER" is always the same. Here, it is an exaggeration thanks to being just an example. It would not be a simple incremented string of decimal digits.

    The "SEED" we initialize the algorithm with just decides where in the "BIG NUMBER" we start reading values. Here this is an exaggeration because the "SEED" isn't a simple index; the mathematics involved can be very sophisticated.

    Code:
    BIG NUMBER (The sample value of all possible state mutations strung together.) := 0123456789
    SEED (The starting point in reading all sample values.) := 8
    RESULT-0 (The sample value, which is the pseudo-random value, the interface returns.) := 8
    RESULT-1 (The sampling algorithm has mutated the current state.) := 9
    RESULT-2 (The sampling algorithm has mutated the current state.) := 0
    The size of "BIG NUMBER" can be trillions of trillions of trillions of digits. (Some of the best algorithms navigate a number unfathomably large.)

    The "SEED" can be tens or hundreds of thousands of digits. (The algorithm here is just a single value. There are other algorithms where the seed is an array hundreds of thousands of elements long.)

    The "RESULT" is generally machine sized.

    Soma

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Aha, that makes more sense, thanks for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With Srand
    By BrandNew in forum C Programming
    Replies: 3
    Last Post: 02-24-2011, 02:02 AM
  2. help with srand()
    By ~C_Student~ in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 10:55 AM
  3. srand()
    By bikr692002 in forum C++ Programming
    Replies: 35
    Last Post: 09-03-2006, 09:13 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand help
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-15-2001, 09:14 PM