Random Numbers

How do you do this...

Write an instruction to generate a random number in the range -501 to + 50 inclusive.
@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.
OP doesn't seem to want to pay attention in class, and now wants to spam us with all his easy homework Qs.
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
use this

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