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 what I have that is not working correctly:

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


int main()
{

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

//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;
cin >> Lower_Bound >> Upper_Bound;
cout << "Please enter the number of random numbers you wish to print: " << endl;
cin >> RandPrint;

srand(seed);
Lower_Bound_Rand = rand() % Lower_Bound;
Upper_Bound_Rand = rand () % Upper_Bound;
Range = rand()% Lower_Bound && rand()% Upper_Bound;


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


    return 0;
}

Lower_Bound_Rand = rand() % Lower_Bound;
Upper_Bound_Rand = rand () % Upper_Bound;
Range = rand()% Lower_Bound && rand()% Upper_Bound;

So you're saying that Range will be either 0 or 1, with a very very strong bias toward 1. The result of the && operator is a boolean value, and unless otherwise specified each half of the test will compare the result of the expression to see if it's either 0 or not. So this would be equivalent (using your redundant *_Rand variables:

Range = (Lower_Bound_Rand != 0 && Upper_Bound_Rand != 0);

You already know how to find a random number between 0 and an upper bound, so why not use the difference of the upper and lower bounds as the upper bound and then add the lower bound?

Range = rand() % (UpperBound - Lower_Bound + 1) + Lower_Bound;

This will shift the range such that it starts at the lower bound and doesn't exceed the upper bound. Not the +1 in there to make the upper bound inclusive. Otherwise it won't be due to how modulo arithmetic works.

You also don't change the value of Range inside the loop, so it'll never be different. The expectation is that you recalculate a new random number for each iteration of the loop.

I see. However, this code returns the same number everytime.

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


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;
cin >> 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;
}

However, this code returns the same number everytime.

Please read my previous post again. All the way to the end this time.

Edited 4 Years Ago by deceptikon

I guess I am confused. I am not changing the value of Range inside the loop. I am merely printing out what Range is. Can you give me a hint on what your getting at? It would be greatly appreciated...I am sure it is goning to be an "oh! thats right!" But I am stuck.
Thanks!

I am not changing the value of Range inside the loop. I am merely printing out what Range is.

You're not changing the value of Range in the loop, but you should be. Range doesn't change, that's why you keep getting the same number.

There may be some confusion here about what Range represents. It's one number in the range, not all of them.

This article has been dead for over six months. Start a new discussion instead.