I've got a working version of the random number generator from the wiki, and I'm trying to think of a good way to generate numbers that in the 0-5 or 0-10 range. I know a good general purpose way of generating numbers in a range is to keep generating values until you get a number under the ceiling, but that might be a lot of work to get a number under 5. It would be easy for me to do a table look up, which would be a fine solution for our current small game, but I wouldn't want to do that a lot for a large game.
One other idea I had was something like:
but that's also pretty slow, I feel like I'm missing something obvious. Is there a more clever way to do this?
One other idea I had was something like:
Code:
var n = random number from 0..255
var a = 0
var b = 0
while a < n
increment a
increment b
if b > ceiling
b = 0
var a = 0
var b = 0
while a < n
increment a
increment b
if b > ceiling
b = 0
but that's also pretty slow, I feel like I'm missing something obvious. Is there a more clever way to do this?