Here is the assignment (Part A).
In this part you will write a function to generate random numbers in a specified range. Specicially we want you to write a function named generateRandom that generates a single random number 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 srand with this integer.
2) Ask the user for a lower and upper bound for the random number.
3) Asking the user how many random numbers to generate
4) Call generateRandom the requested number of times and prints the generated
random numbers.

Here is my code. However, my code prints out the same number instead of a random number each time.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

int generateRandom(int &Lower_Bound,int &Upper_Bound);//function prototype generate random
void setPrecision(int a, int b); // SetPrecision Prototype
double computePercent(int input1, int input2); // ComputePrecision Prototype

int main()
{

int seed; 
int Lower_Bound;
int Upper_Bound;
//int Lower_Bound_Rand;
//int Upper_Bound_Rand;
int RandPrint;
int Range;
//int N;

//Begin Part A:

cout << "Please enter an integer seed for the random number generator: " << endl;
cin >> seed;
cout << "Please enter the lower and upper bounds for the random number: " << endl;
generateRandom(Lower_Bound,Upper_Bound);
cout << "Please enter the number of random numbers you wish to print: " << endl;
cin >> RandPrint;

srand(seed);
Range = rand() % (Upper_Bound - Lower_Bound + 1)  + Lower_Bound;


for (int i =0; i<RandPrint; i++){
    cout << "Random integer = " << RandPrint << Range << endl;
}


    return 0;
}


int generateRandom(int &Lower_Bound,int &Upper_Bound){
cin >> Lower_Bound;
cin >> Upper_Bound;

return (Lower_Bound>>Upper_Bound);

}

void setPrecision(int a,int b){
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
}// SetPrecision function

double computePercent(int input1, int input2){

    return 0;
}

Recommended Answers

All 4 Replies

What's the purpose of the return value on line 49 when the program discards it? That function is not doing its intended purpose according to instruction #4.

Let's see if you followed the assignment:

Specicially we want you to write a function named generateRandom that generates a single random number in a range specified by the user.

Look at the generateRandom() function. Does it in fact generate any random numbers?

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 srand with this integer.
2) Ask the user for a lower and upper bound for the random number.
3) Asking the user how many random numbers to generate

Did you do each of these three things?

4) Call generateRandom the requested number of times and prints the generated random numbers.

Did you do this properly?

You need to be much more careful when you program. Take each step one at a time and program that step before moving to the next step. Don't lump them all together to and try programming everything in one go. That's where many mistakes happen.

Why did you create a new thread for the same question?

I suggest you study more, because this code is far more than messy!

1- You should follow a indentation style, and use spaces and tabs correctly;
2- Why use the ctime library if the seed number will be inserted by the user?
3- And why use the cmath library?
4- The return statement in line 49 doesn't make any sense! And the function only initializes the variables, so its return must be of type 'void';
5- The function setprecision(), as much as its parameters are useless. Try to use the iomanip library to use setprecision();
6- What is the role of the function computePercent()??
7- The rand() function generalized is written like this:
number = shiftingValue + rand() % scalingFactor;
In your case: Rang = LowerBound + rand() % UpperBound;

As you can see, there is a lot of things you skipped! But don't give up! Just study more!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.