为什么不是唯一的数字仍然在出现?

huangapple go评论67阅读模式
英文:

Why not unique numbers are still coming?

问题

I need to generate the unique numbers, join it with the letter and push to array.

arr = []
for (var i = 0; i < 5; i++) {
  function generateRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
  }
  var randomNum = generateRandomNumber(1, 15);
  if (!arr.includes(randomNum)) {
    arr.push('B' + randomNum.toString())
  } else {
    if (arr.length < 5) {
      return generateRandomNumber(min, max)
    } else break
  }
}

I have provisioned uniqueness check, however the same values are still coming.

英文:

I need to generate the unique numbers, join it with the letter and push to array.

arr = []
for (var i = 0; i &lt; 5; i++) {
  function generateRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
  }
  var randomNum = generateRandomNumber(1, 15);
  if (!arr.includes(randomNum)) {
    arr.push(&#39;B&#39; + randomNum.toString())
  } else {
    if (arr.length &lt; 5) {
      return generateRandomNumber(min, max)
    } else break
  }
}

I have provisioned uniqueness check, however the same values are still coming.

答案1

得分: 1

我已经提供了唯一性检查,然而相同的值仍然出现。

唯一性检查是错误的。
数组仅包含以字母'B'开头的字符串(通过行arr.push('B' + randomNum.toString())添加),而唯一性检查搜索数字。

调用arr.includes(randomNum)总是返回false。每个生成的randomNum值都以'B'前缀添加到数组中,不管数组包含什么内容。


尝试一个更简单的方法:生成数字,如果它们还没有在列表中,则将它们添加到列表中,当列表足够大(五个项目)时停止。
然后遍历列表,并将'B'前缀添加到每个项目。

像这样:

function generateRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

// 生成1到15之间的5个唯一随机数
let arr = []
while (arr.length < 5) {
  let num = generateRandomNumber(1, 15);
  // 忽略已经生成的值
  if (!arr.includes(num)) {
    arr.push(num);
  }
}

// 为生成的数字添加'B'前缀
arr = arr.map((num) => `B${num}`);

console.log(arr);

希望这对你有帮助。

英文:

> I have provisioned uniqueness check, however the same values are still coming.

The uniqueness check is incorrect.<br>
The array contains only strings starting with letter &#39;B&#39; (added by the line arr.push(&#39;B&#39; + randomNum.toString())) while the uniqueness check searches for numbers.

The call arr.includes(randomNum) always returns false. Each and every generated value of randomNum is prefixed with &#39;B&#39; and pushed into the array, no matter what the array contains.


Try a simpler approach: generate numbers, add them to the list if they are not already there, stop when the list is large enough (five items).<br>
Then run through the list and add the &#39;B&#39; prefix to each item.

Like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function generateRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}


// Generate 5 unique random numbers between 1 and 15
let arr = []
while (arr.length &lt; 5) {
  let num = generateRandomNumber(1, 15);
  // Ignore a value that has already been generated
  if (!arr.includes(num)) {
    arr.push(num);
  }
}

// Add the &#39;B&#39; prefix to the generated numbers
arr = arr.map((num) =&gt; `B${num}`);

console.log(arr);

<!-- end snippet -->

答案2

得分: 1

条件只在循环中检查一次。
此外,如果条件位于else状态中,有可能也会出现与以前相同的数字。
您可以使用这种方法:

let arr = []
function generateRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

function add(){
  var randomNum = generateRandomNumber(1, 15);
    if(arr.length < 5){
        if (!arr.includes('B' + randomNum.toString())) {
            arr.push('B' + randomNum.toString())
        }
        add()
    }
  
}
add()
console.log(arr)
英文:

Condition only checked once in the loop.
Also if the condition is in the else state, there is a chance that there will also be same number as previous.
You can use this approach

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let arr = []
function generateRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

function add(){
  var randomNum = generateRandomNumber(1, 15);
    if(arr.length &lt; 5){
        if (!arr.includes(&#39;B&#39; + randomNum.toString())) {
            arr.push(&#39;B&#39; + randomNum.toString())
        }
        add()
    }
  
}
add()
console.log(arr)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 16:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定