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;
Related
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.
This question already has answers here:
Javascript: Round by 100 [duplicate]
(2 answers)
Closed 2 years ago.
In adobe javascript calculation field, I'm trying to round a whole number to nearest 100th, example:
round $2946.16 to $2900
and $2955.42 to $3000
var DP2 = Number(this.getField("DownPaymentBeforeTTL").valueAsString);
var SST = Number(this.getField("SubmissionSalesTax").valueAsString);
event.value = (Math.round(DP2+SST))
Math.round only rounds decimals in adobe javascript calculation text field.
You could divide the number by 100, round it to get an integer, and then multiply back by 100:
var total = DP2 + SST;
event.value = Math.round(total / 100) * 100;
A little cheat
Math.round((DP2+SST)/100)*100
This question already has answers here:
Making a random number that's a multiple of 10
(7 answers)
Closed 5 years ago.
Guys, I wanted to know how can I generate a number, for example between 5 and 100 but only 5 by 5, for example, it would generate 5, 10, 15, 20 up to 100, but not any other number.
I've tried using Math.floor(Math.random() * (100 - 5 + 1)) + 5but i can't seem to figure out how it can generate 5 by 5 numbers and I really need it right now.
I'm still an amateur so please no flame.
function randomIntStep(min,max,step) {
return min + Math.floor(Math.random() * max / step) * step;
}
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.
This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 6 years ago.
I wan't to generate a random number and use it as a class name for an element.
Math.random();
This will generate a random number '0.7220265011042561'
Since this will be used as a class within the class attribute of an element, the decimal will surely cause problems and most likely isn't valid.
How can I generate a random whole number?
Math.round( Math.random()*10000000 )
This will generate a random whole number between 1 and 100.
Math.floor((Math.random() * 100) + 1);
Change the 100 part to redefine your range.
Add however many significant digits you want with the 100000 part:
Math.floor(Math.random() * 100000);
If you want them the same length, for smaller numbers (e.g., 5), here's a left-padded version:
var sigFig = // (put the number of significant digits you want here)
var number = Math.floor(Math.random() * Math.pow(10, sigFig));
var numStr = String(number);
while (numStr.length < sigFig) {
numStr = '0' + numStr;
}
// do stuff with numStr
Edit: Moved the significant figure to a variable.