Question

In: Computer Science

// How to create a random number this program only creates a specific same number #include...

// 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;

Solutions

Expert Solution

Create a random number

  • For creating random number outputs, It is necessary to enter random input
  • The integer seed value should accept from the user
  • Then different results can be obtained

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


Related Solutions

java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX....
Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes. Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint: Think though the easiest way to construct the phone number. Each digit does do not have to be determined separately....
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
Create a program that generates random number between 0 and 50 and puts them into a...
Create a program that generates random number between 0 and 50 and puts them into a linked list. When it generates 49, instead of adding it to the list, it prints the list, freeing each node and then exits. Submit your .c file
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
create a python function (only) that generates 10 random numbers within a specific range. Every time...
create a python function (only) that generates 10 random numbers within a specific range. Every time a number is generated, it is appended to a list (you need to initialize a list). Then the function counts how many times a number is repeated in the created list (you can choose any number to count). The function returns the list, the number you have chosen and its count. --define the function here ---
Java - Write a test program that creates an Account object with an account number of...
Java - Write a test program that creates an Account object with an account number of AC1111, a balance of $25,000, and an annual interest rate of 3.5. Use the withdraw method to withdraw $3,500, use the deposit method to deposit $3,500, and print the balance, the monthly interest, and the date when this account was created.
Create a class called RandomWhen. The program must continuously generate a random number between 1 and...
Create a class called RandomWhen. The program must continuously generate a random number between 1 and 100 inclusive. The program must stop when the number 1 is generated, and the loop iteration will be printed. Run the program 3 different times. Sample output: It took ## iterations to generate a 1. It took ## iterations to generate a 1. It took # iterations to generate a 1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT