Javascript 如何从随机数集中找到最小值?

Javascript 如何从随机数集中找到最小值?,javascript,Javascript,看 我理解为什么我总是得到1作为最小球数;我是否创建另一个包含6个随机数的变量/数组?如果是,如何创建?我不完全了解如何创建。首先,定义一个包含随机数的数组: var random_balls = []; 每次生成随机数时,将其添加到该数组中: // generate, the + is to convert a string like '12' to the number 12, otherwise // comparing them gives wrong results: '12' &l


我理解为什么我总是得到1作为最小球数;我是否创建另一个包含6个随机数的变量/数组?如果是,如何创建?我不完全了解如何创建。首先,定义一个包含随机数的数组:

var random_balls = [];
每次生成随机数时,将其添加到该数组中:

// generate, the + is to convert a string like '12' to the number 12, otherwise
// comparing them gives wrong results: '12' < '9' but 12 > 9
var random_ball = +balls[Math.floor(Math.random()*balls.length)];

// if indexOf returns -1, the ball is not present in the array
// (otherwise it returns the index at which the ball is)
while(random_balls.indexOf(random_ball) !== -1) {
    // generate again if you already took this ball
    random_ball = +balls[Math.floor(Math.random()*balls.length)];
}

// add to array
random_balls.push(random_ball);

// display
document.write(random_ball);

首先,定义一个包含随机数的数组:

var random_balls = [];
每次生成随机数时,将其添加到该数组中:

// generate, the + is to convert a string like '12' to the number 12, otherwise
// comparing them gives wrong results: '12' < '9' but 12 > 9
var random_ball = +balls[Math.floor(Math.random()*balls.length)];

// if indexOf returns -1, the ball is not present in the array
// (otherwise it returns the index at which the ball is)
while(random_balls.indexOf(random_ball) !== -1) {
    // generate again if you already took this ball
    random_ball = +balls[Math.floor(Math.random()*balls.length)];
}

// add to array
random_balls.push(random_ball);

// display
document.write(random_ball);

你从球数组中取最小值。你要做的就是从数组中打印出六个随机元素。您需要创建一个包含6个元素的新数组,将原始数组中的6个随机元素复制到该数组中,然后获得最小值。还请注意,您可以获得相同的元素两次。

您正在从balls数组中获取最小值。你要做的就是从数组中打印出六个随机元素。您需要创建一个包含6个元素的新数组,将原始数组中的6个随机元素复制到该数组中,然后获得最小值。还请注意,您可以获得相同的元素两次。

定义全局最小变量,并在选择“随机球”后更改它:

var min = Number.POSITIVE_INFINITY;

...

for(i=0; i<NUMBER_OF_BALLS; i++) {
    var ball = balls[Math.floor(Math.random()*balls.length)];

    min = Math.min(ball, min);

    document.write(ball);
    document.write("  ");
}
小提琴:


循环后,最小变量将保持最小值。

定义全局最小变量,并在选择随机球后更改它:

var min = Number.POSITIVE_INFINITY;

...

for(i=0; i<NUMBER_OF_BALLS; i++) {
    var ball = balls[Math.floor(Math.random()*balls.length)];

    min = Math.min(ball, min);

    document.write(ball);
    document.write("  ");
}
小提琴:


循环结束后,min变量将保存min number。

实际上,您需要将随机选择的值推送到一个数组中,并找到该数组的min WRT

  document.write("your balls are ");

  NUMBER_OF_BALLS = 6

  var i = 0;
  var balls = ['1','2','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','31','32','33','34','35','36','37','39','40'];

  var chosen = [];
  for(i=0; i<NUMBER_OF_BALLS; i++) {
      chosen.push(balls[Math.floor(Math.random()*balls.length)]);
      document.write(chosen[chosen.length-1]);
      document.write("  ");
  }

  document.write("<br>");
  document.write("<br>");

  min = Number.POSITIVE_INFINITY;
  for (i=0; i<chosen.length; i++) {   
    if (chosen[i] < min)
      min = chosen[i];
  }

  document.write("The lowest ball was: " + min);          
  document.write("<br>");

实际上,您需要将随机选择的值推送到一个数组中,并找到该数组的最小WRT

  document.write("your balls are ");

  NUMBER_OF_BALLS = 6

  var i = 0;
  var balls = ['1','2','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','31','32','33','34','35','36','37','39','40'];

  var chosen = [];
  for(i=0; i<NUMBER_OF_BALLS; i++) {
      chosen.push(balls[Math.floor(Math.random()*balls.length)]);
      document.write(chosen[chosen.length-1]);
      document.write("  ");
  }

  document.write("<br>");
  document.write("<br>");

  min = Number.POSITIVE_INFINITY;
  for (i=0; i<chosen.length; i++) {   
    if (chosen[i] < min)
      min = chosen[i];
  }

  document.write("The lowest ball was: " + min);          
  document.write("<br>");

我想换一种方式:

var arr = [], // acceptable values
    randomArray = []; // random array

// Create an array of numbers 0 to 45;
for(var i=1; i <=45 ; i++){
  arr.push(i);   
}

// From http://stackoverflow.com/q/962802#962890
function shuffle(array) {
    var tmp, current, top = array.length;

    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array;
}

// Randomly sort the array and take 6
randomArray = shuffle(arr).slice(0,6);

document.write("Random array: " + randomArray + "<br>");
document.write("Minimum value: " + Math.min.apply(Math, randomArray));
例如:

首先,我将从1-45生成一组数字,而不是您拥有的字符串数组。然后,我使用Fisher-Yates随机排列对其进行随机排序,查看到另一个问题的链接,为了得到6个随机数的子集,我在数组上做了一个切片


然后,通过Math.min.applyMath和randomArray可以找到最小值。我在这里选择另一种方法:

var arr = [], // acceptable values
    randomArray = []; // random array

// Create an array of numbers 0 to 45;
for(var i=1; i <=45 ; i++){
  arr.push(i);   
}

// From http://stackoverflow.com/q/962802#962890
function shuffle(array) {
    var tmp, current, top = array.length;

    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array;
}

// Randomly sort the array and take 6
randomArray = shuffle(arr).slice(0,6);

document.write("Random array: " + randomArray + "<br>");
document.write("Minimum value: " + Math.min.apply(Math, randomArray));
例如:

首先,我将从1-45生成一组数字,而不是您拥有的字符串数组。然后,我使用Fisher-Yates随机排列对其进行随机排序,查看到另一个问题的链接,为了得到6个随机数的子集,我在数组上做了一个切片


然后通过Math.min.applyMath,randomArray找到最小值

为什么1不是['1'、'2'、'4'、…、'40']可接受的最小值的可能重复项?尽管问题写得不是很清楚,但上面的评论者没有读过这个问题。问题不是找到最小值,而是生成一个随机数数组。如果你认为你的问题写得不清楚,也许你应该解决这个问题。你的随机集是否包含重复的数字,或者它们是否是唯一的?为什么1不是['1'、'2'、'4'、…、'40'可接受的最小值的可能重复?尽管问题写得不是很清楚,但上面的评论者没有读过。问题不是找到最小值,而是生成一个随机数数组。如果你认为你的问题写得不太清楚,也许你应该解决这个问题。你的随机集可以包含重复的数字,还是应该是唯一的?@DaveRandom:这似乎对我有用。你在用什么浏览器?IE8,看看我能用的代码;我不明白为什么没有,但它就是没有-给我这个:你的球是5 35 18 15 40 35最低的球是:15,应该是5…@DaveRandom:我错过了一些东西-比较字符串而不是数字。是的,我也刚刚记下了,需要一个parseInt-每个人似乎都错过了那个!如果你要修改它,我想添加一些东西来确保绘制的值不会重复是一个好主意,因为这看起来像是一个彩票类型的模拟,而且你不能两次得到同一个彩票球…@DaveRandom:这似乎对我有用。你在用什么浏览器?IE8,看看我能用的代码;我不明白为什么没有,但它就是没有-给我这个:你的球是5 35 18 15 40 35最低的球是:15,应该是5…@DaveRandom:我错过了一些东西-比较字符串而不是数字。是的,我也刚刚记下了,需要一个parseInt-每个人似乎都错过了那个!如果您正在修改它,我怀疑添加一些确保绘制的值不会重复的内容是一个好主意,因为这看起来像是一个彩票类型的模拟,并且您不可能两次获得相同的彩票球。。。