What will Math.floor(Math.random() * 10 + 1); do [duplicate] - javascript

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(40 answers)
How does Math.floor(Math.random() * (Max - Min + 1) + Min) work in JavaScript? (Theory)
(3 answers)
Closed 8 months ago.
I came across the following command:
Math.floor(Math.random() * 10 + 1);
I understand that Math.floor() will convert to the lowest integer, but what does Math.random() * 10 + 1 do?

Math.random returns a floating point number x such that:
Multiplying 10 to x yields a floating point number in the range:
And then adding 1 to 10x yields a floating point number in the range:
And finally flooring 10x + 1 yields an integer in the range:
Therefore, Math.floor(Math.random() * 10 + 1) yields an integer that lies in the range [1, 10].

Math.random generates a random number between 0 and 1. So for example it can be .124 or .42242. When you multiply this random number against something, you are basically making it so that the new result is no less than 0, but no more than the number you are multiplying it against.
So in your example, you are multiplying it against 10, which means you will get a random number between 0 and 10 with decimals; technically this range is 'more than 0, but less than 10'. The +1 just increases the total range so that it is between 1 and 11, or 'more than 1, but less than 11.
When you apply Math.floor() to this, you are rounding all of these results down. So in the case without the '+1', your finite range is actually 0-9, and with the +1 it is 1-10

Related

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

This question already has answers here:
Generate random number between two numbers in JavaScript
(32 answers)
Closed 3 years ago.
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!
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:
Math.random() generates a number between 0 and 0.999999999...
max - min + 1 = 8
So the generated number will be in the range 8 * 0 and 8 * 0.99999999...
Flooring it will round down in the range [0, 7]
The result would need to be offset by the starting number (i.e. the minimum allowed number - 2)

JavaScript random generate 0 or 1 integer [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 5 years ago.
I am trying to generate random 0 or 1 as I am writing a script to populdate my database. If it is 1, I will save it as male and 0 the other way around.
Inside my JavaScript:
Math.floor((Math.random() * 1) + 1);
I used this to generate either 1 or 0. However, with the code above, it always return me with 1. Any ideas?
You can use Math.round(Math.random()). If Math.random() generates a number less than 0.5 the result will be 0 otherwise it should be 1.
There is a +1 with Math.random, so it will always going to add 1 to the randomly generated number.
You can just randomly generate a number, since Math.random will generate any floating number between 0 & 1, then use if.. else to assign 0 or 1
var y = Math.random();
if (y < 0.5)
y = 0
else
y= 1
console.log(y)

How can I generate a random number between 1 and 2,147,483,647 [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 6 years ago.
I know about random that generates a random number between 0 and 1.
Math.random();
But how can I generate a random number between 1 and 2,147,483,647 using Javascript?
You can simply multiply Math.random() times the limit value and then round down. Since Math.random() returns a decimal value between 0 and 1, you will always get some scaled proportion of your limit value so your result will be between 0 and your limit value.
If you want the result to be an integer, then use can call Math.floor() on the result of the multiplication to round down to an integer.
Since you also want the lowest value to be 1 instead of 0 and you want the result to include the limit value, then you can add 1 to the result to put it in the exact range.
var maxInt = 2147483647; // max 32-bit signed int
function randomInt() {
// generate random value between 1 and maxInt inclusive of both values
return Math.floor(Math.random() * maxInt) + 1;
}
Working demo: https://jsfiddle.net/jfriend00/ntkokacf/
In order to find a number you can do something to the effect of:
var randomNumber = Math.floor(Math.random() * 2147483647);
You can also write a function to repeat that and that will allow for any minimum or maximum:
function random(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
The explanation of the function is that it asks for a minimum and a maximum number. Whatever the minimum number is, we add to the second half of the return statement. The second half needs to give us the number of different possible numbers between min and max, so we multiply Math.random() by that exact number, which is the difference between max and min plus one. The problem is that this gives a decimal, therefore we take the floor of that to get rid of the decimal.

JavaScript Random Number [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 9 years ago.
This maybe more mathematical question than programming. In JS I wanted to a function that returns a random integer number in an interval lets say 1-6 and this is what I found:
// Returns a random integer between min and max
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
I feel guilty if I copy and paste this in my code. I don't understand this :
Why we subtract min from max, add 1, multiply the answer by Math.random() and then add the min. I tired with several numbers manually on paper and it work just fine ! But I don't understand why !
Assuming you already understand the behaviour of Math.floor and Math.random, here's the rest step by step:
Math.random() ↝ a random number between 0 (inclusive) and 1 (exclusive)
Math.random() * max ↝ a random number between 0 (inclusive) and max (exclusive)
Math.floor(Math.random() * max) ↝ a random integer between 0 (incl.) and max (excl.)
Math.floor(Math.random() * (max - min)) + min ↝ a random integer between min (incl.) and max (excl.)
Math.floor(Math.random() * ((max + 1) - min)) + min ↝ a random integer between min (incl.) and max+1 (excl.) (OR between min and max both inclusive)
Math.random() will give you a "real" number from 0 to 1 (not including 1.0).
That's cool and all, but what if I want a "real" number from 1 to 2?
The answer: "transform" your [0,1) into [1,2).
In practical terms, it means adding 1 to your result.
Try it out -- Math.random()+1 will give you a number from 1 to 2.
In mathematics this is known as a "mapping". That is -- for every possible real number in [0,1), find a way to "map" that real number to another real number in [1,2). That is, if I give you any real number between [0,1), you should be able to map that number -- apply that number to a function that will return a number between [1,2).
In our case, that function f(x) = x+1.
Do you see how this gives us random numbers between [1,2)? Visualize the two intervals next to each other and imagine a line going from every point in [0,1) to its corresponding map in [1,2). Now, pick a random point on [0,1) ... and follow the line. You'll follow the line to a random point in [1,2)!
Now, all complete one-to-one maps from [0,1) to [1,2) will turn a random number between [0,1) to a random number between [1,2)...but not all of them will give you an evenly distributed random number between [1,2). The mathematics behind what maps give you evenly distributed results is a bit complicated but in short, if your map only involves adding, subtracting, multiplying, and dividing by constants, it's "legal" in the sense that the results will also be evenly distributed.
So, now we know how to transform [0,1) into [1,2).
What if I want to map [0,1) onto [0,2)? I can't just add numbers anymore ...
How about I multiply everything by two?
This should work -- the function f(x) = x*2 does indeed map every point on [0,1) to a point on [0,2) --- and because it only involves multiplication by constants (2), it is a distribution-preserving map.
This works! Math.random()*2 will give you a random number between 0 and 2.
Okay, now something a bit more complicated ... transforming [0,1) into [1,3).
Multiplying by two doesn't work ... 0*2 = 0, and that's not in your target range.
Adding one doesn't work... even though 0+1 is in your target range and 1+1 is, as well, there is no way you can ever reach 3.
If we can't transform [0,1) into [1,3), let's try and see if we can transform something else into [1,3).
How about [0,2)? Yes, we can do this ... the function f(x) = x+1 perfectly maps [0,2) to [1,3). You can think of + as "shifting" the range up.
And so the solution here is clear -- first, turn [0,1) into [0,2), then turn [0,2) into [1,3).
We already know the first (f(x) = x*2), and we figured out the second (f(x) = x+1). So the "combined" transformation/map is f(x) = (x*2)+1.
That is, Math.random()*2 + 1 will give you a number from 0 to 3.
Now for the final trick...mapping [0,1) to an arbitrary range [min,max).
The secret here is to re-write this as [min,min+range), where range = max-min.
Here you can see that it's simple to transform the range [0,range) to [min,min+range) -- you just add "min" to it. So if I had the range [0,range), and I wanted to get [min,min+range), i would use f(x) = x+min.
So how do we get from [0,1) to [0,range) ?
Multiply by range!
f(x) = (x*range) + min
Now writing things back to original terms, using range = max-min
f(x) = (x*(max-min)) + min
will transform a real number from [0,1) to a real number from [min,max)
I'll leave the rest (turning it into a useful integer) to you
0 <= Math.random() < 1 =>
0 <= Math.random() * 6 < 6 =>
0 <= Math.floor( Math.random() * 6 ) <= 5
then you add 'min' so it would look like this:
min <= Math.floor( Math.random() * 6 ) <= 5 + min
in your exemple, for min = 1 you will have all the numbers in 1-6.
I hope now is much clear.
Here's an explanation of your code:
Math.random() generates a random number between 0 and 1 (not including 1).
You need to scale that value based on the range of numbers you want. Your range is how far from your min desired number to your max desired number which is max - min.
If you want to include the max value in the range of numbers generated, then use max - min + 1
You then need to make sure the random number starts at the right base rather than 0 so you add min to it.
Then, if you want it to be an integer you call Math.floor() to truncate it to the next lowest integer.
So, if you just had this:
Math.floor(Math.random())
You would always get zero. Because Math.floor() of a float value between 0 and 1 (not including one) will always truncate down to 0.
Then, if you expand the range with:
Math.floor(Math.random() * (max - min + 1))
You would now get a random number between 0 and max - min including the larger value.
So, to then get it to start at the right base, you add in min like this:
Math.floor(Math.random() * (max - min + 1)) + min

How can I get a one-digit random number in JavaScript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random whole numbers in JavaScript in a specific range
How can I get one-digit random numbers (1, 2, 3, ..., not 0.1, 0.2, ... or 1.0, 5.0, ...) using Math.random() or some other way in JavaScript?
Math.random() returns a float between 0 and 1, so just multiply it by 10 and turn it into an integer:
Math.floor(Math.random() * 10)
Or something a little shorter:
~~(Math.random() * 10)
Disclaimer:
JavaScript's math.rand() is not cryptographically secure, meaning that this should not be used for password, PIN-code and/or gambling related random number generation. If this is your use case, please use the web crypto API instead! (W3C).
If the digit 0 is not included (1-9):
function randInt() {
return Math.floor((Math.random()*9) + 1);
}
If the digit 0 is included (0-9):
function randIntWithZero() {
return Math.floor((Math.random()*10));
}
var randomnumber=Math.floor(Math.random()*10)
where 10 dictates that the random number will fall between 0-9.
Math.floor((Math.random()*10));
And there goes your random integer between 0 and 10!
Use this:
Math.floor((Math.random()*9)+1);

Categories

Resources