I've deployed this algorithm for a specific situation, and I wanted to post here so can some one use it in his project. It's in JavaScript, but it can easily be converted to another language. Hope to be useful.
/* generating a random array from a source array, algorithm */
function createRandomArray(srcArray, amount) {
var rndArray = []; // random array
while (rndArray.length < amount) { // how many random items?
// generating a random index
const random_index = Math.floor(Math.random() * srcArray.length);
// if random array doesn't have random index then...
if (!rndArray.includes(random_index)) {
// push the current item from source array with random inex
rndArray.push(srcArray[random_index]);
// then remove the selected item from source array with random inex
srcArray.splice(random_index, 1);
}
}
return rndArray; // the output of this function is a an array with random items
}
const sourceArray = [1,2,3,4,5,6,7,8,9,0];
print(createRandomArray(sourceArray, 5));
// output [6, 1, 8, 2, 7]