2

My goal is to make these gems all have different values from 1-10

If you could help me out while still using mathfloor I would appreciate that as well

//Variables
var emGem = 0;
var ruGem = 0;
var diGem = 0;
var saGem = 0;
var possibleGem = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

//Initiate scores
emGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
ruGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
diGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
saGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];

//Debug
console.log(emGem);
console.log(ruGem);
console.log(diGem);
console.log(saGem);
console.log(randomNumber);
Liam
  • 27,717
  • 28
  • 128
  • 190
Solarce
  • 31
  • 2
  • Do shuffle them. – Bergi Jun 25 '18 at 09:39
  • With random anything, it's always possible to get the same output twice even if it is a very low probability, i.e. never 0 probability. – ggdx Jun 25 '18 at 09:40
  • Possible duplicate of [How to efficiently randomly select array item without repeats?](https://stackoverflow.com/questions/17891173/how-to-efficiently-randomly-select-array-item-without-repeats) – Liam Jun 25 '18 at 09:41

3 Answers3

2

You need to figure out a way to select elements without replacement. You might pick a random index, then splice the element at that index to get the element while removing it from the array:

var possibleGem = [1,2,3,4,5,6,7,8,9,10];

const randItem = () => {
  const randIndex = Math.floor(Math.random() * possibleGem.length);
  return possibleGem.splice(randIndex, 1)[0];
};

const emGem = randItem();
const ruGem = randItem();
const diGem = randItem();
const saGem = randItem();

//Debug
console.log(emGem);
console.log(ruGem);
console.log(diGem);
console.log(saGem);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Thanks everyone! I used all your answers to think about how I could write this myself because you guys were using things that I have not learned yet but I will be sure to check it out! If you are interested in how I decided to do it here it is.

while (true) {
emGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
ruGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
diGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
saGem = possibleGem[Math.floor(Math.random() * possibleGem.length)];
if  (emGem !== ruGem && emGem !== diGem && emGem !== saGem &&  ruGem !== diGem && ruGem !== saGem && diGem !== saGem) {
    break;
}}
Solarce
  • 31
  • 2
0

I would make an array of possible gems and then shuffle it. Each time I need a value, I'd just pop it off the array.

The shuffleArray() code is from How to randomize (shuffle) a JavaScript array?

//Variables
var emGem = 0;
var ruGem = 0;
var diGem = 0;
var saGem = 0;
var possibleGem = [1,2,3,4,5,6,7,8,9,10];
shuffleArray(possibleGem);

//Initiate scores
emGem = possibleGem.pop();
ruGem = possibleGem.pop();
diGem = possibleGem.pop();
saGem = possibleGem.pop();

//Debug
console.log(emGem);
console.log(ruGem);
console.log(diGem);
console.log(saGem);

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]]; // eslint-disable-line no-param-reassign
    }
}
mankowitz
  • 1,864
  • 1
  • 14
  • 32