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])
Related
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));
This question already has answers here:
Generate random number between two numbers in JavaScript
(32 answers)
Closed 1 year ago.
this code is supposed to display a random character/ letter of a given name in javascript using while loops and if statements . . .
the problem that I faced is that the RandomLetterIndex is between 0 and 5 (<=5) when I want it to be between 0 and 4 (<5)
const MyName = "Ayman";
var RandomLetterIndex = Math.floor(Math.random() * 10);
while (RandomLetterIndex > MyName.length) {
RandomLetterIndex = Math.floor(Math.random() * 10);
if (RandomLetterIndex < MyName.length && RandomLetterIndex !== 5) {
break
}
}
console.log(RandomLetterIndex);
console.log(MyName.charAt(RandomLetterIndex));
If you want the random number to be less than the length of the word, instead of using while loops, you can do this
var RandomLetterIndex = Math.floor(Math.random()*MyName.length);
multiplying by length instead of 10 makes sure that the value always lies in the range [0, length-1] instead of [0, 10-1]
The problem is with the 0 based index and the length property. MyName.length will equate to 5 and thus the while loop will stop and the consoles print out.
while (RandomLetterIndex > MyName.length - 1) {
Try like this with the minus 1.
Your while loop ends when RandomLetterIndex is 5. Thats why you see a five in the console.
Also, you are breaking the loop, and therefore the while check is kind of useless.
This question already has answers here:
JavaScript format number to day with always 3 digits [duplicate]
(6 answers)
Closed 4 years ago.
I have a problem in my slot machine,
How to make it always display 3 digit
For example, I random the number 001 - 200
I want it to show 001 from start
and I want to display number like this 002 010 067 when it stop random
$('.reel-container:first').slotMachine(001).toString();
$('#gen').click(function() {
$('.reel-container:first').slotMachine((Math.floor(Math.random() * 200) + 1).toString());
});
My jsfiddle https://jsfiddle.net/xmenzaa/mrs93b58/11/
Thanks
use this function to generate random number
function randGen(){
var randNum = (Math.floor(Math.random() * 200) + 1).toString()
if(a.toString().length==3){
return randNum;
}
else if(a.toString().length==2){
return "0"+ randNum;
}
else if(a.toString().length==1){
return "00"+ randNum;
}
}
this code generate 3 digit random number and use it in
$('.reel-container:first').slotMachine(randGen());
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 6 years ago.
I'm getting large numbers as input and want to display them with a small space for every step of thousand (every 3 digits). So I'm looking for an array of max. three digits as output.
Examples:
Input: 10 Output: [10]
Input: 1234 Output: [1, 234]
Input: 24521280 Output: [23, 521, 280]
It does not matter if the output array contains strings or numbers.
What would be a most elegant (comprehensive / short) solution in Javascript ES6?
I wrote two working solutions, but I feel that they are overly complicated or that I'm missing something:
Solution 1)
function numberToThreeDigitArray(number) {
if (number / 1000 < 1) return [number];
return [
...numberToThreeDigitArray(Math.floor(number / 1000)),
number % 1000
];
}
Solution 2)
function numberToThreeDigitArray(number) {
return number.toString().split('').reverse().map((char, i) => {
return i !== 0 && i % 3 === 0
? ' ' + char
: char;
}).join('').split('').reverse().join('').split(' ');
}
number.toLocaleString().split(',').map(num => +num) should do it for you.
See the toLocaleString MDN docs.
Example:
const arr = (24521280).toLocaleString().split(',').map(num => +num)
// [24, 521, 280]
// or
function getNumber(n) {
return n.toLocaleString().split(',').map(num => +num)
}
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
I can't figure out how to solve the following problem.
I have an array of numbers from 1 to 100.
I need to convert them to strings but to a length of 5.
So, for instance:
1 becomes 00001
2 becomes 00002
3 becomes 00003
4 becomes 00004
etc, etc..
It seems so easy but I cannot find a function. The best I found was .toFixed(n) which is the number of decimal points to use.
Here's a very simple padding function:
function padLeft(str, length, paddingCharacter) {
str = '' + str; //Make sure that we convert it to a string if it isn't
while (str.length < length) {
str = paddingCharacter + str; //Pad it
}
return str;
}
padLeft(123, 5, '0'); //00123