435,030 Members | 2,277 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,030 IT Pros & Developers. It's quick & easy.

Random Number Not Seeding

 P: n/a I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr[],int n) { // const int random_number_range=1000000; const int random_number_range=100; int number,xcell; randomNumber rnd; for (xcell=0;xcell #include #include #include "../ftsoftds/include/d_random.h" #include "../ftsoftds/include/d_sort.h" // Contains selectionSort //#include "../ftsoftds/include/d_timer.h" #include "my_sort.h" // My sorting library. using namespace std; void printArray(string name,int arr[],int n) { int cell; cout< arr[j]) { // swap arr[i] and arr[j] int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } //////////////////////////////////////////////////////////////////////////////// void my_sort::fillArray(int arr[],int n) { // const int random_number_range=1000000; const int random_number_range=100; int number,xcell; randomNumber rnd; for (xcell=0;xcell
4 Replies

 P: n/a Jack wrote: I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr[],int n) { // const int random_number_range=1000000; const int random_number_range=100; int number,xcell; randomNumber rnd; for (xcell=0;xcell

 P: n/a Ahhh, your reference to 'srand' makes sense. Below is the code to 'd_random.h' which contains the 'randomNumber' class. Can you take a look at it and make any recommendations on possibly adding 'srand'? I'm not a hardcore C++ coder... Thanks. \$ cat d_random.h #include #include using namespace std; // generate random numbers class randomNumber { public: // initialize the random number generator randomNumber(long s = 0); // return a 32-bit random integer m, 1 <= m <= 2^31-2 long random(); // return a 32-bit random integer m, 0 <= m <= n-1, // where n <= 2^31-1 long random(long n); // return a real number x, 0 <= x < 1 double frandom(); private: static const long A; static const long M; static const long Q; static const long R; long seed; }; const long randomNumber::A = 48271; const long randomNumber::M = 2147483647; const long randomNumber::Q = M / A; const long randomNumber::R = M % A; randomNumber::randomNumber(long s) { if (s < 0) s = 0; if (s == 0) { // get time of day in seconds since 12:00 AM, // January 1, 1970 long t_time = time(NULL); // mix-up bits by squaring t_time *= t_time; // result can overflow. handle cases // > 0, < 0, = 0 if (t_time > 0) s = t_time ^ 0x5EECE66DL; else if (t_time < 0) s = (t_time & 0x7fffffff) ^ 0x5EECE66DL; else s = 0x5EECE66DL; } seed = s; } long randomNumber::random() { long tmpSeed = A * ( seed % Q ) - R * ( seed / Q ); if( tmpSeed >= 0 ) seed = tmpSeed; else seed = tmpSeed + M; return seed; } long randomNumber::random(long n) { double fraction = double(random())/double(M); return int(fraction * n); } double randomNumber::frandom() { return double(random())/double(M); } Aug 17 '05 #3

 P: n/a Jack wrote: Ahhh, your reference to 'srand' makes sense. Below is the code to 'd_random.h' which contains the 'randomNumber' class. Can you take a look at it and make any recommendations on possibly adding 'srand'? AFAICS, there is no need for it. The constructor argument plays the seed role. Construction of an object with the default argument value (zero, 0) seeds the generator with the time, any other positive value is kept as the seed. I still think you should have a global object of type 'randomNumber' in your program to keep the sequence instead of restarting it every time when your function 'fillArray' is called. It is quite possible that while creating new 'randomNumber' object _might_ generate the sequence of pseudo-random numbers different every time, if all calls happen to be within the same second (which is not unreasonable in our times, when computers are fast), then the sequence generated would be exactly the same for every call to 'fillArray'. I recommend just modifying your definition of 'rnd' object in your 'fillArray' function to 'static': void my_sort::fillArray(int arr[],int n) { // const int random_number_range=1000000; const int random_number_range=100; int number,xcell; static randomNumber rnd; ... Victor I'm not a hardcore C++ coder... Thanks. Neither am I, really... No, honestly, I am not... I swear! [..] Aug 17 '05 #4

 P: n/a Dude! Sweet! The "static" did the trick... Now I have to read what that's doing :) . What you said about all calls happening in the same second made sense too. So I put a few second loop between two fills, and what do you know... Random data... Most cool! Aug 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion. 