Very new to the site and javascript so. Been looking in the forum for a while but couldn't find the answer I was looking for, or at least couldn't understand the threads I'd read.
I'm tryng to make a generator to create as many combinations as needed of the arrays above without repeating the combination and using only one item of each array per iteration. There are also a few extra needs I'd add such as an unique id for the iteration and an extra property to mark the iterations where all the properties have the same value.
This is the code
var accesories = ["pijama" , "urban" , "joker" , "joyboy" , "crypto"];
var hats = accesories;
var tshirts = accesories;
var boots = accesories;
var cards = [];
function randomizeParts() {
model.accesories = accesories[Math.floor(Math.random() * 5)];
model.hats = hats[Math.floor(Math.random() * 5)];
model.tshirts = tshirts[Math.floor(Math.random() * 5)];
model.boots = boots[Math.floor(Math.random() * 5)];
};
function addInsomnio (quantity) {
for (let i = 1 ; i <= quantity ; i++){
model = {
id : 0,
accesories: 0,
hats: 0,
tshirts: 0,
boots: 0}
//adding four digits id
i < 10 ? model.id = '000' + i : i < 100 ? model.id = '00' + i : i < 1000 ? model.id = '0' + i : i <= 10000 ? model.id = i :false;
//randomizing parts
randomizeParts()
//checking if rarity was generated
model.accesories === model.hats && model.accesories === model.tshirts && model.accesories === model.boots ? model.rarity = "original" : false;
//checking its unique
// ????
//Pushing a beautifull brand new and unique card
cards.push(model);
}
};
is there a way to compare the random model to the existing objects in cards before pushing and so randomize it again as many times as needed if that combination already exist?
Note: this is planned to be used one time only to generate a 10,000 items json as a support for a photoshop script.
You can identify each combination with a unique number, and maintain a set of used numbers:
function choices(count, ...arrays) {
let used = new Set;
let size = arrays.reduce((size, arr) => size * arr.length, 1);
if (count > size) count = size;
let result = [];
for (let i = 0; i < count; i++) {
let k;
do {
k = Math.floor(Math.random() * size);
} while (used.has(k));
used.add(k);
result.push(arrays.reduce((acc, arr) => {
acc.push(arr[k % arr.length]);
k = Math.floor(k / arr.length);
return acc;
}, []));
}
return result;
}
let accesories = ["a", "b", "c", "d", "e"];
let hats = ["A", "B", "C", "D", "E"];
let tshirts = ["1", "2", "3", "4", "5"];
let boots = [".", ";", "?", "!"];
// Get 10 choices:
let result = choices(10, accesories, hats, tshirts, boots);
console.log(result);