Uniform Generator
Uniform Generator |
Computer simulations often require random numbers. One way to generatepseudo-random numbers is via a function of the form
where `` " is the modulus operator.
Such a function will generate pseudo-random numbers (seed) between 0 andMOD-1. One problem with functions of this form is that they will alwaysgenerate the same pattern over and over. In order to minimize thiseffect, selecting the STEP and MOD values carefully can result in auniform distribution of all values between (and including) 0 and MOD-1.
For example, if STEP = 3 and MOD = 5, the function will generate theseries of pseudo-random numbers 0, 3, 1, 4, 2 in a repeating cycle. In thisexample, all of the numbers between and including 0 and MOD-1 will begenerated every MOD iterations of the function. Note that by the natureof the function to generate the same seed(x+1) every time seed(x)occurs means that if a function will generate all the numbers between 0 andMOD-1, it will generate pseudo-random numbers uniformly withevery MOD iterations.
If STEP = 15 and MOD = 20, the function generates the series0, 15, 10, 5 (or any other repeating series if the initial seed is other than 0).This is a poor selection of STEP and MOD because no initial seedwill generate all of the numbers from 0 and MOD-1.
Your program will determine if choices of STEP and MOD willgenerate a uniform distribution of pseudo-random numbers.
Input
Each line of input will contain a pair of integers for STEPand MOD in that order ( ).
Output
For each line of input, your program should print the STEP value right-justified in columns 1 through 10, the MOD value right-justified incolumns 11 through 20 and either ``Good Choice" or ``Bad Choice"left-justified starting in column 25. The ``Good Choice" message shouldbe printed when the selection of STEP and MOD will generate allthe numbers between and including 0 and MOD-1 when MOD numbers aregenerated. Otherwise, your program should print the message ``Bad Choice".After each output test set, your program should print exactly one blank line.
Sample Input
3 5 15 20 63923 99999
Sample Output
3 5 Good Choice 15 20 Bad Choice 63923 99999 Good Choice
这道题的大意就是给予两个数,然后通过题中的公式找随机数,判断是否足够随机,即各个数都是否能得出。而对于这道题,本质上其实就是求这两个数是否是互质的,我是通过辗转相除法来做的。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100000;
int s[N];
int main() {
int Z,I,M,L;
int cnt = 0;
while (cin >> Z >> I >> M >> L) {
if (Z + I + M + L == 0)
break;
cnt++;
memset(s,0,sizeof(s));
s[L] = 1;
int t = 1;
while (1) {
L = (Z * L + I) % M;
if (s[L])
break;
t++;
s[L] = t;
}
cout << "Case " << cnt << ": " << ++t-s[L] << endl;
}
return 0;
}