What is wrong with this random number generator

Okay, here's my function for random number generation:

    squiffy.getRndInteger = function(min,max){
        return Math.floor(Math.random()*max)+min;

And heres the calling code:

    set("god_toolReduction", squiffy.getRndInteger(50,90));

This is supposed to give me a number from 50-90, but I just saw it come up with a 96. Any ideas? I pulled this from another example I'd found here in the forum.


Try this one.

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;

It's the last example here:


This looks like it works. I'll have to keep an eye on it.

See, my code was used so that every time you played, you didn't know what the chance that something would work or not (the percentages would drop for each chance. The idea was that this would drop them 50 to 90%. But there were a couple of times I saw the percentages rise!

Thanks much for the quick response on this!

there were a couple of times I saw the percentages rise!


We are working with a min of 50 and a max of 90.

First, this bit is pulling a random number between 0 and 90: Math.random()*90

So, for the sake of argument, lets say the random number this time is 89.

That means we now have: Math.floor(89)+50;

This returns 139.

This topic is now closed. Topics are closed after 60 days of inactivity.