Random Numbers

May 3, 2014 at 5:02am
How do you do this...

Write an instruction to generate a random number in the range -501 to + 50 inclusive.
May 3, 2014 at 3:34pm
@Mutombo

Easy. Add the two numbers together, as positive numbers. Generate a random number in that range, and subtract your lowest number. As you have here..

501 + 50 = 551
random number from 0 to 551
subtract 501

So, if the random generated number is 551, subtract 501, you get 50. Your top number.
If the number is 0, you subtract 501, you get, of course, -501, your lowest number.
May 3, 2014 at 4:14pm
OP doesn't seem to want to pay attention in class, and now wants to spam us with all his easy homework Qs.
May 3, 2014 at 4:19pm
I have a feeling he doesn't attend any of his classes, else he would not be posting these kinds of questions.
Last edited on May 3, 2014 at 5:36pm
May 4, 2014 at 4:47am
use this

1
2
int random_number;
random_number = 501 + rand() % (50);
Topic archived. No new replies allowed.