Question:
i need help on a c++ program using "srand"?
2012-02-23 18:20:12 UTC
Part A
write a function name generateRandom that generates random numbers in a range specified by the user. the function takes two integer parameters representing the lower and upper bounds of the range and it returns an integer random number between the lower and upper bound inclusive. For example, if the parameters are 5 and 25, the function will return a random number between 5 and 25. write a main function that tests this function by:
1)asking the user for an integer with which to seed the random number generator. call strand with this integer.
2)ask the user for a lower and upper bound for the random number,
3)call generateRandom and print the generated randomNumber.

part B
You often want to test a random number generator to make sure that it really seems to be generating random numbers. Two ways to test it is to generate a long sequence of random numbers and make sure that the average of the random numbers falls you're the midpoint of the range a\of the random numbers being generated, and to make sure that the percentage of random numbers above and below the midpoint is roughly 50% each. For example, if your range id 5-25, then the midpoint of the range is 15. you would expect the average of your random numbers to be roughly 15 and for 47-48% of your numbers to fall below the midpoint, 47-48% to fall above the midpoint, and 5% to fall at the midpoint. if the range were instead 5-24, then the midpoint is 14.5 and i would expect 50% of my random numbers to fall below the midpoint and 50% above the midpoint. since the midpoint is a floating point number, i would not expect any of my random numbers to be at the midpoint.
In this part, you will modify your program from part A to calculate these statistics. Modify your program in the following fashion:
1) write a void function called setPrecision that takes an integer parameter, which is the number of decimal places you would like to see in you printed numbers, and that sets cout so that it prints this number of decimal places. you "magic-formula";

a. cout.setf(ios::fixed);
b. cout.setf(ios::showpoint);
c. cout.precision(numDecimalPlaces)

where numDecimalPlaces is your integer parameter.
2) write a function called computerPercent to calculate the percentage number was rolled, the function should take two numbers, the number of times a number was rolled and the total number of dice rolls, and return an integer that represents the percentage of rolls, rounded to the nearest integer(round up from 0.5). For example, given 7 and 28, you should return 25, which represents 25%. you will need to use the static_cast function to convert one of your two integer parameters to a double; otherwise you will end up with integer arithmetic that always calculates 0 (e.g. 7/28 = 0 in integer arithmetic)
3) write a function avg that computes and returns the floating average of a sequence of random numbers, and also calculates and prints the percentage of numbers falling above, and at the midpoint. the function should take three integer parameters-1) the number of random numbers to generate, and 2) the lower and upper bounds for the random numbers. it should then use a for loop to call generateRandom the specified number of times oand compute the average. your for loop should also keep track of the the number of random numbers that fall below, at, and above the midpoint. your function should call computePercent to compute the percentage of numbers below, at, and aboce the midpoint and print these percentages.
4)test you r new function by modifying your main function from part A as follows: a. in addition to the inputs you requested from the user in part A you should also ask the user for the number of decimal places that should be used for output and the number of random numbers to be generated.
b. it should then call your setPrecision function to set cout to the desired precision for decimal places and avg to generate and compute the average of the requested random numbers. your program should then print the average of the random numbers. the more random numbers that are generated, the closer the average should be to the midpoint of the range.

example of output:

please enter the lower and upper bounds for the random number: 5 25
random number = 6
please enter the number of decimal places you would like: 3
please enter the number of random numbers to generate: 10000
midpoint = 15.000
percent less than midpoint = 47
percent equal to midpoint = 5
percent greater then midpoint = 48
average of 10000 numbers between 5 and 25 is 15.069

can anyone help me with this problem?
Three answers:
2012-02-23 18:27:43 UTC
Why don't you write what you can? This surely must be more interesting than your humanities classes.



You are close try this

int somefunction(int lowerbound, int upperbound)

{

//if you want to include the upperbound add 1

return lowerbound + (rand() % (upperbound+1-lowerbound));

}
Rog
2012-02-23 18:34:26 UTC
I concur. We are all eager and willing to help someone fix a bug they find in their code... no one, however, is prepared to write your entire homework program.
?
2016-11-08 14:33:22 UTC
it really is extra constructive : if ( x == 10) { std::cout << "My well-liked huge type is 10 too!: "; } else if ( x > 10) { std::cout << "Your well-liked huge type is larger than 10!: "; } else if ( x < 10) { std::cout << "Your well-liked huge type is decrease than 10!: "; }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...