Well, to begin with, random number generation using rand isn't truly random, which is why you might experience these oddities.
after running this code for a while, the average distance started to get closer and closer to 0, ultimately it hovers ±2 around 0.
Code: Select all
TEMPLATE_INLINE V RND( V Min, V Max )
{
return static_cast< V >( static_cast< double >( rand( ) ) / static_cast< double >( RAND_MAX ) * ( static_cast< double >( Max ) - static_cast< double >( Min ) + 1.0 ) + static_cast< double >( Min ) );
}
-----------------------------------------------------------------------------------------------
static int average = 0;
static int times = 0;
int position = 0;
repeat( 100 )
{
position = 0;
for ( int i = 0; i < 1000000; i++ )
{
int a = RND( 0, 9 );
if ( a < 5 ) position--;
else if ( a >= 5 ) position++;
}
average += position;
times ++;
}
text.render( PointD( 20, 185 ), 0xFFFFFFFF, "distance %i", position );
text.render( PointD( 20, 210 ), 0xFFFFFFFF, "average %f, times %i", double( average ) / double( times ), times );
Code: Select all
average -1.306078, times 10200 ( thats 10 200 000 000 steps )
The position ( or distance ) after each pass may vary from ±20 to ±1500, but on average the player is near 0.