Random number generator doesn't produce expected numbers within a given range using values from input fields

Paul Lovell

I'm trying to generate a random number between two given values. I'm able to produce this with a pretty standard little function, however when I try to set the maximum and minimum values through an input field, I get some unexpected results.

This is using jQuery, which isn't necessary for this particular function but is needed for the larger project.

Here's an example of what I'm finding:

https://jsfiddle.net/u2k41hzd/

function randomNumber(min, max) {
    points = Math.floor(Math.random() * (max - min + 1) + min);
}

$( "button" ).on( "click", function ( event ) {
    minPoints = $( ".min-points" ).val();
    maxPoints = $( ".max-points" ).val();
    randomNumber(minPoints, maxPoints);
    $(".random").html(points);
});

In the case of the minimum number being 1 and the maximum being 6, I would expect to get numbers between 1 and 6. However, I get numbers between 0 and 5.

If the minimum number is 2 and the maximum 6, I would expect to get numbers between 2 and 6, but get numbers between 0 and 4. Passing in 3 and 6 gives numbers between 0 and 3, and so on.

Ignoring the input values and hard coding them instead seems produce expected results with no issue. Essentially I'm just unsure as to why the input values are behaving as they are. I'm sure I've just misunderstood something or made a mistake somewhere, but I've not been able to determine the reason!

Vasil Dininski

The issue is that you need to add the min to the rounded number, not to the randomly generated number:

function randomNumber(min, max) {
    points = Math.floor(Math.random() * (max - min + 1)) + min;
}

To explain further, for the case of 2 and 9:

  1. Math.random() generates a number between 0 and 0.999999999...
  2. max - min + 1 = 8
  3. So the generated number will be in the range 8 * 0 and 8 * 0.99999999...
  4. Flooring it will round down in the range [0, 7]
  5. The result would need to be offset by the starting number (i.e. the minimum allowed number - 2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using fn:random-number-generator to produce random numbers more than once

Javascript: Random number generator won't produce random number

Random number generator does not produce an expected answer Node JS

Random number within range and a given granularity in Golang

Produce a random number in a range using C#

Calling a random number generating member function doesn't produce entirely random numbers

Scala Random Number Generator Not Producing Unique Random Numbers Between A Range

Input row doesn't have expected number of values required by the schema

Random number generator always returning 0 when given a range

Is there any way to generate a random number within a given range using only CSS?

I want to generate random numbers within given range in java

Generating random numbers which allows some repetition within a given range

How can i create a random number generator in python that doesn't create duplicate numbers

Random number from given numbers in structure C

How to select random values from a given range

Remove values from a tensor that are within a given range

How to generate random numbers in range from INPUT?

How to get a random multiple of a given number within a range

Python Random Number Generator within a normal distribution with Min and Max values

Python Random Number Unique number generator before using new numbers

pandas: assign random numbers in given range to equal column values

Javascript random number generator (generates every number once) using input

Random number generator with different numbers

Plotting random numbers from a given distribution along with their expected value in Python

AVR Atmega128 random number generator from range

Vue.js how to collect values from input fields when the number of input fields aren't constant

How to specify the number of odd and even numbers from a random generator in R?

Get list of "n" unique random numbers from the given range

Outputting 20 different random numbers within a range using php