Re: rand doesn't seem to generate random numbers
 
golnar wrote:
function rand seems to generate the same number all the time. What
should I do?
As others have pointed out, you need to seed the random generator.  The 
benefit is that saving a particular seed allows you to reproduce the 
entire sequence of random numbers that grow from it.  That's 
particularly useful if you find a sequence of randoms that evinces a bug 
in your code.  Saving the seed lets you use the sequence in your 
regression suite.
I don't think most people like to admit they do this (and will probably 
throw rocks at me for suggesting it), but the quick and dirty way to 
generate a seed is to use the current time.  I also like to generate one 
throw-away number right after seeding, because the first number 
sometimes doesn't vary much for close seeds.
OK, I've just slapped down some code to help you get a feel for how the 
seed affects the random number sequence on your platform.  On the 
Windows XP box I'm using right now, running five iterations for each of 
ten consecutive seeds, I get the following results:
         seed           0           1           2           3           4
------------------------------------------------------------------------
   1157498310          41       18467        6334       26500       19169
   1157498311       15724       11478       29358       26962       24464
   1157498312        5705       28145       23281       16827        9961
   1157498313         491        2995       11942        4827        5436
   1157498314       32391       14604        3902         153         292
   1157498315       12382       17421       18716       19718       19895
   1157498316        5447       21726       14771       11538        1869
   1157498317       19912       25667       26299       17035        9894
   1157498318       28703       23811       31322       30333       17673
   1157498319        4664       15141        7711       28253        6868
Here's the code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <string>
template<
     typename seed_type, /* E.g. std::time_t. */
     unsigned count,     /* Desired number of calls to rand(). */
     unsigned width>     /* Column width of output. */
class srand_test {
public:
     typedef unsigned count_type;
     typedef unsigned width_type;
     /* Print column headers and underline them. */
     static void print_header() {
         std::cout << std::setw(width) << "seed";
         for(count_type i = 0; i < count; ++i) {
             std::cout << std::setw(width) << i;
         }
         std::string underline(width * (count + 1), '-');
         std::cout << '\n' << underline << '\n';
     }
     /* Print the seed, followed by the results of 'count'
      * consecutive calls to std::rand(). */
     void operator()(seed_type seed) {
         std::cout << std::setw(width) << seed;
         for(count_type i = 0; i < count; ++i) {
             std::cout << std::setw(width) << std::rand();
         }
         std::cout << '\n';
     }
};
int main() {
     /* Run the same test ten times, but with a slightly different
      * seed each time. */
     typedef srand_test<std::time_t, 5, 12> test_type;
     test_type::print_header();
     test_type test;
     std::time_t seed = std::time(0);
     for(unsigned i = 0; i < 10; ++i) {
         test(seed + i);
     }
}