Random Number Help

closed account (967L1hU5)
So I'm making a game, and in it you're supposed to die at a certain age. I wanted the age to be random, for more spunk. I wanted to age to be in between 60-80. So I used this code:

1
2
int deathage;
deathage = rand() % 80 + 60;

(I know I could have that as one line)

The only problem, is that when I was testing it, by having it print deathage, it showed 114 one time. How do I get it to be just between 60-80.
you might try a var with a base of 60 then add a random number between 0 -20:
int deathVar = 60 + rand () % 20;
closed account (967L1hU5)
Thank you very.
let me know if it works :) I've been finding uses for rand () myself
I found this:

1
2
3

deathage =  60 + int( ( 80 - 60 ) * rand() / ( RAND_MAX + 1.0 ) );
Last edited on
why do all the extra math (CPU cycles), just assign the value of 60 + the number generated by rand () limited to 0 - 20? That puts it in the limit hi and low, efficiency.... :)
Duh! Nice one, Bin1000101!
Topic archived. No new replies allowed.