0

I am just getting started in programming and ran into this issue. The answers posted on the forum don't really help me out so here goes my question. I am trying to create a function that generates four random numbers between 1-12. Once the numbers are generated, they are assigned to a individual variable.

var gem1;
var gem2;
var gem3;
var gem4;

function updateGem() {
    gem1 = Math.floor(Math.random() * 12 + 1);
    gem2 = Math.floor(Math.random() * 12 + 1);
    gem3 = Math.floor(Math.random() * 12 + 1);
    gem4 = Math.floor(Math.random() * 12 + 1);
}

As you can see sometimes the numbers generated are matching, which is not what I need. I need every var to have it's own unique number.

The value of gem 1 is 8
The value of gem 2 is 9
The value of gem 3 is 9
The value of gem 4 is 8
elpat7
  • 43
  • 5
  • that's the point of random its random. maybe try a higher boundary reduce chances of collision or store number and compare with previous generated, or generate one number and increment next 3 – joyBlanks Oct 13 '19 at 03:46
  • 1
    You could create an array with the numbers 1 through to 12. Then shuffle this array, and grab the first four elements. This way, you can get an array of 4 unique random numbers between the range from 1 to 12 – Nick Parsons Oct 13 '19 at 03:47
  • Possible duplicate of [Generate unique random numbers between 1 and 100](https://stackoverflow.com/questions/2380019/generate-unique-random-numbers-between-1-and-100) – ASDFGerte Oct 13 '19 at 04:03
  • 1
    Thanks @NickParsons that did the trick! – elpat7 Oct 13 '19 at 04:06

4 Answers4

2

You could do something like this:

function generateRandomUnique(size) {
  const unique = new Set();
  while (unique.size < size)  {
    unique.add(Math.floor(Math.random() * 12 + 1));
  }
  return [...unique];
}

const [gem1, gem2, gem3, gem4] = generateRandomUnique(4);
console.log(gem1, gem2, gem3, gem4);
Derek
  • 4,575
  • 3
  • 22
  • 36
0

Store the variables in an array and check for the number in the array

var gem1;
var gem2;
var gem3;
var gem4;
var arr=[];
function updateGem() {
arr=[];
    gem1 = Math.floor(Math.random() * 12 + 1);
    if(arr.includes(gem1))
    updateGem();
    else
    arr.push(gem1);
    gem2 = Math.floor(Math.random() * 12 + 1);
    if(arr.includes(gem2))
    updateGem();
    else
    arr.push(gem2);
    gem3 = Math.floor(Math.random() * 12 + 1);
    if(arr.includes(gem3))
    updateGem();
    else
    arr.push(gem3);
    gem4 = Math.floor(Math.random() * 12 + 1);
    if(arr.includes(gem4))
    updateGem();
    else
    arr.push(gem4);
    
}
updateGem()
console.log(gem1)
console.log(gem2)
console.log(gem3)
console.log(gem4)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

This is what I did to solve the issue:

function updateGem() {
    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    numbers.sort(() => {
        return 0.5 - Math.random();
    });

    gem1 = numbers[0];
    gem2 = numbers[1];
    gem3 = numbers[2];
    gem4 = numbers[3];

}
elpat7
  • 43
  • 5
0

Generate a random number based on an array length. Swap with the last index and now again generate a number based on array length reducing by 1.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var count = numbers.length;
function updateGem() {

    gem1 = getRandomNumber();
    gem2 = getRandomNumber();
    gem3 = getRandomNumber();
    gem4 = getRandomNumber();

    console.log(gem1, gem2, gem3, gem4);

}
function getRandomNumber() {

    if (count <= 0) {
        count = numbers.length;
    }
    var randomIndex = Math.round(count * Math.random());
    console.log("Random Number="+randomIndex);
    var temp = numbers[randomIndex]; 
    numbers[randomIndex] = numbers[count-1];
    numbers[count-1] = temp;
    count--; // next time do not generate this random number
    return temp;
}
Chetan Laddha
  • 993
  • 8
  • 22