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:

#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;
}

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.

step 3 will have to use floating point numbers because one integer divided by another integer will not produce any fractions.

fmod() returns the remainder after division, similar to mod operator % for integers. So fmod(1.1234,1.0) will return 0.1234

double z;
        z=a*x/p;
        cout << modf(z, 1.0);

Error: no matching function for call to `modf(double&, double)'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.