Using C++ The most well-known method for generating random numbers, which has be
ID: 3576587 • Letter: U
Question
Using C++
The most well-known method for generating random numbers, which has been used almost exclusively since it was introduced by D. Lehmer in 1951, is the so-called linear congruential method. The method is as follows:
Set v[0] = s where s is some arbitrary number; a good choice may be to use time(NULL) in place of s. The expression time(NULL) returns the number of seconds that have passed since the epoch – Jan 1 1970 which is taken as the birth date of the C language.
To get a new random number, take the previous one, multiply it by a constant B, add 1 and take the remainder when divided by a second constant M. The result is always an integer between 0 and M-1. Namely, in a for loop do v[i] = (v[i-l]*B + 1) % M;
Write a program to nd the rst n random numbers.
1. a function void random( vector<int>&, size t n) which constructs the vector. Use s = time(NULL), B = 31415821, and M = numeric limits<unsigned int>::max();
2. a function void print( const vector<int>& ) to print the entries of the vector in the fashion {v[0], v[1], . . . , v[n-1]}
3.The main part of the program should read a positive integer into n and call the above two functions.
Explanation / Answer
2. void print (const vector<int> & lcg) {
for(int i=lcg.begin(); i!=lcg.end(); ++i){
std::cout<<(*i)<<std::endl;
}
3. int main(){
cin>>n;
std::vector<int> vec;
random(vec,n);
print(vec);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.