Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.3/immutable.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
_immu = {};
_immu.sample = function(list, n) {
  if (n === undefined) return list.get(_immu.random(list.size - 1));
  return _immu.shuffle(list).slice(0, Math.max(0, n));
};
_immu.shuffle = function(list) {
  var set = list;
  var size = set.size;
  var shuffled = Immutable.List(Array(size));
  for (var index = 0, rand; index < size; index++) {
    rand = _immu.random(0, index);
    if (rand !== index) shuffled = shuffled.set(index, shuffled.get(rand));
    shuffled = shuffled.set(rand, set.get(index));
  }
  return shuffled;
};
_immu.random = function(min, max) {
  if (max == null) {
    max = min;
    min = 0;
  }
  return min + Math.floor(Math.random() * (max - min + 1));
};
var list = Immutable.List([1,2,3,4,5]);
console.log(_immu.sample(list, 3).toJS()); //toJS here is just to show it as an array
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers