how is the distribution of two consecutive numbers?
(all consecutive numbers should be nearly equally distributed, otherwise there must be clusters or local minima or maxima or hyperplane behaviors).
This is the test code:
Code: Select all
// Test on statistical equal distributionof (n, n+1)
// of Lego and customized randomization functions
// ver. 1.1
#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
//********************************************
//-------------------------------------------------------
// Kernighan & Ritchie LCG (linear congruence generator)
//-------------------------------------------------------
// global variables and functions to plant a random seed and to randomize
// how to use:
// sKrand(x) // x = val. for repet. sequences
// myVar = Krand() % 100; // assignes a random number
unsigned long _RAND_SEED_ = 1; // 1= default value (program start)
unsigned long _OLD_SEED_ = 1;
//--------------------------------------------
unsigned long Krand() // custom random function
{
_RAND_SEED_ = _RAND_SEED_ * 1103515245 + 12345;
return (_RAND_SEED_ % (RAND_MAX + 1));
}
//--------------------------------------------
void sKrand(unsigned long seed) // seeds for a new random series
{
if (seed==0) // 0: a "real" randomized random seed
{
seed = ( 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
}
//---------------------------------------------------------
// Mersenne Twister by Makoto Matsumoto & Takuji Nishimura
//---------------------------------------------------------
const int N_MTW = 25;
const int M_MTW = 7;
const unsigned long A_MTW[] = {0, 0x8ebfd028};
unsigned long y_MTW[25];
int i_MTW;
char virgin_MTW=TRUE, new_MTW=FALSE;
void InitMTW() {
i_MTW = N_MTW+1;
}
//--------------------------------------------
unsigned long sMrand(unsigned long x) {
unsigned long r, s;
int k;
if (x==1) InitMTW();
else {
InitMTW();
if (x==0) {x=Random(CurrentTick());}
for (k=0 ; k<25 ; ++k) {
InitMTW();
r = 9+x;
s = 3402+x;
for (k=0 ; k<25 ; ++k) {
r = 509845221 * r + 3;
s *= s + 1;
y_MTW[k] = s + (r >> 10);
}
new_MTW=TRUE;
}
}
}
//--------------------------------------------
unsigned long Mrand(void) {
unsigned long r, s, e;
int k;
if (virgin_MTW) {InitMTW(); virgin_MTW=FALSE; }
if (i_MTW >= N_MTW) {
if ((i_MTW > N_MTW) && !new_MTW) {
r = 9;
s = 3402;
for (k=0 ; k<N_MTW ; ++k) {
r = 509845221 * r + 3;
s *= s + 1;
y_MTW[k] = s + (r >> 10);
}
}
for (k=0 ; k<N_MTW-M_MTW ; ++k)
y_MTW[k] = y_MTW[k+M_MTW] ^ (y_MTW[k] >> 1) ^ A_MTW[y_MTW[k] & 1];
for (; k<N_MTW ; ++k)
y_MTW[k] = y_MTW[k+(M_MTW-N_MTW)] ^ (y_MTW[k] >> 1) ^ A_MTW[y_MTW[k] & 1];
i_MTW = 0;
}
new_MTW=false;
e = y_MTW[i_MTW++];
e ^= (e << 7) & 0x2b5b2500;
e ^= (e << 15) & 0xdb8b0000;
e ^= (e >> 16);
return e;
}
//--------------------------------------------
task main(){
long i, j, old;
long H[16]; // prevalence counter array
float x;
sKrand(2); sMrand(2);
// print output matrix
for (j=0; j<16; ++j) {
printf1(0+(j%2)*50, LCDline[j/2], "%2d:", j);
}
for (i=-1; i<= (1024 << 7); ++i) {
// 128 k randomizations of numbers between 0...15
j=rand()%16; // std Lego FW rand
//j=Random(16); // NXC John Hansen rand
//j=Krand()%16; // Kernighan and Ritchie LCG rand
//j=Mrand()%16; // Mesenne Twister rand
if ((j==old+1) || ((j==0 && old==15)))
if (i>=0) H[j]+=1; // add +1 to the prevalence to the related counter
old=j;
// print prevalence test distribution
printf1(16+(j%2)*50,LCDline[j/2], "%5d", H[j]);
}
PlaySound(SOUND_FAST_UP);
while(!ButtonPressed(BTNCENTER, true));
ClearScreen();
printf1(0,56, "count=%8d", i+1); // !!
j=ArrayMin (H, NA, NA); printf1(0,48, "min =%8d", j);
j=ArrayMax (H, NA, NA); printf1(0,40, "max =%8d", j);
j=ArrayMean(H, NA, NA); printf1(0,32, "mean =%8d", j);
x=ArrayStd (H, NA, NA); printf1(0,24, "stand=%8.2f", x);
while(1) {}
}