@thezachperson31 You could use round, but then both, min and max only had half the chance to roll like the other numbers do. You could also substract one and take ceil. This however leaves the max number with a minimal less chance to roll due to the [0,1) Interval.
–
ChristophDec 22 '12 at 9:18

1

Is anyone getting an unusual amount of Min? I have run this many times on an integer range of [0-10] and I seem to be getting 0 an awful lot. Is it just coincidence? maybe I should use round.
–
LeoApr 18 '13 at 14:21

2

You should get upvotes simply because of the effort you put into the answer, not to mention the completeness :)
–
Eduard LucaDec 12 '14 at 10:33

We are returning a function (borrowing from functional programming) that when called, will return a random integer between the the values bottom and top, inclusive. We say 'inclusive' because we want to include both bottom and top in the range of numbers that can be returned. This way, getRandomizer( 1, 6 ) will return either 1, 2, 3, 4, 5, or 6.

(bottom is lower number, top is greater number)

Math.random() * ( 1 + top - bottom )

Math.random() returns a random double between 0 and 1, and if we multiply it by one plus the difference between top and bottom, we'll get a double somewhere between 0 and 1+b-a.

Math.floor( Math.random() * ( 1 + top - bottom ) )

Math.floor rounds the number down to the nearest integer. So we now have all the integers between 0 and top-bottom. The 1 looks confusing, but it needs to be there because we are always rounding down, so the top number will never actually be reached without it. The random decimal we generate needs to be in the range 0 to (1+top-bottom) so we can round down and get an int in the range 0 to top-bottom

Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom

The code in the previous example gave us an integer in the range 0 and top-bottom, so all we need to do now is add bottom to that result to get an integer in the range bottom and top inclusive. :D

NOTE: If you pass in a non-integer value or the greater number first you'll get undesirable behavior, but unless anyone requests it I am not going to delve into the argument checking code as its rather far from the intent of the original question.

After generating a random number using a computer program, it is still consider as a random number if the picked number is a part or the full one of the initial one. But if it was changed, then mathematicians are not accept it as a random number and they can call it a biased number. But if you are developing a program for a simple task, this will not be a case to consider. But if you are developing a program to generate a random number for a valuable stuff such as lottery program, or gambling game, then your program will be rejected by the management if you are not consider about the above case.

So for those kind of people, here is my suggestion:

Generate a random number using Math.random().(say this n)

Now for [0,10) ==> n*10 (i.e. one digit) and for[10,100) ==> n*100 (i.e. two digits) and so on. Here squire bracket indicates that boundary is inclusive and round bracket indicates boundary is exclusive.
Then remove the rest after the decimal point. (i.e. get floor) - using Math.floor(), this can be done.

If you know how to read random number table to pick a random number, you know above process(multiplying by 1, 10, 100 and so on) is not violates the one that I was mentioned at the beginning.( Because it changes only the place of the decimal point.)

Study the following example and develop it to your needs.

If you need a sample [0,9] then floor of n*10 is your answer and if need [0,99] then floor of n*100 is your answer and so on.

Now let enter into your role:

You've asked numbers among specific range. (In this case you are biased among that range. - By taking a number from [1,6] by roll a die, then you are biased into [1,6] but still it is a random if and only if die is unbiased.)

Note: In method one, first I created an array which contains numbers that you need and then randomly put them into another array.
In method two, generate numbers randomly and check those are in the range that you need. Then put it into an array. Here I generated two random numbers and used total of them to maximize the speed of the program by minimizing the failure rate that obtaining a useful number. However adding generated numbers will also give some biassness. So I would recommend my first method to generate random numbers within a specific range.

In both methods, your console will show the result.(Press f12 in Chrome to open the console)

We do execute high++ becauseMath.random() generates a random number between 0, (inclusive), and 1(exclusive) The one being excluded, means we must increase the high by one before executing any math. We then subtract low from high, giving us the highest number to generate - low, then +low, bringing high back to normal, and making the lowest number atleast low. then we return the resulting number

To test this function, and variations of this function, save the below HTML/JavaScript to a file and open with a browser. The code will produce a graph showing the distribution of one million function calls. The code will also record the edge cases, so if the the function produces a value greater than the max, or less than the min, you.will.know.about.it.

The reason for taking away 0.5 from the minimum value is because using the minimum value alone would allow you to get an integer that was one more than your maximum value. By taking away 0.5 from the minimum value you are essentially preventing the maximum value from being rounded up.