How to generate random numbers without using rand() function?

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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.
Last edited on
Use this to get the fraction part of the division -> http://cplusplus.com/reference/clibrary/cmath/modf/

Then, multiply it with b (=1000 for example) and use it as the new x. Also, mind that p should be greater than b.
Last edited on
Topic archived. No new replies allowed.