Related
I have an array with names
const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];
const teams = players.length / 2; // -> 4 teams
I want to make teams of 2 people (randomly chosen) and make new arrays from the results -> team
function createTeams() {
for (let i = 0; i < teams; i++) {
for (let x = 0; x < 2; x++) {
// get a random player
selectedPlayer = players[Math.floor(Math.random() * players.length)];
console.log('Selected Player will be added to a team: ', selectedPlayer);
// delete this player from the array
while (players.indexOf(selectedPlayer) !== -1) {
players.splice(players.indexOf(selectedPlayer), 1);
}
// add this player to a new array
//?????
}
}
}
Anyone knows how to do this?
function pickTeams(array, teamSize) {
// Shuffle array, make a copy to not alter original our original array
const shuffled = [...array].sort( () => 0.5 - Math.random());
const teams = [];
// Create teams according to a team size
for (let i = 0; i < shuffled.length; i += teamSize) {
const chunk = shuffled.slice(i, i + teamSize);
teams.push(chunk);
}
return teams;
}
const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];
const teams = pickTeams(players, 2);
You can define a new Array which will contains the teams where you push the two players.
Note that it's better to pass the players array as a parameters of the function and make a swallow copy of it so it won't modify the player array.
const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];
function createTeams(players) {
const playersLeft = [...players]
const newTeams = []
const nbTeams = players.length / 2
for (let i=0; i<nbTeams; i++){
const player1 = playersLeft.splice(Math.floor(Math.random()*playersLeft.length),1)[0]
const player2 = playersLeft.splice(Math.floor(Math.random()*playersLeft.length),1)[0]
newTeams.push([player1, player2])
}
return newTeams
}
console.log(createTeams(players))
Edit : Improve version using a nbPlayerPerTeam parameter
const players = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8'];
function createTeams(players, nbPlayerPerTeam) {
const playersLeft = [...players]
const newTeams = []
const nbTeams = players.length / nbPlayerPerTeam
for (let i = 0; i < nbTeams; i++) {
const players = []
for (let j = 0; j < nbPlayerPerTeam; j++) {
const player = playersLeft.splice(Math.floor(Math.random() * playersLeft.length), 1)[0]
players.push(player)
}
newTeams.push(players)
}
return newTeams
}
console.log(createTeams(players, 3))
const players = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8'];
// Divide data by length
const divide = (arr, n) => arr.reduce((r, e, i) =>
(i % n ? r[r.length - 1].push(e) : r.push([e])) && r, []);
const shuffle = (arr) => {
// Deep copy an array using structuredClone
const array = structuredClone(arr);
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle
while (currentIndex != 0) {
// Pick a remaining element
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]
];
}
return array;
}
// Shuffle players
const shuffled = shuffle(players);
// Set a number of people in a team
const groups = divide(shuffled, players.length / 2);
groups.forEach((team, i) => console.log(`team ${i+1}`, JSON.stringify(team)));
Check this updated code, I hope it will work for you. Just create two dimensional array and push players.
const players = ['name1','name2','name3','name4','name5','name6','name7','name8'];
let multi_team=new Array; // new array for team of random 2 players
const teams = players.length / 2; // -> 4 teams
console.log(teams);
createTeams();
function createTeams() {
for (let i = 0; i < teams; i++) {
multi_team[i]= new Array;
for (let x = 0; x < 2; x++) {
// get a random player
selectedPlayer = players[Math.floor(Math.random() * players.length)];
multi_team[i][x]=selectedPlayer;
// delete this player from the array
while (players.indexOf(selectedPlayer) !== -1) {
players.splice(players.indexOf(selectedPlayer), 1);
}
}
}
console.log(multi_team);
}
To differ from previous answers, this is a dynamic way to do it, so you don't care if there are 5 teams or 2, 10 players or 500.
const players = ['j1', 'j2', 'j3', 'j4', 'j5', 'j6', 'j7', 'j8'];
const organizeTeams = (numberOfTeams, players) => {
var teams = [];
// Clone the array to avoid mutations
const freePlayers = [...players];
// How many players will play per team
const playersPerTeam = Math.floor(players.length / numberOfTeams);
// How many player won't have team
const unorganizablePlayers = freePlayers.length % numberOfTeams;
for (let t = 1; t <= numberOfTeams; t++) {
teams = [...teams, pickRandomPlayers(freePlayers, playersPerTeam)];
}
return { teams, playersPerTeam, unorganizablePlayers };
};
const pickRandomPlayers = (players, playersPerTeam) => {
let pickedPlayers = [];
for (let c = 1; c <= playersPerTeam; c++) {
const index = Math.floor(Math.random() * (players.length - 1));
const player = players[index];
pickedPlayers = [...pickedPlayers, player];
// When we pick the player we remove it from the array to avoid duplicates.
players.splice(index, 1);
}
return pickedPlayers;
};
const championship = organizeTeams(3, players);
console.log(`We have ${championship.teams.length} teams.`);
championship.teams.forEach((team, index) => {
console.log(`Team ${index + 1} players: ${team.join(',')}`);
});
console.log(
`There was no place to assign ${championship.unorganizablePlayers} players`
);
The OP needs (to implement ) a reliable shuffle and likewise an array's chunk functionality.
Then one just has to shuffle the provided players array (or a shallow copy of the latter) and to divide the array of randomly ordered player items into chunks, each of custom provided length.
function shuffleArray(arr) {
let idx;
let count = arr.length;
while (--count) {
idx = Math.floor(Math.random() * (count + 1));
[arr[idx], arr[count]] = [arr[count], arr[idx]];
}
return arr;
}
function createListOfChunkLists(arr, chunkLength = 0) {
chunkLength = Math.max(0, chunkLength);
return (chunkLength === 0 && []) ||
arr.reduce((list, item, idx) => {
if (idx % chunkLength === 0) {
list.push([ item ]);
} else {
list[list.length - 1].push(item);
// `at` still not supported by safari.
// list.at(-1).push(item);
}
return list;
}, []);
}
// ... OP ... I have an array with names
const players = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6', 'name7', 'name8'];
// ... OP ... I want to make teams of 2 people (randomly chosen)
// and make new arrays from the results -> team
const teams = createListOfChunkLists(
shuffleArray([...players]), 2
);
console.log({ teams, players });
console.log({
teams: createListOfChunkLists(
shuffleArray([...players]), 2
),
players
});
console.log({
teams: createListOfChunkLists(
shuffleArray([...players]), 4
),
players
});
.as-console-wrapper { min-height: 100%!important; top: 0; }
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);
I have an array of object with each holding rgb values and I want to get the average of channel.
[{ r: 234, g: 250, b: 0 }, { r: 234, g: 250, b: 0 }, { r: 234, g: 250, b: 0 }]
The straight forward method is to map through the array, get the sum of each of the r, g and b values, then divide each by the length of the array.
const arrLength = colorBlock.length
let redArr = colorBlock.map(obj => obj.r)
let greenArr = colorBlock.map(obj => obj.g)
let blueArr = colorBlock.map(obj => obj.b)
const add = (total, num) => total + num;
const totalR = redArr.reduce(add, 0);
const totalG = greenArr.reduce(add, 0);
const totalB = blueArr.reduce(add, 0);
const averageR = parseInt(totalR / arrLength)
const averageG = parseInt(totalG / arrLength)
const averageB = parseInt(totalB / arrLength)
My problem with this is that it's very slow when I have a big color block, a 900 x 900 block took about 5 seconds. Is there a more efficient way to do this?
EDIT: Sorry guy I make a mistake, the causes for my slow down actually came from the function that create the color block, not the average calculation. The calculation only took a few hundred millisecond.
Drop the separation of channels. Traversing one existing array once is likely more efficient than creating and filling 3 new arrays (which itself means traversing the original array 3 times), and then traversing all of them again.
let totals=colorBlock.reduce(
(totals,current)=>{
totals[0]+=current.r;
totals[1]+=current.g;
totals[2]+=current.b;
return totals;
},[0,0,0]);
let averageR=totals[0]/totals.length;
let averageG=totals[1]/totals.length;
let averageB=totals[2]/totals.length;
Your approach has the optimal time complexity. You could gain a factor of speed by avoiding callback functions, and relying on the plain old for loop (not the in or of variant, but the plain one):
const arrLength = colorBlock.length;
let totalR = 0, totalG = 0, totalB = 0;
for (let i = 0; i < arrLength; i++) {
let rgb = colorBlock[i];
totalR += rgb.r;
totalG += rgb.g;
totalB += rgb.b;
}
const averageR = Math.floor(totalR / arrLength);
const averageG = Math.floor(totalG / arrLength);
const averageB = Math.floor(totalB / arrLength);
You may at most halve the time tp process a 900x900 input with this, but that's about it.
To really improve more, you will need to rely on some heuristic that says that most of the time neighboring pixels will have about the same color code. And so you would then skip pixels. That will give you an estimated average, but that might be good enough for your purposes.
Remark: don't use parseInt when the argument is numeric. This will unnecessarily convert the argument to string, only to convert it back to number. Instead use Math.floor.
Use Array.reduce() on the entire object all at once, then Array.map() to calculate the averages:
const obj = [{ r: 200, g: 161, b: 1 }, { r: 50, g: 0, b: 3 }, { r: 50, g: 0, b: 5 }]
const res = obj.reduce((acc,cur) => {
acc[0] += cur.r
acc[1] += cur.g
acc[2] += cur.b
return acc
},[0,0,0]).map(cur => Math.round(cur / obj.length))
console.log(res)
Using a simple for loop instead of map and reduce can save quite a bit of time.
//Generate random colors
function genColor() {
return Math.floor(Math.random() * 255);
}
const SIZE = 500000;
const colorBlock = new Array(SIZE);
for(let i = 0; i < SIZE; i++) {
colorBlock[i] = {r:genColor(), g:genColor(), b:genColor()};
}
const arrLength = colorBlock.length;
var startTime0 = performance.now();
let redArr0 = colorBlock.map(obj => obj.r)
let greenArr0 = colorBlock.map(obj => obj.g)
let blueArr0 = colorBlock.map(obj => obj.b)
const add = (total, num) => total + num;
const totalR0 = redArr0.reduce(add, 0);
const totalG0 = greenArr0.reduce(add, 0);
const totalB0 = blueArr0.reduce(add, 0);
const averageR0 = Math.floor(totalR0 / arrLength)
const averageG0 = Math.floor(totalG0 / arrLength)
const averageB0 = Math.floor(totalB0 / arrLength)
var endTime0 = performance.now();
var totalR1 = 0;
var totalG1 = 0;
var totalB1 = 0;
for(let i = 0; i < SIZE; i++) {
totalR1 += colorBlock[i].r;
totalG1 += colorBlock[i].g;
totalB1 += colorBlock[i].b;
}
const averageR1 = Math.floor(totalR1 / arrLength)
const averageG1 = Math.floor(totalG1 / arrLength)
const averageB1 = Math.floor(totalB1 / arrLength)
var endTime1 = performance.now();
console.log( averageR0, averageG0, averageB0)
console.log( averageR1, averageG1, averageB1)
console.log("Reduce", endTime0 - startTime0);
console.log("for loop", endTime1 - endTime0);
I have a array which updates every minute. When i want to show it over a day, I want to have the average of every hour that day.
The most recent minute is add the end of the array.
//get the last elements from the array
var hours= (this.today.getHours() + 1) * 60
var data = Array.from(this.temps.data)
let lastData = data.slice(Math.max(data.length - hours))
let newData: any
// calculate the average of every hour
for (let i = 0; i < minutes; i++) {
var cut = i * 60
for (let i = cut; i < (cut + 60); i++) {
newData = newData + lastData[i];
let test = newData/60
console.log(test);
}
}
I can't figure out how I make an array from every last 60 elements.
My goal is to get an array like
avgHour[20,22,30,27,]
The array I have is updated every minute. So I need the average of every 60 elements to get a hour.
array looks like this
data[25,33,22,33]
It is every minute from a week so really long.
This Worked For me
var arrays = [], size = 60;
while (arr.length > 0){
arrays.push(arr.splice(0, size));
}
for (let i = 0; i < (arrays.length - 1); i++) {
var sum = 0
for (let b = 0; b < 60; b++) {
sum += arrays[i][b]
}
let avg = sum/60
arr2.push(avg)
}
this just splits the array every 60 elements. Now I can calculate the average for every 60.
duplicate of How to split a long array into smaller arrays, with JavaScript
Thanks for the help!
I am a big fan of the functional programming library Ramda. (Disclaimer: I'm one of its authors.) I tend to think in terms of simple, reusable functions.
When I think of how to solve this problem, I think of it through a Ramda viewpoint. And I would probably solve this problem like this:
const avgHour = pipe(
splitEvery(60),
map(mean),
)
// some random data
const data = range(0, 7 * 24 * 60).map(_ => Math.floor(Math.random() * 20 + 10))
console.log(avgHour(data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script>const {pipe, splitEvery, map, mean, range} = R</script>
I think that is fairly readable, at least once you understand that pipe creates a pipeline of functions, each handing its result to the next one.
Now, there is often not a reason to include a large library like Ramda to solve a fairly simple problem. But all the functions used in that version are easily reusable. So it might make sense to try to create your own versions of these functions and keep them available to the rest of your application. In fact, that's how libraries like Ramda actually get built.
So here is a version that has simple implementations of those functions, ones you might place in a utility library:
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x)
const splitEvery = (n) => (xs) => {
let i = 0, a = []
while (i < xs.length) {a.push(xs.slice(i, i + n)); i += n}
return a
}
const map = (fn) => (xs) => xs.map(x => fn(x))
const sum = (xs) => xs.reduce((a, b) => a + b, 0)
const mean = (xs) => sum(xs) / (xs.length || 1)
const avgHour = pipe(
splitEvery(60),
map(mean)
)
const range = (lo, hi) => [...Array(hi - lo)].map((_, i) => lo + i)
// some random data
const data = range(0, 7 * 24 * 60).map(_ => Math.floor(Math.random() * 20 + 10))
console.log(avgHour(data))
You can reduce the data and group by hour, then simply map to get each hour's average. I'm using moment to parse the dates below, you can do that with whatever lib/js you prefer...
const arr = Array.from({length: 100}, () => ({time: moment().subtract(Math.floor(Math.random() * 10), 'hours'), value: Math.floor(Math.random() * 100)}));
const grouped = [...arr.reduce((a, b) => {
let o = a.get(b.time.get('hour')) || {value: 0, qty: 0};
a.set(b.time.get('hour'), {value: o.value + b.value, qty: o.qty + 1});
return a;
}, new Map)].map(([k, v]) => ({
[k]: v.value / v.qty
}));
console.log(grouped)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
By grouping and then reducing you can do this like following.
function groupBy(list, keyGetter) {
const map = {};
list.forEach((item) => {
const key = keyGetter(item);
if (!map[key]) {
map[key] = [item];
} else {
map[key].push(item);
}
});
return map;
}
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
const now = (new Date()).getTime();
const stepSize = 60000*1;
const withTime = data.reverse().map((x, i) => { return { time: new Date(now - stepSize * i), temp: x } });
const grouped = groupBy(withTime, x => new Date(x.time.getFullYear(), x.time.getMonth(), x.time.getDate(), x.time.getHours()).valueOf());
const avg = Object.entries(grouped).map((x) => {
return {
time: new Date(Number(x[0])),
temp: x[1].map(y => y.temp).reduce((acc, val) => acc + val) * (1.0 / x[1].length)
}
});
console.log(avg);
To measure the average I needed to split the array every 60 elements.
This is the solution I found
//Calculate the average of every 60 elements to get the average of an hour
var arr2: number[] = []
var arr: number[] = []
arr = Array.from(this.temps.data)
var arrays = [], size = 60;
while (arr.length > 0){
arrays.push(arr.splice(0, size));
}
for (let i = 0; i < (arrays.length - 1); i++) {
var sum = 0
for (let b = 0; b < 60; b++) {
sum += arrays[i][b]
}
let avg = sum/60
arr2.push(avg)
}
After all I think its stupid to get the last elements of the array, Because this is a better solution. But thanks for the help!
What is a clean way of taking a random sample, without replacement from an array in javascript? So suppose there is an array
x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
and I want to randomly sample 5 unique values; i.e. generate a random subset of length 5. To generate one random sample one could do something like:
x[Math.floor(Math.random()*x.length)];
But if this is done multiple times, there is a risk of a grabbing the same entry multiple times.
I suggest shuffling a copy of the array using the Fisher-Yates shuffle and taking a slice:
function getRandomSubarray(arr, size) {
var shuffled = arr.slice(0), i = arr.length, temp, index;
while (i--) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(0, size);
}
var x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var fiveRandomMembers = getRandomSubarray(x, 5);
Note that this will not be the most efficient method for getting a small random subset of a large array because it shuffles the whole array unnecessarily. For better performance you could do a partial shuffle instead:
function getRandomSubarray(arr, size) {
var shuffled = arr.slice(0), i = arr.length, min = i - size, temp, index;
while (i-- > min) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(min);
}
A little late to the party but this could be solved with underscore's new sample method (underscore 1.5.2 - Sept 2013):
var x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var randomFiveNumbers = _.sample(x, 5);
In my opinion, I do not think shuffling the entire deck necessary. You just need to make sure your sample is random not your deck. What you can do, is select the size amount from the front then swap each one in the sampling array with another position in it. So, if you allow replacement you get more and more shuffled.
function getRandom(length) { return Math.floor(Math.random()*(length)); }
function getRandomSample(array, size) {
var length = array.length;
for(var i = size; i--;) {
var index = getRandom(length);
var temp = array[index];
array[index] = array[i];
array[i] = temp;
}
return array.slice(0, size);
}
This algorithm is only 2*size steps, if you include the slice method, to select the random sample.
More Random
To make the sample more random, we can randomly select the starting point of the sample. But it is a little more expensive to get the sample.
function getRandomSample(array, size) {
var length = array.length, start = getRandom(length);
for(var i = size; i--;) {
var index = (start + i)%length, rindex = getRandom(length);
var temp = array[rindex];
array[rindex] = array[index];
array[index] = temp;
}
var end = start + size, sample = array.slice(start, end);
if(end > length)
sample = sample.concat(array.slice(0, end - length));
return sample;
}
What makes this more random is the fact that when you always just shuffling the front items you tend to not get them very often in the sample if the sampling array is large and the sample is small. This would not be a problem if the array was not supposed to always be the same. So, what this method does is change up this position where the shuffled region starts.
No Replacement
To not have to copy the sampling array and not worry about replacement, you can do the following but it does give you 3*size vs the 2*size.
function getRandomSample(array, size) {
var length = array.length, swaps = [], i = size, temp;
while(i--) {
var rindex = getRandom(length);
temp = array[rindex];
array[rindex] = array[i];
array[i] = temp;
swaps.push({ from: i, to: rindex });
}
var sample = array.slice(0, size);
// Put everything back.
i = size;
while(i--) {
var pop = swaps.pop();
temp = array[pop.from];
array[pop.from] = array[pop.to];
array[pop.to] = temp;
}
return sample;
}
No Replacement and More Random
To apply the algorithm that gave a little bit more random samples to the no replacement function:
function getRandomSample(array, size) {
var length = array.length, start = getRandom(length),
swaps = [], i = size, temp;
while(i--) {
var index = (start + i)%length, rindex = getRandom(length);
temp = array[rindex];
array[rindex] = array[index];
array[index] = temp;
swaps.push({ from: index, to: rindex });
}
var end = start + size, sample = array.slice(start, end);
if(end > length)
sample = sample.concat(array.slice(0, end - length));
// Put everything back.
i = size;
while(i--) {
var pop = swaps.pop();
temp = array[pop.from];
array[pop.from] = array[pop.to];
array[pop.to] = temp;
}
return sample;
}
Faster...
Like all of these post, this uses the Fisher-Yates Shuffle. But, I removed the over head of copying the array.
function getRandomSample(array, size) {
var r, i = array.length, end = i - size, temp, swaps = getRandomSample.swaps;
while (i-- > end) {
r = getRandom(i + 1);
temp = array[r];
array[r] = array[i];
array[i] = temp;
swaps.push(i);
swaps.push(r);
}
var sample = array.slice(end);
while(size--) {
i = swaps.pop();
r = swaps.pop();
temp = array[i];
array[i] = array[r];
array[r] = temp;
}
return sample;
}
getRandomSample.swaps = [];
Or... if you use underscore.js...
_und = require('underscore');
...
function sample(a, n) {
return _und.take(_und.shuffle(a), n);
}
Simple enough.
You can get a 5 elements sample by this way:
var sample = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
.map(a => [a,Math.random()])
.sort((a,b) => {return a[1] < b[1] ? -1 : 1;})
.slice(0,5)
.map(a => a[0]);
You can define it as a function to use in your code:
var randomSample = function(arr,num){ return arr.map(a => [a,Math.random()]).sort((a,b) => {return a[1] < b[1] ? -1 : 1;}).slice(0,num).map(a => a[0]); }
Or add it to the Array object itself:
Array.prototype.sample = function(num){ return this.map(a => [a,Math.random()]).sort((a,b) => {return a[1] < b[1] ? -1 : 1;}).slice(0,num).map(a => a[0]); };
if you want, you can separate the code for to have 2 functionalities (Shuffle and Sample):
Array.prototype.shuffle = function(){ return this.map(a => [a,Math.random()]).sort((a,b) => {return a[1] < b[1] ? -1 : 1;}).map(a => a[0]); };
Array.prototype.sample = function(num){ return this.shuffle().slice(0,num); };
While I strongly support using the Fisher-Yates Shuffle, as suggested by Tim Down, here's a very short method for achieving a random subset as requested, mathematically correct, including the empty set, and the given set itself.
Note solution depends on lodash / underscore:
Lodash v4
const _ = require('loadsh')
function subset(arr) {
return _.sampleSize(arr, _.random(arr.length))
}
Lodash v3
const _ = require('loadsh')
function subset(arr) {
return _.sample(arr, _.random(arr.length));
}
If you're using lodash the API changed in 4.x:
const oneItem = _.sample(arr);
const nItems = _.sampleSize(arr, n);
https://lodash.com/docs#sampleSize
A lot of these answers talk about cloning, shuffling, slicing the original array. I was curious why this helps from a entropy/distribution perspective.
I'm no expert but I did write a sample function using the indexes to avoid any array mutations — it does add to a Set though. I also don't know how the random distribution on this but the code was simple enough to I think warrant an answer here.
function sample(array, size = 1) {
const { floor, random } = Math;
let sampleSet = new Set();
for (let i = 0; i < size; i++) {
let index;
do { index = floor(random() * array.length); }
while (sampleSet.has(index));
sampleSet.add(index);
}
return [...sampleSet].map(i => array[i]);
}
const words = [
'confused', 'astonishing', 'mint', 'engine', 'team', 'cowardly', 'cooperative',
'repair', 'unwritten', 'detailed', 'fortunate', 'value', 'dogs', 'air', 'found',
'crooked', 'useless', 'treatment', 'surprise', 'hill', 'finger', 'pet',
'adjustment', 'alleged', 'income'
];
console.log(sample(words, 4));
Perhaps I am missing something, but it seems there is a solution that does not require the complexity or potential overhead of a shuffle:
function sample(array,size) {
const results = [],
sampled = {};
while(results.length<size && results.length<array.length) {
const index = Math.trunc(Math.random() * array.length);
if(!sampled[index]) {
results.push(array[index]);
sampled[index] = true;
}
}
return results;
}
Here is another implementation based on Fisher-Yates Shuffle. But this one is optimized for the case where the sample size is significantly smaller than the array length. This implementation doesn't scan the entire array nor allocates arrays as large as the original array. It uses sparse arrays to reduce memory allocation.
function getRandomSample(array, count) {
var indices = [];
var result = new Array(count);
for (let i = 0; i < count; i++ ) {
let j = Math.floor(Math.random() * (array.length - i) + i);
result[i] = array[indices[j] === undefined ? j : indices[j]];
indices[j] = indices[i] === undefined ? i : indices[i];
}
return result;
}
You can remove the elements from a copy of the array as you select them. Performance is probably not ideal, but it might be OK for what you need:
function getRandom(arr, size) {
var copy = arr.slice(0), rand = [];
for (var i = 0; i < size && i < copy.length; i++) {
var index = Math.floor(Math.random() * copy.length);
rand.push(copy.splice(index, 1)[0]);
}
return rand;
}
For very large arrays, it's more efficient to work with indexes rather than the members of the array.
This is what I ended up with after not finding anything I liked on this page.
/**
* Get a random subset of an array
* #param {Array} arr - Array to take a smaple of.
* #param {Number} sample_size - Size of sample to pull.
* #param {Boolean} return_indexes - If true, return indexes rather than members
* #returns {Array|Boolean} - An array containing random a subset of the members or indexes.
*/
function getArraySample(arr, sample_size, return_indexes = false) {
if(sample_size > arr.length) return false;
const sample_idxs = [];
const randomIndex = () => Math.floor(Math.random() * arr.length);
while(sample_size > sample_idxs.length){
let idx = randomIndex();
while(sample_idxs.includes(idx)) idx = randomIndex();
sample_idxs.push(idx);
}
sample_idxs.sort((a, b) => a > b ? 1 : -1);
if(return_indexes) return sample_idxs;
return sample_idxs.map(i => arr[i]);
}
My approach on this is to create a getRandomIndexes method that you can use to create an array of the indexes that you will pull from the main array. In this case, I added a simple logic to avoid the same index in the sample. this is how it works
const getRandomIndexes = (length, size) => {
const indexes = [];
const created = {};
while (indexes.length < size) {
const random = Math.floor(Math.random() * length);
if (!created[random]) {
indexes.push(random);
created[random] = true;
}
}
return indexes;
};
This function independently of whatever you have is going to give you an array of indexes that you can use to pull the values from your array of length length, so could be sampled by
const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
getRandomIndexes(myArray.length, 3).map(i => myArray[i])
Every time you call the method you are going to get a different sample of myArray. at this point, this solution is cool but could be even better to sample different sizes. if you want to do that you can use
getRandomIndexes(myArray.length, Math.ceil(Math.random() * 6)).map(i => myArray[i])
will give you a different sample size from 1-6 every time you call it.
I hope this has helped :D
Underscore.js is about 70kb. if you don't need all the extra crap, rando.js is only about 2kb (97% smaller), and it works like this:
console.log(randoSequence([8, 6, 7, 5, 3, 0, 9]).slice(-5));
<script src="https://randojs.com/2.0.0.js"></script>
You can see that it keeps track of the original indices by default in case two values are the same but you still care about which one was picked. If you don't need those, you can just add a map, like this:
console.log(randoSequence([8, 6, 7, 5, 3, 0, 9]).slice(-5).map((i) => i.value));
<script src="https://randojs.com/2.0.0.js"></script>
D3-array's shuffle uses the Fisher-Yeates shuffle algorithm to randomly re-order arrays. It is a mutating function - meaning that the original array is re-ordered in place, which is good for performance.
D3 is for the browser - it is more complicated to use with node.
https://github.com/d3/d3-array#shuffle
npm install d3-array
//import {shuffle} from "d3-array"
let x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
d3.shuffle(x)
console.log(x) // it is shuffled
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.0.0/d3.min.js"></script>
If you don't want to mutate the original array
let x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
let shuffled_x = d3.shuffle(x.slice()) //calling slice with no parameters returns a copy of the original array
console.log(x) // not shuffled
console.log(shuffled_x)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.0.0/d3.min.js"></script>