Substitute Random Items In A Javascript Array
In Javascript, am trying to randomly substitute half (in this case, 3 out of 6) of the items from an array with different ones (all of the same type), and I need the original items
Solution 1:
You could take a closure over the array and wanted zero counts and return a function which generates random integer and map the array with zeros or values.
functiongetRandom(array, count) {
returnfunction () {
const indices = newSet();
do {
indices.add(Math.floor(Math.random() * array.length));
} while (indices.size < count)
return array.map((v, i) => indices.has(i) ? 0 : v);
};
}
var myArray = ['a', 'b', 'c', 'd', 'e', 'f'],
getArrayWithZeros = getRandom(myArray, 3);
console.log(...getArrayWithZeros());
console.log(...getArrayWithZeros());
console.log(...getArrayWithZeros());
console.log(...getArrayWithZeros());
console.log(...getArrayWithZeros());
Solution 2:
Another option
const myArray = ['a', 'b', 'c', 'd', 'e', 'f'];
// Randomizer functionconstrand = ([...arr], len, rep) => {
let ins = {};
while(Object.keys(ins).length < len) {
let r = ~~(Math.random() * arr.length);
if(!ins[r]) ins[r] = true;
}
for(let i = 0; i < arr.length; i++) if(ins[i]) arr[i] = rep;
return arr;
}
// Array, number of elements to replace, replace with// Here we transform toString for better readabilityconsole.log(rand(myArray, 3, 0).toString());
console.log(rand(myArray, 3, 0).toString());
console.log(rand(myArray, 3, 0).toString());
console.log(rand(myArray, 3, 0).toString());
console.log(rand(myArray, 3, 0).toString());
Solution 3:
You need to calculate half of the length
of the array
, generate random indexes
of this amount, and then change them:
var my_array = ['a', 'b', 'c', 'd', 'e', 'f'];
functionalterHalfWithZero(arr){
let my_array = [...arr];
let indexList = {};
let len = Math.floor(my_array.length / 2)
while(Object.values(indexList).length != len){
let random_index = Math.floor(Math.random() * my_array.length);
indexList[random_index] = random_index;
}
indexList = Object.values(indexList);
for(let i = 0; i < indexList.length; i++)
my_array[indexList[i]] = 0;
return my_array;
}
console.log(...alterHalfWithZero(my_array));
console.log(...alterHalfWithZero(my_array));
console.log(...alterHalfWithZero(my_array));
Solution 4:
First get unique random indexes. Loop over the indexes and make them 0.
var my_array = ["a", "b", "c", "d", "e", "f"];
// Get unique random indexesconstrandom = (num, count) => {
const set = newSet();
while (set.size < count) {
set.add(Math.floor(Math.random() * num));
}
return [...set];
};
constalter = (arr, count = 3) => {
const output = [...arr];
random(arr.length, count).forEach((index) => (output[index] = 0));
return output;
};
console.log(alter(my_array));
console.log(alter(my_array));
Solution 5:
Here is one solution.
var my_array = ["a", "b", "c", "d", "e", "f"];
constsubstitute = array => {
const indexes = Array.from(Array(array.length).keys())
.sort(() =>Math.random() - 0.5)
.slice(0, array.length / 2);
return array.map((value, index) =>
indexes.some(i => i === index) ? value : 0
);
};
console.log(substitute(my_array));
Post a Comment for "Substitute Random Items In A Javascript Array"