PDA

Click to See Complete Forum and Search --> : restricting random



godot242
04-02-2002, 05:24 PM
greetings,
i need to create a random whole number between two numbers such as,
between 7 and 25.

does anyone know how this is done?

-jason

ironmallet
04-03-2002, 02:55 AM
to create a random integer between 7 and 25.

Math.random() returns a random # between 0 and .99999
so first we stretch the span
18*(Math.random());
which changes the span to 0 to 17.9999
so then add 7.5 and chop the decimal.
Math.floor(18*(Math.random())+7.5);
so this makes the range 7 to 25, just the integers.

in general:
a to b, just the integers

Math.floor((b-a)*(Math.random())+a+.5);

just in case:

are you trying to get something like 6d4+1 ?

because that's different.

six four sided dice plus 1 makes a nice bell curve centered on 16, to get this sort of curve, just add up 6 separately generated a=1,b=4 types, then add 1.

you'll notice a lack of >21 <11 results, try it....