How do I convert this simple python code into javascript? [duplicate] - javascript

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 8 years ago.
I was looking at python script when I have across this:
randomNumber = random.randint(2, number)-1
How can I translate this into javascript?
Thanks?

You can use the examples given in the Math.random docs page,
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
console.log(getRandomInt(2, number) - 1);

randomNumber = Math.floor(Math.random() * (1 + number))

Related

Print a random integer number between 1 and 42 (both inclusive) to the console [duplicate]

This question already has answers here:
Generating random integer in range that doesn't start at zero
(4 answers)
Closed 3 days ago.
I have a question in a exercise to print a random integer number between 1 and 42, but im getting an error in my console "Are you doing the multiplication inside the ceil() method call?"
My code is this one.
var someRandomNumber = Math.random();
var wholeRandomNumber = Math.ceil(someRandomNumber);
console.log(Math.ceil(Math.random() * 42));
I dont know what im doing wrong, honestly if someone can help me! :)
You should try this
function generateRandom(min = 0, max = 1000) {
// find diff
let difference = max - min;
// generate random number
let rand = Math.random();
// multiply with difference
rand = Math.floor( rand * difference);
// add with min value
rand = rand + min;
return rand;
}
console.log(generateRandom(min=1,max=42));

Math random add subtract counter limited between a range of numbers [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(40 answers)
Closed 2 years ago.
can you help me out on how to limit the below to stay within a min and max range eg 3 - 10
<div id="number">3</div>
setInterval(function(){
random = (Math.floor((Math.random()*1)+1));
var plusOrMinus = Math.random() < 0.5 ? -1 : 1;
random = random * plusOrMinus;
currentnumber = document.getElementById('number');
document.getElementById('number').innerHTML = parseInt(currentnumber.innerHTML) + random;
}, 1000);
See Mozilla Docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values
Getting a random integer between two values This example returns a random integer between the specified values. The value is no lower
than min (or the next integer greater than min if min isn't an
integer), and is less than (but not equal to) max.
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
By Mozilla Contributors, licensed under CC-BY-SA 2.5.

How is this code return at -2 to 2 number? [duplicate]

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
How does Math.floor( (Math.random() * 10) + 1 ); work?
(4 answers)
Closed 2 years ago.
How can this return a number between -2 and 2
var number = Math.random() * 4 - 2;
The method Math.random() gives you a decimal number between 0 and 1. If it returns, 0.00001, you'll get 0.00001 * 4 - 2 = 0.00004 - 2 = -1.99996
A pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.

How to get random index in a specific range only once [duplicate]

This question already has answers here:
Random number, which is not equal to the previous number
(16 answers)
Generate unique number within range (0 - X), keeping a history to prevent duplicates
(4 answers)
run for random numbers and keep state [duplicate]
(4 answers)
Closed 4 years ago.
Hi I would like to get an random index in a specific range of a list.
After that the index should not be returned again if I want another random index of a list.
Here is a way to generate random numbers in a [min, max) range and without repetition.
It uses a lookup object to store the values returned so far for each range.
If no number can be returned, it returns undefined.
const usedIndicesByRange = {};
function randomIntInRange(min, max) {
const key = `${min}-${max}`;
if (!(key in usedIndicesByRange)) {
usedIndicesByRange[key] = [];
}
if (usedIndicesByRange[key].length === max - min) {
return undefined;
}
const getIdx = () => Math.floor(Math.random() * (max - min) + min);
let idx = getIdx();
while (usedIndicesByRange[key].includes(idx)) {
idx = getIdx();
}
usedIndicesByRange[key].push(idx);
return idx;
}
console.log([...Array(12)].map(() => randomIntInRange(0, 10)));

Generate random number, between 2 numbers? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random numbers in Javascript in a specific range?
Say I wanted to generate a random number from 50 to 100.
I know there's:
Math.random()
but I could only find ways to go from 1 to 'x'
From MDN on Math.random():
// 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 believe this would also work:
function randomInt(min,range) {
return Math.floor((Math.random()*(range+1))+min)
}
In your case min would be 50 and range would be 50

Categories