I wrote a litte program with which you may observe the distibution of the random numbers.
The numbers are randomized from 0...15 and their distribution is displayed on the screen over the time.
You may freeze the screen pressing any button (ExitBtn not too long!) ;)
Enjoy it!
Code: Select all
//*****************************************************************
// math.h random functions
// global variable to plant the random seed
unsigned long RAND_SEED = 1; // to be patched, e.g. by srand()
// random function, only positive values
unsigned int rand_()
{
RAND_SEED = RAND_SEED * 1103515245 + 12345;
return (RAND_SEED % (RAND_MAX + 1));
}
// random seed for a new random series
void srand(int seed)
{
RAND_SEED = seed; // just patches the global RAND_SEED variable
}
//*****************************************************************
// nxtio.h string and i/o functions
#define printf1( _x, _y, _format1, _value1) { \
string sval1 = FormatNum(_format1, _value1); \
TextOut(_x, _y, sval1); \
}
bool btnhit(){
return ( ButtonPressed(BTN1, false) || ButtonPressed(BTN2, false)
|| ButtonPressed(BTN3, false) || ButtonPressed(BTN4, false));
}
char LCDline[]={56,48,40,32,24,16,8,0};
//*****************************************************************
task main(){
int myRand, ibuf, x, y;
int RandDistr[16];
SetLongAbort(1);
srand(1);
// or any other value, e.g. using a timer counter like
srand(CurrentTick()*BatteryLevel());
printf1( 0,LCDline[0],"seed=%d",RAND_SEED);
printf1( 0,LCDline[3],"%s", "press any btn");
while(!btnhit());
ClearScreen();
for (char i=0; i<8; i++) {
printf1( 0,LCDline[i],"%2d",2*i);
printf1(46,LCDline[i],"|%2d",2*i+1);
}
while (RandDistr[myRand]<9999) {
myRand=rand_() % 16; // 0...15
RandDistr[myRand]++;
ibuf=RandDistr[myRand];
x=(myRand%2)*50;
y=myRand/2;
printf1(22+x,LCDline[y],"%4d",ibuf);
while(btnhit()); // wait while button pressed for screen freezing
}
while(true);
}