remy
remy

Reputation: 1305

how to generate a num in range

I have a formula to generate a score, for example, score = UnixTimeOfNow(), but I want to get the final score in range of (1,n), such as (1,100). so how can I map the score to specific range?

Upvotes: 0

Views: 55

Answers (1)

aioobe
aioobe

Reputation: 421100

To map score to a value in range (1, n) inclusive you can do

score = 1 + (score % n);

(That's Java, C/C++ syntax.)

The % is the modulo operator which says that for instance 205 % 100 is 5.

Upvotes: 3

Related Questions