For example: To generate 8 unique random numbers and store them to an array, you can simply do this:

var arr = [];
while(arr.length < 8){
    var r = Math.floor(Math.random() * 100) + 1;
    if(arr.indexOf(r) === -1) arr.push(r);
}
console.log(arr);

Answer from adam0101 on Stack Overflow
🌐
Medium
mavtipi.medium.com › how-to-generate-unique-random-numbers-in-a-specified-range-in-javascript-80bf1a545ae7
How to generate unique random numbers in a specified range in Javascript | by Mav Tipi | Medium
March 23, 2020 - In Ruby this would be as simple as random(16), but in JavaScript we have to write this function ourselves. And we just did! One second though. What I wanted was a number between one and fifteen, not zero and fifteen. I can achieve that by adding one to the whole thing and subtracting one from the top end: function myRandomIntByMax(n){ return Math.floor(Math.random() * n) + 1 }myRandomIntByMax(15) // 14 · Alright. We’ve got a general-purpose random number generator, and we know how to adjust the range, now comes the hard part.
Discussions

Generate unique random numbers in javascript or typescript - Stack Overflow
This will deal with uniqueness Create GUID / UUID in JavaScript?, then you can transform it to a number if you need another format, but I think the uniqueness spirit should come from overthere ;) ... There are many examples around internet. You can start with using Math.random() . For example: generate ... More on stackoverflow.com
🌐 stackoverflow.com
Generating Unique Random Numbers - JavaScript - SitePoint Forums | Web Development & Design Community
I’m trying to generate 5 unique random numbers between two numbers. The sixth number is a random number between another set of numbers but it doesn’t have to be unique. I tried to ensure the uniqueness of each number by placing the first randomly generated number in an array. More on sitepoint.com
🌐 sitepoint.com
0
May 11, 2021
Create a unique number with javascript time - Stack Overflow
I need to generate unique id numbers on the fly using javascript. In the past, I've done this by creating a number using time. The number would be made up of the four digit year, two digit month, ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - JS function to generate unique random number - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most · Connect and share knowledge within a single location that is structured and easy to search More on stackoverflow.com
🌐 stackoverflow.com
🌐
Smashing Magazine
smashingmagazine.com › 2024 › 08 › generating-unique-random-numbers-javascript-using-sets
Generating Unique Random Numbers In JavaScript Using Sets — Smashing Magazine
Want to create more randomized effects in your JavaScript code? The `Math.random()` method alone, with its limitations, won’t cut it for generating unique random numbers. Amejimaobari Ollornwi explains how to generate a series of unique random numbers using the `Set` object, how to use these random numbers as indexes for arrays, and explores some practical applications of randomization.
🌐
npm
npmjs.com › package › unique-random
unique-random - npm
April 8, 2024 - Useful for things like slideshows where you don't want to have the same slide twice in a row. ... import {consecutiveUniqueRandom} from 'unique-random'; const random = consecutiveUniqueRandom(1, 10); console.log(random(), random(), random()); //=> 5 2 6 · Generate random numbers that are consecutively unique, meaning that each number in the sequence is distinct from the one immediately before it.
      » npm install unique-random
    
Published   Apr 08, 2024
Version   4.0.0
Author   Sindre Sorhus
🌐
SitePoint
sitepoint.com › javascript
Generating Unique Random Numbers - JavaScript - SitePoint Forums | Web Development & Design Community
May 11, 2021 - I’m trying to generate 5 unique random numbers between two numbers. The sixth number is a random number between another set of numbers but it doesn’t have to be unique. I tried to ensure the uniqueness of each number by …
🌐
GitHub
github.com › sindresorhus › unique-random
GitHub - sindresorhus/unique-random: Generate random numbers that are consecutively unique
The returned function is also an iterable which consumes from the same source as the function: import {exhaustiveUniqueRandom} from 'unique-random'; const random = exhaustiveUniqueRandom(1, 10); for (const number of random) { console.log(number); ...
Starred by 116 users
Forked by 15 users
Languages   JavaScript 91.8% | TypeScript 8.2% | JavaScript 91.8% | TypeScript 8.2%
Find elsewhere
🌐
DEV Community
dev.to › niero › generating-unique-random-numbers-in-javascript-using-sets-2dcd
Generating Unique Random Numbers in JavaScript Using Sets - DEV Community
August 29, 2024 - This is where JavaScript's Set object comes in handy. In this article, we'll explore how to generate unique random numbers using Set and why it's an effective approach.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript - MDN Web Docs
Use the Web Crypto API instead, and more precisely the Crypto.getRandomValues() method. function getRandomInt(max) { return Math.floor(Math.random() * max); } console.log(getRandomInt(3)); // Expected output: 0, 1 or 2 console.log(getRandomInt(1)); // Expected output: 0 console.log(Math.random()); ...
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-generate-unique-random-numbers-between-1-and-100-with-javascript-ffa29c389833
How to Generate Unique Random Numbers Between 1 and 100 with JavaScript | by John Au-Yeung | JavaScript in Plain English
April 17, 2023 - Since Math.random only generates numbers between 0 and 1, we’ve to multiply the returned result by 100, round it down with Math.floor and add 1 to it. If the number isn’t already in arr as returned by indexOf , then we call push to add it to… ... New JavaScript and Web Development content every day.
🌐
Stack Overflow
stackoverflow.com › questions › 46809887 › js-function-to-generate-unique-random-number
javascript - JS function to generate unique random number - Stack Overflow
function getRandomNumber() { var array = []; let randomN = Utilities.generateRandomNumber(array, 5); return randomN; } I am then using this function inside a react component like so:
🌐
Nesin
nesin.io › blog › unique-random-numbers-javascript
Generate Unique Random Numbers in JavaScript - Nesin.io
July 8, 2023 - import uniqueRandom from 'unique-random'; const random = uniqueRandom(1, 25); console.log(random(), random(), random()); // 18 4 10 · Returns a function that returns a unique number, every time we call it. Happy generating numbers!
🌐
DEV Community
dev.to › rahmanfadhil › how-to-generate-unique-id-in-javascript-1b13
How to Generate Unique ID in JavaScript - DEV Community
December 22, 2019 - In JavaScript, we can use a library called uuid to generate UUID. ... UUID has several versions, but the version that appropriate for generating unique id is version 4. And, that code will generate something like this.
🌐
Codemzy
codemzy.com › blog › random-unique-id-javascript
How to generate a random unique ID with JavaScript - Codemzy's Blog
January 13, 2023 - Finally, we cut off the 0. at the start of the decimal number, and end the string at our length argument .substring(2, length+2) - (+2 for the 2 characters we lost at the start of string!). Now we can call the randomId function and get an ID ...
🌐
LearnersBucket
learnersbucket.com › home › examples › javascript › unique id generator in javascript
Unique id generator in javascript - LearnersBucket
June 21, 2019 - Javascript does not have any inbuilt method to generate unique ids, but it does a have method called Math.random() which generates a unique number every time called.