This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(40 answers)
Closed 1 year ago.
So basically i got this function
Math.random()
we were taught to use this formula in order to get values from 1 to x
parseInt(Math.random()*x)+1;
but now what i want to do is to get values from x to y but i'm not sure how to do it .. any help ?
const doRandom = (min,max) => {
return Math.floor(Math.random() * (max - min) + min);
}
console.log(doRandom(5,9));
Related
This question already has answers here:
Javascript function to generate random integers with nonuniform probabilities
(3 answers)
Closed 4 years ago.
Hi I wanna get random numbers out of 2,3,5 . So i use this code.
function Random(low, hi) {
return Math.floor(Math.random() * (hi - low + 1) + low);
}
But this is only for 2 numbers , But i wanna get for 1 time for example 3 for 2 time 5, and 3 time 3 , but of course not exactly in that order . Any ideas ?
function random(numbers) {
return numbers[Math.floor(Math.random()*numbers.length)];
}
Then you can call it with a list:
random([2, 3, 5])
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 6 years ago.
So I'm trying to create a random number generator in html using javascript to randomise and alert the user of the number generated. However I always get an error when getting my number which I don't experience when using codecademy. The code is:
<input type="button" value="Enter" id="enter"
onClick="alert(getRandomInt(document.getElementById('min').value,
document.getElementById('max').value));">
</div>
<script type="text/javascript">
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
When 5 and 10 are input as min and max numbers respectively,for example,the resulting numbers are 15,25,05,35 etc. The first digits apparently is found using the function
Math.floor(Math.random() * (max - min + 1))
and the last digit would be the number 5 which is the minimum. So I was wondering if anyone could teach me how to put the + min into the entire operation so I can get a number within the range of 5-10? Thanks in advance
min is a string and by adding, you add a string, with subtraction the value is casted to Number.
You could add another + sign in front of min, that cast the value to Number
return Math.floor(Math.random() * (max - min + 1)) + +min;
// ^^^
This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 6 years ago.
I have this code:
function sellByte() {
if (player.bytes >= 1) {
player.bytes = player.bytes - 1;
player.money = player.money + 0.10;
document.getElementById("bytes").innerHTML = "Bytes: " + player.bytes;
document.getElementById("money").innerHTML = "$" + player.money;
}
}
And whenever I sell a Byte my money value ends up looking like $10.00000003 or something along those lines, how would I go about rounding the money value UP every time this function is run?
Working with float numbers in JS is very tricky. My suggestion is to operate only with smaller units (cents instead of dollars) and then you will only deal with integers and will not have similar issues.
Use Math.round(player.money* 100) / 100 for 2 decimal rounding.
Use any of the following code
Math.round(num * 100) / 100
using fixed Method
var numb = 123.23454;
numb = numb.toFixed(2);
or you can refer following link for more help
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
This question already has answers here:
Rounding numbers to 2 digits after comma
(10 answers)
Closed 9 years ago.
I want to round the up my total til two decimal places.
I have this:
var updateTotal = function () {
var people = parseInt($('#people').val());
var bill = parseInt($('#bill').val());
var tip = parseInt($('#tip').val());
var billTip = bill + tip;
$('#total').text(billTip / people);
and i've also found this snippet to help round up but i cant seem to get my head around how to implement it.
var rounded = Math.round((10 / 3) * 100) / 100;
Thanks
It's already implemented for you. Substitue (10 / 3) for your own variables. All it's doing is shifting the decimal place two places to the right (by multiplying by 100), rounding, then shifting it two left (by dividing by 100).
var rounded = Math.round((billTip / people) * 100) / 100;
You can also use .toFixed
$('#total').text((billTip / people).toFixed(2));
I would use parseFloat on both your numbers, or else it will round to 00:
$('#total').text(parseFloat(billTip / people).toFixed(2));
You can use ceil function Math.ceil(billTip)
and for refernce you can also visit below link
http://www.w3schools.com/jsref/jsref_ceil.asp
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you round to 1 decimal place in Javascript?
My Value is 1.450 and I have to round it to 1 decimal place.
I want 1.450 = 1.5 in Javascript can any body fix this please.
You need this:
var mynum = 1.450,
rounded = Math.round(mynum * 10) / 10;
suppose you have
var original=28.453;
Then
var result=Math.round(original*10)/10 //returns 28.5
From http://www.javascriptkit.com/javatutors/round.shtml
You can also see How do you round to 1 decimal place in Javascript?
Given your fiddle, the simplest change would be:
result = sub.toFixed(1) + "M";
to:
result = Math.ceil(sub.toFixed(1)) + "M";
If you use Math.round then you will get 1 for 1.01, and not 1.0.
If you use toFixed you run into rounding issues.
If you want the best of both worlds combine the two:
(Math.round(1.01 * 10) / 10).toFixed(1)
You might want to create a function for this:
function roundedToFixed(_float, _digits){
var rounder = Math.pow(10, _digits);
return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}