C++/生成随机数列

来源:百度知道 编辑:UC知道 时间:2024/04/28 23:38:56
用线性同余法生成随机数序列的公式为:
R(k)=(multiplier*R(k-1)+increment)%modulus
请解释一下这个怎么用?

线性同余的具体方法,你不必了解,因为有一个函数己经帮你实现了。用rand()。

#include <iostream.h>
#include <stdio.h>
#include <time.h>
main()
{int a[10],b,c,d;
srand(time(0));
for (b=0;b<10;b++)
{
a[b]=rand()%100;
}
for (b=0;b<10;b++)
{d=0;
for (c=2;c<a[b];c++)
{
if (a[b]%c==0) d=1;
}
if (d==0) cout<<a[b]<<endl;
}
}