JavaScript random generate 0 or 1 integer [duplicate] - javascript

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)

Related

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

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

find random number between -7 and -12 with javascript [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(40 answers)
Closed 1 year ago.
I got this javascript code that returns a random integer number between 1 and 10.
I got a problem making it return a random number between -7 and -12.
You gotta bail me out of this one, please...
const a = Math.floor(Math.random() * (10 - 1)) + 1;
console.log(`Random value between 1 and 10 is ${a}`);
The difference between -7 and -12 = 5.
First, change your code to give a random number between 0-4 (or 1-5) instead of 1-10.
After that, subtract 12 from the outcome.

Hi. I want to give the variables dX and dY a random number, either -2, 0, or 2 [duplicate]

This question already has answers here:
How can I randomly generate a number in the range -0.5 and 0.5?
(2 answers)
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 2 years ago.
dX = 0;
dY = 0;
right now the Variables are set to 0 but I wan o give them a random number either -2, 0 or 2. I think that math.random() might work, but I don't know how to use it with negative numbers.

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)

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.

Categories

Resources