产生(a, b)区间上均匀分布的随机数
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <math.h>
#include <ctype.h>
double uniform(double a, double b, long int *seed)
{
double t;
*seed = 2045 * (*seed) + 1;
*seed = *seed - (*seed/1048576) * 1048576;
t = (*seed)/1048576.0;
t = a + (b - a) * t;
return t;
}
int main(int argc , char **argv)
{
double a, b, x ;
int i, j;
long int s;
a = 0.0; b = 1.0; s = 13576;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 5; j++)
{
x = uniform(a, b , &s);
printf("%13.7f", x);
}
printf("\n");
}
return 0;
}
0.4767847 0.0247250 0.5625448 0.4041634 0.5140734
0.2800465 0.6950178 0.3114319 0.8782053 0.9298382
0.5190802 0.5189323 0.2166414 0.0317173 0.8618803
0.5452194 0.9737177 0.2526760 0.7224417 0.3932228
0.1406450 0.6190815 0.0216627 0.3002472 0.0055094
0.2666759 0.3523169 0.4879723 0.9032717 0.1905766
0.7290525 0.9124527 0.9657679 0.9952755 0.3383932
0.0141182 0.8717089 0.6446400 0.2887373 0.4677734
0.5966806 0.2119122 0.3603582 0.9325981 0.1631441
0.6297092 0.7554045 0.8021469 0.3904352 0.4400234