主题:关于随机数的问题
我编写了一个小程序计算随机数,输入参数相同只能计算出一个随机数而不是c语言中订输入参数相同却能得到不同随机数,为什么?
using System;
using System.Collections.Generic;
using System.Text;
namespace chi01_1
{
class Program
{
static double uniform(double a,double b,int seed)
{
double t;
seed = 2045 * seed + 1;
seed = seed - (seed / 1048576) * 1048576;
t = seed/1048576.0;
t = a + (b -a) * t;
return t;
}
static void Main(string[] args)
{
double aa, bb, x;
int i, j;
int [] s= {13578,13579,13546,12678,20157};
aa = 0.0;
bb = 1.0;
//s = {13578,13579,13546,12678,20157};
x = 0.0;
//
for (i = 0; i < 10; i++)
{
for (j = 0; j < 5; j++)
{
x = uniform(aa, bb, s[j]);
Console.WriteLine("{0}",x);
}
Console.WriteLine("\n");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace chi01_1
{
class Program
{
static double uniform(double a,double b,int seed)
{
double t;
seed = 2045 * seed + 1;
seed = seed - (seed / 1048576) * 1048576;
t = seed/1048576.0;
t = a + (b -a) * t;
return t;
}
static void Main(string[] args)
{
double aa, bb, x;
int i, j;
int [] s= {13578,13579,13546,12678,20157};
aa = 0.0;
bb = 1.0;
//s = {13578,13579,13546,12678,20157};
x = 0.0;
//
for (i = 0; i < 10; i++)
{
for (j = 0; j < 5; j++)
{
x = uniform(aa, bb, s[j]);
Console.WriteLine("{0}",x);
}
Console.WriteLine("\n");
}
}
}
}