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

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.

Related

Regex to allow only number(integer no decimal points) between -130 to 30 [duplicate]

This question already has answers here:
RegExp range of number (1 to 36)
(8 answers)
Closed 2 years ago.
Already done regex for 0 to 50
^(?:[1-9]|[1-4][0-9]|50)$
its working fine. Now trying to write with negative numbers like regex only allow -130 to 30 without decimal points.
^(\-[1-9][0-9]?|\-1(0[0-9]|[12][0-9]|30)|[0-9]|[12][0-9]|30)$
\-[1-9][0-9]? checks for -99 to -1
\-1(0[0-9]|[12][0-9]|30) checks for -130 to -100
[0-9]|[12][0-9]|30 checks for 0 to 30

Regex pattern for any integer between -20 and 150 including upto 2 decimals E.g 36.50 [duplicate]

This question already has an answer here:
Regex for values between -10 and 10 up to 1 decimal place
(1 answer)
Closed 3 years ago.
Want to generate a regex for any integer between -20 and 150 including upto 2 decimals
E.g 36.50
Max value 150.00
and min value -20.00
Have tried this so far but its including -20.78 also and 150.04 also.
I want to limit it to 150.00 and -20.00
^((\-([1-9]|1[0-9]|20)(\.\d{2})?)|([0-9]|[1-8][0-9]|9[0-9]|100)(\.\d{2})?)$
Can you please help me out ?
I vote for just using an inequality, along with a simple regex check to assert that there is no decimal component more precise than hundreths:
var input = "10.35";
if (input >= -20 && input <= 150 &&
/^-?\d+(?:\.\d{1,2})?$/.test(input)) {
console.log("MATCH");
}
try this,
instead of taking 20 with all other make it separate
^((\-([1-9]|1[0-9])(\.\d{2})?)|((\-(20)(\.(00))|([0-9]|[1-8][0-9]|9[0-9]|100)(\.\d{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)

Javascript - Limit the number to no decimals [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm using following Javascript code in one of my calculations:
var bx1 = parseFloat(tx9) * ( 70 / 100 );
The output of bx1 is showing in decimals. However, I want it to be no decimals. Looking for some help!
Eg. bx1 = 700*70/100 = 489.999. I want it either 489 or 490, anything is fine.
Use Math.round().
var = Math.round(parseFloat(tx9) * ( 70 / 100 ));
You can use either Math.round(), Math.floor() or Math.ceil():
var bx1 = 489.999;
console.log(Math.floor(bx1));
console.log(Math.round(bx1));
console.log(Math.ceil(bx1));
floor() will round down your number.
round() will round your number (based on decimal value).
ceil() will round up your number.

Create a random number between -100 and 100 in JavaScript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating random numbers in Javascript
I have the following code var randomnumber=Math.floor(Math.random()*101); that generates a random number for me between 1 and 100. What I would like to do is generate a random number between -100 and 100. I am not sure what to do exactly. Any help would be appreciated.
First, generate a random number between 1 - 200 then subtract 100:
var randomNumber = Math.floor(Math.random() * 201) - 100;
var randomnumber=Math.floor(Math.random()*200)-100;

Categories

Resources