In a statistics book there is an algorithm of generating pseudo-random numbers with uniform distribution [0,1]. It says:
I. Enter an initial variable X positive integer.
II. Multiply X by variable "a", which should be at least 5 digits long.
III. Divide a*X by value p (positive integer).
IV. Take the fraction part of the division as a first pseudo-random number.
V. Make the number from step IV into integer by a necessary multiplication and use it in step II.
VI. Step II-V are repeated to generate more pseudo-random numbers.
Here is what I have:
How do I realize steps 4 and 5? I need to obtain random numbers between 0 and 1 and later use them in some other calculations. Please, help.Code:#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int x,a,p; cout << "Enter initial positive integer number: "; cin >> x; cout << "Enter positive integer a: "; cin >> a; cout << "Enter positive integer p: "; cin >> p; for(int i=1; i<=12; i++) { x=(a*x % p); cout << x << endl; } system("PAUSE"); return EXIT_SUCCESS; }




Reply With Quote