How can you generate a number that is 5-10% greater than the number that the user inputted using the rand()
function?
Asked
Active
Viewed 76 times
-2

Aykhan Hagverdili
- 28,141
- 6
- 41
- 93
-
2Generate a number between 5 and 10 and multiple the original number by that then divide by 100? – NathanOliver Oct 15 '19 at 20:18
-
1And the result from what @NathanOliver said, must be added to the input number – angelos_lex Oct 15 '19 at 20:19
-
but what if the original number is `0`?! – Sean Bright Oct 15 '19 at 20:21
-
3@Unimportant Then you get `0` and that meets the requirements ;) – NathanOliver Oct 15 '19 at 20:22
-
1@Unimportant Nice, that made me think of negatives... so the absolute value of what Nathan said must be added to the input number i guess – angelos_lex Oct 15 '19 at 20:27
-
1Did you make any attempt? When the body of the question is identical to its title, the answer to that is usually "no"... – Lightness Races in Orbit Oct 15 '19 at 20:33
-
@LightnessRacesinOrbit Hello, nice to meet you as well. I did make many many attempts for hours in fact. It didn't work so I knew that with me being a beginner, I most likely just needed advice on where to even start. In the future, please be more polite and don't make assumptions. I was recommended to this site and I have never used it before. How was I to know that great people would help me almost immediately and that it would be my in my best interest to put more effort into my question? I do not appreciate your feedback. Have a better day today please! – Oct 16 '19 at 21:12
-
Okay, first, I do not appreciate your "have a better day today please", that is totally uncalled for. I am trying to help you, on my own time. To do that, you should put _into the question_ some examples of the things you have tried and exactly how they went wrong. This is the same for beginners and not-beginners, just as it would be in real life if you had a conversation with somebody, and it's well covered on documentation for the site as well. Don't forget that you're asking us to give our time for you for free to solve your problem. Thanks. – Lightness Races in Orbit Oct 16 '19 at 23:27
1 Answers
3
If you insist on using rand (which you really shouldn't), you can divide by RAND_MAX to get you a percentage:
double rand_val = (double)rand() / RAND_MAX;
You have a range of .05 (5-10%), so multiply that percentage by .05 to figure out how far between 5% and 10% you'll be. And then add .05 (5%) so you'll get a value between 5% and 10%:
double my_percent = (rand_val * .05) + .05
Now use it on your number:
double new_val = val * (1 + my_percent)
If you're not tied to rand however, I'd strongly suggest using the random library:
#include <random>
int main()
{
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<> dist(.05, .1);
double val = 100;
double new_val = val * (1 + dist(e2)); //so easy!!
}
See it run here: ideone

scohe001
- 15,110
- 2
- 31
- 51
-
Thank you! I ended up talking with my professor and went a different route. I appreciate all of your efforts though! CS is hard to learn in a large lecture setting, for me at least! – Oct 16 '19 at 21:09
-
@cuzz if you have your own answer to the question, by all means post it as an answer! It may help the next person who comes along with this problem :) – scohe001 Oct 16 '19 at 21:10
-
I didn't know that you can post your own answers! I will do that after my code is graded to make sure that there weren't any errors that I missed when adding additional code. Thank you again! – Oct 16 '19 at 21:18