[c++]代码库
#include <stdio.h>
#include <time.h>
static unsigned int s_seed = 0;
void random_seed ( int seed )
{
if ( !seed ) s_seed=time ( NULL );
else s_seed=seed;
}
int random_int ( int min, int max )
{
s_seed=214013*s_seed+2531011;
return min+ ( s_seed ^ s_seed>>15 ) % ( max-min+1 );
}
float random_float ( float min, float max )
{
s_seed=214013*s_seed+2531011;
return min+ ( s_seed>>16 ) * ( 1.0f/65535.0f ) * ( max-min );
}
int main()
{
/* initialize random seed: */
random_seed ( 0 );
printf ( "random_int(20,9393) = %d\n",random_int ( 20,9393 ) );
printf ( "random_float(20.0,9393.0) = %f\n",random_float ( 20.0f,9393.0f ) );
return 0;
}