线性同余法产生随机数序列的公式为:
x[k+1] = (a*x[k] + c) % m
参数 a, c, m 都是由用户来设定的,并和一个种子数(例如 x[0])一起作为命令行参数传递给生成的程序。
一个简单的例子:a=7, c=1, m=13, and seed=5
一个复杂的例子:a=69069, c=0, m=2^32=4294967296, seed=31
下面的代码将输出一个随机数序列(最多有 m-1 个不同的值),然后继续循环。
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <Windows.h>
- static long seed = 13;
- static long a;
- static long c;
- static long m;
- void random_init(long s) {
- if (s != 0) seed = s;
- }
- long random() {
- seed = (a*seed + c)%m;
- return seed;
- }
- int main(int argc, char * argv[]) {
- long s;
- int k;
- if (argc != 5) {
- printf("usage: %s a, c, m, seed\n", argv[0]);
- return 1;
- }
- a = atoi(argv[1]);
- c = atoi(argv[2]);
- m = atoi(argv[3]);
- s = atoi(argv[4]);
- random_init(s);
-
- for (k = 0; k < m-1; k++) {
- printf("%8ld", random());
- if (k % 8 == 7) { // 输出 8 个数字以后换行
- printf("\n");
- Sleep(1); // 暂停 1 秒
- }
- }
- printf("\n");
- return 0;
- }
构建成功后,需要到命令行(cmd.exe)运行该程序才能看到效果。这里涉及到 main() 函数的传参问题,请查看:C语言main()函数详解
比如,我的项目放在 E:\cDemo\ 目录下,在命令行中的输入截图如下:
注意:输入的参数个数(cDemo.exe也算一个参数)必须等于 5 程序才能正确执行。
上面的截图中,我给 m 传的值为 56,产生了 55( m-1 ) 个随机数。