Solarce
Solarce

Reputation: 31

How do you not get repeating numbers from math.floor?

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);

Upvotes: 2

Views: 147

Answers (3)

Solarce
Solarce

Reputation: 31

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;
}}

Upvotes: 1

mankowitz
mankowitz

Reputation: 2031

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
    }
}

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370679

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);

Upvotes: 2

Related Questions