Hi,
Maybe here is something like what you want to see.
Alex
//#########################################################
//------------------- C++ code : BEGIN -------------------
//=================
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
//=================
#include <iostream>
#include <iomanip>
#include <vector>
//==========================================
int get_limited_ordinary_random (
int lower_limit_i,
int upper_limit_i
)
{
const int coef_CNS = 19;
int ret_value;
int rand_num;
//----------------------------
assert (lower_limit_i <= upper_limit_i);
//----------------------------
rand_num = random ();
rand_num = rand_num/coef_CNS;
rand_num = (rand_num - 1)%(upper_limit_i - lower_limit_i + 1) +
1;
ret_value = lower_limit_i + rand_num - 1;
//----------------------------
assert (ret_value >= lower_limit_i);
assert (ret_value <= upper_limit_i);
//----------------------------
return (ret_value);
} // int get_limited_ordinary_random
//========================================
int get_limited_super_random (
int lower_limit_i,
int upper_limit_i
)
{
struct timeval tp;
struct timezone tzp;
unsigned long tseconds = 0;
unsigned long tmicroseconds = 0;
switch (gettimeofday (&tp, &tzp))
{
case 0 :
tseconds = tp.tv_sec;
tmicroseconds = tp.tv_usec;
break;
case -1 :
cout << "Cannot get time from <gettimeofday> : "
<< (strerror (errno))
<< endl;
assert (0);
break; // unused
default :
assert (0);
break; // unused
} // switch (gettimeofday (&tp, &tzp))
//==========================================
srandom (static_cast<unsigned int> ((tseconds << 20) |
tmicroseconds));
// tmicroseconds take 5 bytes; so tseconds' shift is : 5 bytes *
4 bits
//==========================================
return get_limited_ordinary_random (lower_limit_i,
upper_limit_i);
//==========================================
} // int get_limited_super_random
//========================================
void test_random (
int number_of_calls_i,
int lower_limit_i,
int upper_limit_i
)
{
vector<int> vect_ordinary_random;
vector<int> vect_super_random;
//=============================
for (int i = 0; i < (upper_limit_i - lower_limit_i + 1); i++)
{
vect_ordinary_random.push_back (0);
vect_super_random.push_back (0);
}
//=============================
for (int i = 0; i < number_of_calls_i; i++)
{
vect_ordinary_random [get_limited_ordinary_random
(lower_limit_i, upper_limit_i) - lower_limit_i]++;
}
for (int i = 0; i < number_of_calls_i; i++)
{
vect_super_random [get_limited_super_random
(lower_limit_i, upper_limit_i) - lower_limit_i]++;
}
//=============================
cout << endl;
cout << "##################################" << endl;
cout << "######### Test ###################" << endl;
cout << "##################################" << endl;
cout << "\t: Ordinary" << "\t Super" << endl;
cout << "\t: --------" << "\t -----" << endl;
for (int i = 0; i < (upper_limit_i - lower_limit_i + 1); i++)
{
cout << "["
<< (i + lower_limit_i)
<< "] \t: "
<< setw (9)
<< vect_ordinary_random [i]
<< "\t "
<< setw (9)
<< vect_super_random [i]
<< endl;
}
cout << "##################################" << endl;
cout << endl;
//=============================
int ordinary_sum = 0;
int super_sum = 0;
for (int i = 0; i < (upper_limit_i - lower_limit_i + 1); i++)
{
ordinary_sum += vect_ordinary_random [i];
super_sum += vect_super_random [i];
}
//===============================
assert (ordinary_sum == super_sum);
assert (ordinary_sum == number_of_calls_i);
//===============================
} // void test_random (
//========================
//========================
int main (int argc, char **argv)
{
const int number_of_random_numbers_CNS = 5;
const int lower_limit_CNS =
10;
const int upper_limit_CNS = 20;
if (argc == 1)
{
// Small test
//=====================
cout << endl;
cout << "\t===== Ordinary random =====" << endl;
for (int i = 0; i < number_of_random_numbers_CNS; i++)
{
cout << get_limited_ordinary_random (
lower_limit_CNS,
upper_limit_CNS
)
<< endl;
}
cout << endl;
cout << "\t===== Super random ========" << endl;
for (int i = 0; i < number_of_random_numbers_CNS; i++)
{
cout << get_limited_super_random (
lower_limit_CNS,
upper_limit_CNS
)
<< endl;
}
cout << endl;
//=======
return 0;
//=======
} // if (argc == 1)
//=====================
// Big test
test_random (
(10000 * (upper_limit_CNS - lower_limit_CNS + 1)),
lower_limit_CNS,
upper_limit_CNS
);
//=====================
return 0;
}
//------------------- C++ code : END ----------------------
//#########################################################
//------------------- Running Results : BEGIN -------------
//------------------- Running Results : END ---------------
// Running#A.1 (Small test)
%a.out
===== Ordinary random =====
15
18
20
18
17
===== Super random ========
13
11
18
14
10
// Running#A.2 (Small test)
%a.out
===== Ordinary random =====
15
18
20
18
17
===== Super random ========
17
18
14
13
16
// Running#A.3 (Small test)
%a.out
===== Ordinary random =====
15
18
20
18
17
===== Super random ========
19
16
11
18
13
// Running#B.1 (Big test)
%a.out never_mind
##################################
######### Test ###################
##################################
: Ordinary Super
: -------- -----
[10] : 9967 9856
[11] : 10081 9950
[12] : 9942 10014
[13] : 10081 10007
[14] : 9942 9889
[15] : 10148 10157
[16] : 9927 10246
[17] : 9968 10033
[18] : 9924 9925
[19] : 9998 9912
[20] : 10022 10011
##################################
// Running#B.2 (Big test)
%a.out never_mind
##################################
######### Test ###################
##################################
: Ordinary Super
: -------- -----
[10] : 9967 10052
[11] : 10081 9899
[12] : 9942 9826
[13] : 10081 10057
[14] : 9942 9912
[15] : 10148 9853
[16] : 9927 10002
[17] : 9968 10220
[18] : 9924 10107
[19] : 9998 9930
[20] : 10022 10142
##################################
// Running#B.3 (Big test)
%a.out never_mind
##################################
######### Test ###################
##################################
: Ordinary Super
: -------- -----
[10] : 9967 10027
[11] : 10081 10060
[12] : 9942 10076
[13] : 10081 9908
[14] : 9942 10077
[15] : 10148 10007
[16] : 9927 10015
[17] : 9968 9963
[18] : 9924 9939
[19] : 9998 9986
[20] : 10022 9942
##################################
//#########################################################
//------------------- Environment -------------------------
g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)
uname -sr : SunOS 5.6
//---------------------------------------------------------
//#########################################################
Sent via Deja.com http://www.deja.com/
Before you buy.
Alex
Actually it is unnecessary to call srandom every time.
We may do it, but we don't have to.
Here are three functions that enable to get limited int random :
get_ordinary_limited_int_random (),
get_once_seeded_limited_int_random (),
get_everytime_seeded_limited_int_random ().
Alex
//#########################################################
//------------------- C++ code : BEGIN -------------------
//=================
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>
//=================
#include <iostream>
//=================
const int SHIFT_VALUE = 20;
//------------------------------------------
// tmsec take 5 hexadecimal digits;
// so tsec' shift is : 5 * 4 bits = 20
//------------------------------------------
//==========================================
//==========================================
bool get_current_time (
unsigned long& tsec_o,
unsigned long& tmsec_o
)
{
struct timeval tp;
struct timezone tzp;
bool ret_boolValue = true;
tsec_o = 0;
tmsec_o = 0;
switch (gettimeofday (&tp, &tzp))
{
case 0 :
tsec_o = tp.tv_sec;
tmsec_o = tp.tv_usec;
break;
case -1 :
cout << "Cannot get time from <gettimeofday> : "
<< (strerror (errno))
<< endl;
ret_boolValue = false;
break;
default :
assert (0);
break; // unused
} // switch (gettimeofday (&tp, &tzp))
//------------------
return ret_boolValue;
//------------------
} // bool get_current_time
//==========================================
int get_ordinary_limited_int_random (
int lower_limit_i,
int upper_limit_i
)
{
const int coef_CNS = 19;
int ret_value;
int rand_num;
//----------------------------
assert (lower_limit_i <= upper_limit_i);
//----------------------------
rand_num = random ();
rand_num = rand_num/coef_CNS;
rand_num = (rand_num - 1)%(upper_limit_i - lower_limit_i + 1) +
1;
ret_value = lower_limit_i + rand_num - 1;
//----------------------------
assert (ret_value >= lower_limit_i);
assert (ret_value <= upper_limit_i);
//----------------------------
return (ret_value);
} // int get_ordinary_limited_int_random
//========================================
int get_once_seeded_limited_int_random (
int lower_limit_i,
int upper_limit_i
)
{
unsigned long tsec;
unsigned long tmsec;
static bool flag = true;
if (flag)
{
if (!get_current_time (tsec, tmsec))
{
assert (0);
}
srandom (static_cast<unsigned int> ((tsec <<
SHIFT_VALUE) | tmsec));
flag = false;
}
return get_ordinary_limited_int_random (
lower_limit_i,
upper_limit_i
);
} // int get_once_seeded_limited_int_random
//========================================
int get_everytime_seeded_limited_int_random (
int lower_limit_i,
int upper_limit_i
)
{
unsigned long tsec;
unsigned long tmsec;
if (!get_current_time (tsec, tmsec))
{
assert (0);
}
srandom (static_cast<unsigned int> ((tsec << SHIFT_VALUE) |
tmsec));
return get_ordinary_limited_int_random (
lower_limit_i,
upper_limit_i
);
} // int get_everytime_seeded_limited_int_random
//------------------- C++ code : END ----------------------
//#########################################################