Skip to content

randomSubset

Subhajit Sahu edited this page Dec 28, 2022 · 1 revision

Pick an arbitrary subset.

Similar: randomSubset, subsets, isSubset.


function randomSubset(x, n, fr)
// x:  lists
// n:  number of entries [-1 ⇒ any]
// fr: random number generator ([0, 1))
const xlists = require('extra-lists');

var x = [['a', 'b', 'c', 'd'], [1, 2, 3, 4]];
xlists.randomSubset(x).map(c => [...c]);
// → [ [ 'b', 'c' ], [ 2, 3 ] ]

xlists.randomSubset(x, 3).map(c => [...c]);
// → [ [ 'a', 'b', 'd' ], [ 1, 2, 4 ] ]

xlists.randomSubset(x, 2).map(c => [...c]);
// → [ [ 'b', 'd' ], [ 2, 4 ] ]


References