Uniform Generator统一发电机 13

来源:互联网 发布:灭茶苦茶 知乎 编辑:程序博客网 时间:2024/04/27 21:43

Uniform Generator统一发电机

Problem Description

Computersimulations often require random numbers. One way to generate pseudo-randomnumbers is via a function of the form
              seed(x+1) = [seed(x) +STEP] % MOD
where '%' is the modulus operator. 
Such a function will generate pseudo-random numbers (seed) between 0 and MOD-1.One problem with functions of this form is that they will always generate thesame pattern over and over. In order to minimize this effect, selecting theSTEP and MOD values carefully can result in a uniform distribution of allvalues between (and including) 0 and MOD-1. 
For example, if STEP = 3 and MOD = 5, the function will generate the series ofpseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In this example, allof the numbers between and including 0 and MOD-1 will be generated every MODiterations of the function. Note that by the nature of the function to generatethe same seed(x+1) every time seed(x) occurs means that if a function willgenerate all the numbers between 0 and MOD-1, it will generate pseudo-randomnumbers uniformly with every MOD iterations. 
If STEP = 15 and MOD = 20, the function generates the series 0, 15, 10, 5 (orany other repeating series if the initial seed is other than 0). This is a poorselection of STEP and MOD because no initial seed will generate all of thenumbers from 0 and MOD-1. 
Your program will determine if choices of STEP and MOD will generate a uniformdistribution of pseudo-random numbers. 

计算机模拟通常需要的随机数。以产生伪随机数的一种方法是通过以下形式的功能

              种子(X + 1)= [种子(x)的+步骤]%MOD

其中,“%”是模运算符。

这样的功能将生成在0和MOD-1的伪随机数(种子)。用这种形式的功能的一个问题是,它们将一直反复产生相同的图案。为了减少这种影响,在选择步骤和MOD值仔细可导致所有值之间(并包括)0和MOD-1的均匀分布。

例如,如果步骤= 3和MOD = 5,则该函数将生成一系列伪随机数0,3,1,4,2的以重复周期。在本实施例中,所有的数字之间并包括0和MOD-1将产生的功能的每MOD迭代。注意,通过该函数的性质,以产生相同的种子(X + 1)每次种子(x)的发生意味着,如果一个函数将生成在0和MOD-1的所有数字,就会产生伪随机数一致每MOD迭代。

如果步骤= 15和MOD = 20,该函数产生0系列,15,10,5(或如果所述初始种子是0以外的任何其他重复系列)。这是一个糟糕的选择步骤和MOD因为没有初始种子将产生所有的数字从0和MOD-1。

程序将确定是否步骤和MOD的选择将产生的伪随机数的均匀分布。

 Input

Each line of inputwill contain a pair of integers for STEP and MOD in that order (1 <= STEP,MOD <= 100000).

输入的每一行包含一个整数对用于STEP和MOD以该顺序(1 <= STEP,MOD<=100000)。

 Output

For each line ofinput, your program should print the STEP value right- justified in columns 1through 10, the MOD value right-justified in columns 11 through 20 and either"Good Choice" or "Bad Choice" left-justified starting incolumn 25. The "Good Choice" message should be printed when theselection of STEP and MOD will generate all the numbers between and including 0and MOD-1 when MOD numbers are generated. Otherwise, your program should printthe message "Bad Choice". After each output test set, your programshould print exactly one blank line.

对于输入的每一行,你的程序应该打印步长值右对齐列1到10,国防部价值20,要么“不错的选择”或“糟糕的选择”右对齐的11列左对齐列首发应打印25.“好选择”消息时,STEP和MOD的选择将生成之间并包括0和MOD-1时产生的MOD号码的所有数字。否则,你的程序应该打印的信息:“选择”。每个输出测试集之后,你的程序应该打印正好有一个空行。

Sample Input

3 5

15 20

63923 99999

 

Sample Output

        3         5    Good Choice

 

       15        20    Bad Choice

 

    63923     99999    Good Choice

代码如下:

#include <stdio.h>#include <stdlib.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */#include <string.h>  int visit[100005];  int main() {      int step,mod,seed;           while(scanf("%d%d",&step,&mod)!=EOF) {           memset(visit,0,sizeof(visit));//初始化一个数组元素           visit[0]=1;          seed=0;         int i;         for(i=1;i<mod;++i) {             seed=(seed+step)%mod;             if(visit[seed])             break;             visit[seed]=1;         }         if(i==mod)         printf("%10d%10d    Good Choice\n\n",step,mod);         else         printf("%10d%10d    Bad Choice\n\n",step,mod);     }     return 0; }


 

0 0
原创粉丝点击