In: Computer Science
// How to create a random number this program only creates a specific same number
#include <iostream>
#include <iomanip>
double random (int &seed);
bool determine_larger (double rand_num);
void print_results (double rand_num);
using namespace std;
int main()
{
int seed = 8;
double rand_num = random (seed);
print_results (rand_num);
return 0;
}
// function: random number
// does:generates a random number between (0, 1)
double random (int &seed)
{
int const MODULUS = 15749;
int const MULTIPLIER = 69069;
int const INCREMENT = 1;
seed = ( (MULTIPLIER*seed) + INCREMENT)% MODULUS;
return double (seed)/MODULUS;
}
// function: identify if smaller or larger than .5
// does: identifies if the number is smaller or larger
bool determine_larger (double rand_num)
{
bool t, f;
f = false;
t = true;
if (rand_num >= .5)
return t;
else return f;
}
// function: print results
// does: prints if the number is larger or smaller
void print_results (double rand_num)
{
if (determine_larger(rand_num))
cout << rand_num << " is larger " << endl;
else cout << rand_num << " is smaller " <<
endl;
Create a random number
E.g: add this code on the program;
int seed;
cout<<"Enter a number to find random"<<endl;
cin>> seed;
The modified program code
#include <iostream>
#include <iomanip>
double random (int &seed);
bool determine_larger (double rand_num);
void print_results (double rand_num);
using namespace std;
int main()
{
int seed;
cout<<"Enter a number to find random value:"<<endl;
cin>> seed;
double rand_num = random (seed);
print_results (rand_num);
return 0;
}
// function: random number
// does:generates a random number between (0, 1)
double random (int &seed)
{
int const MODULUS = 15749;
int const MULTIPLIER = 69069;
int const INCREMENT = 1;
seed = ( (MULTIPLIER*seed) + INCREMENT)% MODULUS;
return double (seed)/MODULUS;
}
// function: identify if smaller or larger than .5
// does: identifies if the number is smaller or larger
bool determine_larger (double rand_num)
{
bool t, f;
f = false;
t = true;
if (rand_num >= .5)
return t;
else return f;
}
// function: print results
// does: prints if the number is larger or smaller
void print_results (double rand_num)
{
if (determine_larger(rand_num))
cout << rand_num << " is larger " << endl;
else cout << rand_num << " is smaller " << endl;
}
The output