Thread: I didnt notice this about random :(

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    I do something completely different-
    I include <cstdlib>, which has a rand() function, and include<ctime>.
    I then use the clock() function to execute rand() many times based on how many clock ticks have passed since the program started, which puts me into an entirely random place in the rand()'s random number list.

    I had no idea about srand() and all that.

    Anyway; cstdlib's rand() doesn't go up in value every time, and a way of making a reliable min and max is by dividing the random number by RAND_MAX, multiplying by (MAX-MIN) and adding MIN.

  2. #17
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    rand() is not sufficient for most programs. There are several algos available on the internet and in books that are far better and quite simple to implement.

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Bubba View Post
    rand() is not sufficient for most programs. There are several algos available on the internet and in books that are far better and quite simple to implement.
    The C standard mandates no specific algorithm for rand(). I don't think it's justified to claim that rand() is "not sufficient" when it isn't even fully specified how it works.

    I wouldn't use rand() for anything that had statistical significance, since I have no idea how it works (nor should I rely on such knowledge since it can change at any time). That doesn't mean it sucks by default, though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Anyway, C++ TR1 and C++0x define an extensive library of random number generators that are suitable for serious statistical use. Boost.Random is the library the new standard library is based on, but there are some changes. So if your compiler doesn't provide a TR1 implementation, you can use Boost.Random.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #20
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Eldarion View Post
    I do something completely different-
    I include <cstdlib>, which has a rand() function, and include<ctime>.
    I then use the clock() function to execute rand() many times based on how many clock ticks have passed since the program started, which puts me into an entirely random place in the rand()'s random number list.

    I had no idea about srand() and all that.

    Anyway; cstdlib's rand() doesn't go up in value every time, and a way of making a reliable min and max is by dividing the random number by RAND_MAX, multiplying by (MAX-MIN) and adding MIN.
    Are you sure there is any guarantee that clock() will return values that are random enough.

    Anyway, if you keep giving ascending values to srand, there's no surprise that you might get ascending values from the next call to rand(). The whole point of rand() is that there is a long-long randomish mathematical sequence and srand() determines your starting point in the sequence.

    This doesn't mean that the first number you get after seeding is particularly unpredictable, e.g

    Code:
    #include <cstdio>
    #include <cstdlib>
    
    int main()
    {
        for (int seed = 32; seed <= 42; ++seed) {
            srand(seed);
            printf("%d %d\n", seed, rand());
        }
    }

    32 143
    33 146
    34 149
    35 152
    36 156
    37 159
    38 162
    39 165
    40 169
    41 172
    42 175
    Also see how the (pseudo)randomness kicks in if you draw more random numbers from a seed:
    Code:
    #include <cstdio>
    #include <cstdlib>
    
    int main()
    {
        for (int seed = 32; seed <= 42; ++seed) {
            srand(seed);
            printf("%d", seed);
            for (int i = 0; i != 8; ++i) {
                printf(" %6d", rand());
            }
            puts("");
        }
    }
    Code:
    32    143  23988   3067  18799  11241   6380   9660   4148
    33    146   1968  20932  10094  21556  10307  12772   1221
    34    149  12717   6028   1389  31870  14234  15884  31062
    35    152  23465  23892  25453   9417  18160  18997  28135
    36    156   1446   8988  16748  19731  22087  22109  25207
    37    159  12194  26852   8043  30046  26014  25222  22280
    38    162  22942  11948  32107   7593  29941  28334  19353
    39    165    923  29813  23402  17907   1099  31447  16426
    40    169  11671  14909  14697  28222   5026   1791  13498
    41    172  22420      5   5992   5768   8953   4904  10571
    42    175    400  17869  30056  16083  12879   8016   7644
    Last edited by anon; 02-24-2009 at 05:41 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM