I now have tested the random number generator of the Lego FW statistically by 1 million randomizations of numbers between 0-15 (inclusive)
The result is shocking:
Some numbers are generated almost twice as often as others, and, worse, they are always even the same at different test runs : this is the test code:
Code: Select all
// Test on statistical equal distribution of Lego and customized randomization
#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
char LCDline[]={56,48,40,32,24,16,8,0};
task main(){
long i, j;
long H[16]; // prevalence counter array
// print output matrix
for (j=0; j<16; ++j) {
printf1(0+(j%2)*50, LCDline[j/2], "%2d:", j);
}
//srand(1); // standard seed (only for customized randomization function)
for (i=0; i<1000000; ++i) {
// 1 Million randomizations of numbers between 0...15
j=rand()%16; // std Lego FW randomization function
// alternatively:
//j=rand_()%16; // customized randomization function
H[j]+=1; // add +1 to the prevalence to the related counter
// print prevalence test distribution
printf1(16+(j%2)*50,LCDline[j/2], "%5d", H[j]);
}
PlaySound(SOUND_FAST_UP);
while(1) {}
}
Code: Select all
// Test on statistical equal distribution
//of Lego and customized randomization functions
#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
char LCDline[]={56,48,40,32,24,16,8,0};
//********************************************
// randomize functions
//********************************************
// global variables and functions to plant a random seed and to randomize
// how to use:
// srand(x) // x = val. for repet. sequences
// myVar = rand_() % 100; // assignes a random number
unsigned long _RAND_SEED_ = 1; // 1= default value (program start)
unsigned long _OLD_SEED_ = 1;
//--------------------------------------------
unsigned long rand_() // random function
{
_RAND_SEED_ = _RAND_SEED_ * 1103515245 + 12345;
return (_RAND_SEED_ % (RAND_MAX + 1));
}
//--------------------------------------------
void srand(unsigned long seed) // seeds for a new random series
{
if (seed==0) // 0: a "real" randomized random seed
{seed = abs(CurrentTick()*BatteryLevel());} // substitute to time(0)
else
if (seed==-1) { // -1: restore last random series
seed = _OLD_SEED_;
}
_OLD_SEED_ = seed;
_RAND_SEED_ = seed; // patch for rand_ function
}
//--------------------------------------------
task main(){
long i, j;
long H[16]; // prevalence counter array
// print output matrix
for (j=0; j<16; ++j) {
printf1(0+(j%2)*50, LCDline[j/2], "%2d:", j);
}
// (only for customized randomization function)
srand(1); // reproducible standard seeds 1 (2, 3, 4, ...)
// alternatively:
//srand(0); // randomized random seed (time(0)*Voltage)
for (i=0; i<1000000; ++i) {
// 1 Million randomizations of numbers between 0...15
//j=rand()%16; // std Lego FW randomization function
// alternatively:
j=rand_()%16; // customized randomization function
H[j]+=1; // add +1 to the prevalence to the related counter
// print prevalence test distribution
printf1(16+(j%2)*50,LCDline[j/2], "%5d", H[j]);
}
PlaySound(SOUND_FAST_UP);
while(1) {}
}