Random Numbers without repeat. Beginner Lvl
You can create an array of the numbers that you want to choose from. Then simply remove the number when it is pulled.
function lottoGame(max_numbers) {
let lottoArray = [];
let numbers = [];
let max_num = 6;
for (var i = 1; i <= max_num; i++) {
numbers.push(i);
}
nums = [];
for (let x = 1; x <= max_numbers; x++) {
rand = Math.floor(Math.random() * numbers.length);
num = numbers[rand];
nums.push(num);
numbers.splice(rand, 1);
}
document.querySelector('.loti').innerHTML = nums.join(",")
}
lottoGame(5)
<div class="loti"></div>
JavaScript generate random numbers without repeating
use a variable to check if the number is the same.
something like this: (using LastNumber to store the lastNumber) if it allready is used we gonna try again)
var videoLinks = [
....
];
var lastNumber = 0;
var randomNumber = function () {
var getRandomNumber = Math.floor(Math.random() * 5);
if(getRandomNumber != lastNumber){
var random = videoLinks[getRandomNumber];
document.getElementById("videoWrapper").innerHTML = random[0];
lastNumber = getRandomNumber;
}else{
randomNumber();
}
};
randomNumber(); // To call the function on load
How to randomly generate numbers without repetition in javascript?
Generate a range of numbers:
var numbers = [1, 2, 3, 4];
And then shuffle it:
function shuffle(o) {
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
var random = shuffle(numbers);
Generate a non-repeating random number in JavaScript
You have 2 mistakes, oné is the array inside the function this cleared for each try, and then there is wrong logic ending up in an infinite loop.
const usedIndexes = [];
function getUniqueRandomNumber(x) {
const index = Math.floor(Math.random() * (x));
if (usedIndexes.includes(index)) {
return this.getUniqueRandomNumber(x);
} else {
console.log(index);
usedIndexes.push(index);
return index;
}
}
Also, I would think about using Set
, in this situation instead of the array.
const usedIndexes = new Set();
function getUniqueRandomNumber(max, min = 0) {
const newNumber = Math.floor(Math.random() * (max - min) + min);
if (usedIndexes.has(newNumber)) {
return this.getUniqueRandomNumber(max, min);
} else {
usedIndexes.add(newNumber);
return newNumber;
}
}
I have also edited variables names to better reflect their actual use and added a minimum for a random number.
Can it be that difficult to generate 10 random numbers, no repeat?
You can repeatedly add numbers to a Set, and stop when its size reaches 10:
const set = new Set();while (set.size !== 10) { set.add(Math.floor(Math.random() * 100));}const numbers = [...set];console.log(numbers);
Related Topics
How to Disable and Enable a Click Event on a Table Td
Running Functions One After Other in React
Formdata Created from an Existing Form Seems Empty When I Log It
React Native: Image Not Displaying With Dynamic/Local Uri
How to Combine First Name and Last Name in JavaScript
How to Get the Id of a Button That Was Clicked - Reactjs
Textarea With Initial Auto Height by Content With Pure CSS
Expand Div on Click With Smooth Animation
Datatables - Search in Multiple Columns With a Single Drop Down
Django Template JavaScript Passing a Python Variable to a JavaScript One
How to Format Numbers by Prepending 0 to Single-Digit Numbers
Calculate Percentage JavaScript
Reactjs: Expected Onclick Listener to Be a Function, Instead Got Type String
How to Get Width of (Dom) Element in Angular2
How to Instantiate a JavaScript Class in Another Js File
Possible to Detect If a User Has Multiple Tabs of Your Site Open