Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript从数组中拾取随机形状_Javascript_Arrays_Random_Underscore.js - Fatal编程技术网

使用Javascript从数组中拾取随机形状

使用Javascript从数组中拾取随机形状,javascript,arrays,random,underscore.js,Javascript,Arrays,Random,Underscore.js,我想从数组中选择两三个形状,但我不能。我尝试了以下代码: var range = _.sample(shapes, 2); 此代码可以运行,但不是随机的 var range = shapes.length-2 * Math.random(); 这将从数组中获得1个随机元素 var shape = shapes[Math.floor(Math.random() * shapes.length)]; 您可以将其转换为一个函数,使其更易于使用 function sample(arr) { r

我想从数组中选择两三个形状,但我不能。我尝试了以下代码:

var range = _.sample(shapes, 2);
此代码可以运行,但不是随机的

var range = shapes.length-2 * Math.random();

这将从数组中获得1个随机元素

var shape = shapes[Math.floor(Math.random() * shapes.length)];
您可以将其转换为一个函数,使其更易于使用

function sample(arr) {
  return arr[Math.floor(Math.random() * arr.length)];
}
现在更容易获得多个随机形状

var shape1 = sample(shapes);
var shape2 = sample(shapes);
但是,在上面的示例中,shape1和shape2可能是相同的结果。想想掷骰子吧:两种结果都可能是4

也许这不是我们想要的。也许我们想要的更像是一个数字彩票,其中数字1-50存在于一个桶中,一旦一个数字从桶中移除,它就不可能再次被拾取

要做到这一点,我们必须稍微调整一下函数

function sample(arr, count) {
  // default sample size of 1
  if (!count) count = 1;

  // create a copy of the original array
  var copy = arr.slice(0);

  // init samples with empty array
  var samples = [];

  // use a loop to sample n-items
  for (var i=0; i<count; i++) {
    // get the index of a random item
    var idx  = Math.floor(Math.random() * copy.length);

    // remove it from the copy of our array,
    // and add it to the samples result
    samples = samples.concat(copy.splice(idx, 1));
  }

  // return the samples
  return samples;
}
不过,我们可以对该代码进行有用的调整。考虑以下事项:

要从一副牌中随机抽取两张扑克牌,我们可以用两种方法

从已分类/未分类牌组的2个随机位置选择2张牌 洗牌,从上面拿两张牌 到目前为止,我们编写的示例函数使用的方法更像1。但是,如果我们使用像2这样的方法编写它,我们可以免费获得一个数组洗牌函数!酷

让我们看看会是什么样子

function random(x) {
  return Math.floor(Math.random() * x);
}

function shuffle(arr) {
  var copy = arr.slice(0);
  return arr.reduce(function(shuffled, elem) {
    var idx = random(copy.length);
    return shuffled.concat(copy.splice(idx, 1));
  }, []);
}

function sample(arr, n) {
  return shuffle(arr).slice(0, n || 1);
}
现在,我们已经达到了最初的目标,并获得了额外的可重用函数random和shuffle,这是将示例作为高阶过程编写的副作用。^

更新问题似乎与下划线.js有关,而不是一般的数组,所以这里有一个小的更新-

您似乎正确地使用了调用,但在指定采样值>1时,还需要将结果用作数组:

var shapes = ['circle','star','square','hexagon','triangle','polygon'];
var range = _.sample(shapes, 2);  // => ["square", "triangle"]; (example result)
因此,不需要使用Math.random来选择任何项目,因为现在的项目是随机选择的。只需迭代数组,使用每个索引的实际值

范例


Underjorejs_u2;.samplearr,n返回随机样本。你怎么知道这不是随机的?我觉得这个问题应该加下划线…@Sonia告诉我们是否有任何答案解决了问题。如果没有,请提供更多详细信息。谢谢
var shapes = ['circle','star','square','hexagon','triangle','polygon'];
var range = _.sample(shapes, 2);  // => ["square", "triangle"]; (example result)
... other code ...

for(var i = 0, shape; shape = range[i]; i++) {  // will loop through the range array

    switch(shape) {                             // current item
        case "square":                          // (not 0 etc. as in the original code)
            ... code for square here...
            break;

        case "triangle":
            ... code for triangle here...
            break;

        ... etc. ...
    }
}