Reputation: 169
I want my program to generate 500 random numbers with the range of 10 and 65. How do I do that?
Currently, my program generates between 0 to 74 random numbers.
And also, it seems that my highest and lowest value does not read the random generated number but the range of it.
This is my code:
#include<iostream>
#include<exception>
#include<stdlib.h>
using namespace std;
int main(){
int year;
int nextLine = 20;
int highestValue = 0;
int lowestValue = 0;
int ages[500];
int range = 65;
cout << "Enter year today: ";
cin >> year;
srand(year);
cout << "The ages are: \n";
for (int i = 0; i < 500; i++){
ages[i] = rand() % 65 + 10;
cout << ages[i] << " ";
if (i + 1 == nextLine){
cout << "\n";
nextLine += 20;
}
}
for (int j = 0; j < 500; j++){
if (ages[j] > highestValue)
highestValue = ages[j];
if (ages[j] < lowestValue)
lowestValue = ages[j];
}
cout << "\nRange(HV-LV): " << highestValue << " - " << lowestValue << " = " << highestValue - lowestValue;
system("pause>0");
return 0;
}
EDIT: This is a working code including the range. :)
#include<iostream>
#include<exception>
#include<algorithm>
using namespace std;
int main(){
int year;
int nextLine = 20;
int highestValue;
int ages[500];
int lowestValue;
int range = 65;
cout << "Enter year today: ";
cin >> year;
srand(year);
cout << "The ages are: \n";
for (int i = 0; i < 500; i++){
ages[i] = rand() % 56;
ages[i] += 10;
cout << ages[i] << " ";
if (i + 1 == nextLine){
cout << "\n";
nextLine += 20;
}
}
highestValue = *max_element(ages, ages + 500);
lowestValue = *min_element(ages, ages + 500);
cout << "\nRange(HV-LV): " << highestValue << " - " << lowestValue << " = " << highestValue - lowestValue;
system("pause>0");
return 0;
}
Upvotes: 3
Views: 2100
Reputation: 10073
The problem is not with rand() % x + y
As noted by Jordan Melo, remainder (%) has a higher precedence than addition (+). Splitting the lines has no effect on the computation.
The problem is that your lowestValue=0
, which is not correct. No value generated by your rand() expression can be lower than 0.
It helps to pay attention to your output. Notice that none of the output values are less than 10. This shows that your rand() expression is working correctly.
Your modified code is incorrect also
In your modified code, you have changed lowestValue
and highestValue
to be uninitialized values. What this means is that your code may or may not work.
In order to find the largest, the initial largest must be the smallest possible value.
In order to find the smallest, the initial smallest must be the largest possible value.
int lowestValue = 74;
int largestValue = 10;
Hope this helps.
Upvotes: 1
Reputation: 122726
Once you have random numbers generated, lets say in the range from 0 to 74 but you need them in the range from 10 to 65 you just have to transform them by
x = y * (55.0/74.0) + 10;
or more general, if y is in [ymin,ymax)
and you need x in [xmin,xmax)
you can get it via
x = (y-ymin) * ( (xmax-xmin)/(ymax-ymin) ) + xmin;
There is already a (better) answer, just wanted to mention it.
Upvotes: 1
Reputation: 1243
There are two reasons why your original code wasn't producing the results you desired.
1) ages[i] = rand() % 65 + 10;
should be ages[i] = rand() % 56 + 10;
But you already know that :)
2) When you are calculating your lowest value, you set int lowestValue = 0;
, but in your test
if (ages[j] < lowestValue)
lowestValue = ages[j];
you can see that obviously, none of your age values are going to be less than zero, so the lowestValue
never gets updated. But if you examine your printout, it shows that none of your values are actually below 10.
If you start with int lowestValue = std::numeric_limits<int>::max()
instead of int lowestValue = 0
and include #include <limits>
, you should get the expected result.
That's why you didn't see the issue when you used min_element
and max_element
:)
Upvotes: 2
Reputation: 86
Change this:
ages[i] = rand() % 65 + 10;
To this:
ages[i] = rand() % 56;
ages[i] += 10;
Upvotes: 2