使用javascript的乐透号码随机发生器

使用javascript的乐透号码随机发生器,javascript,printing,while-loop,Javascript,Printing,While Loop,我对javascript非常陌生。在我的课上,我们为一个随机数生成器写了一个代码,但我的代码不起作用。我想知道是否有人可以看看它,告诉我我做错了什么。我认为我的循环语法是错误的,但不能确定 函数lottoGen(){ var i=0;//增量的变量 var d=0;//减量的变量 var arr2=[0,0,0,0,0,0];//6个数组值。从0开始 arr2[5]=Math.random(1,26);//为数组中的位置5选择随机数 当(i在第一次迭代时,i为0,因此d也为0,因此该块: wh

我对javascript非常陌生。在我的课上,我们为一个随机数生成器写了一个代码,但我的代码不起作用。我想知道是否有人可以看看它,告诉我我做错了什么。我认为我的循环语法是错误的,但不能确定

函数lottoGen(){
var i=0;//增量的变量
var d=0;//减量的变量
var arr2=[0,0,0,0,0,0];//6个数组值。从0开始
arr2[5]=Math.random(1,26);//为数组中的位置5选择随机数

当(i在第一次迭代时,
i
为0,因此
d
也为0,因此该块:

while (d !== 0 && d <= 4) {
  d--;
  if (arr2[i] === arr2[d]) {
    i--;
  }
  i++;
 }
}

此外,
Math.random()
不接受任何参数并返回从0到1的数字,因此要获得某个范围内的整数,必须使用一个小实用程序:

 const random = (min, max) => min + Math.floor((max - min) * Math.random());

console.log(random(1, 69));

PS:老实说,你的代码实际上很难理解,而且注释也没有真正的帮助。与其描述代码,不如试着描述你在那里试图实现的目标:

 // Step through the array and fill it with random numbers
 while (i <= 4) { 
  arr2[i] = random(0, 69);
  d = i;
 // Check all positions to the left if the number is already taken
  while (d !== 0 && d <= 4) {
    d--;
    if (arr2[i] === arr2[d]) {
      // If thats the case, stay at this position and genrate a new number
      i--;
    }
  }
  i++;
}
//遍历数组并用随机数填充

虽然(i
Math.random
不接受任何参数,总是返回[0,1]中的一个数字[(或[0,1]在某些区域)感谢您的反馈。我将尝试一下。
 // Step through the array and fill it with random numbers
 while (i <= 4) { 
  arr2[i] = random(0, 69);
  d = i;
 // Check all positions to the left if the number is already taken
  while (d !== 0 && d <= 4) {
    d--;
    if (arr2[i] === arr2[d]) {
      // If thats the case, stay at this position and genrate a new number
      i--;
    }
  }
  i++;
}
 function lottoGen() {
   const result = [];

  for(let count = 0; count < 6; count++) {
    let rand;
    do {
      rand = random(0, 69);
    } while(result.includes(random))
    result.push(rand);
  }

  return result;
}