Which algorithm is used to generator random number?
Answers
Answer:
To generate random number we can use random (), srand(), rand() .
Most commonly used one is random().
Explanation:
Program for turbo c++ compiler:
#include<iostream.h>
#include<stdlib.h>
void main()
{
randomize();
int max, num;
cout << "Enter the number of random numbers you want ";
cin >> n;
cout << "Enter the maximum value of random number ";
cin >> max;
cout << "Random numbers from 0 to " << max << " are:" << endl;
num = random(max);
cout << num ;
}
Here I have used randomize() in addition to random() because it helps in making the desired number truly random. This can be explained by the fact that computer has zero iq and zero decision making ability. when it received random comman commanding it to point to any random number then it points to that number and store that value. Hence using only random() produces same output. But when we use randomize() it deletes the previous value and makes computer start over this making it a random value
There are two principles method used to generate random numbers-
1) HRNG (hardware random numbers generator)
2) PRNG (pseudo random numbers generator