英文:
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 < 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.
答案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 'B' (added by the line arr.push('B' + 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 'B' 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 'B' 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 < 5) {
let num = generateRandomNumber(1, 15);
// Ignore a value that has already been generated
if (!arr.includes(num)) {
arr.push(num);
}
}
// Add the 'B' prefix to the generated numbers
arr = arr.map((num) => `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 < 5){
if (!arr.includes('B' + randomNum.toString())) {
arr.push('B' + randomNum.toString())
}
add()
}
}
add()
console.log(arr)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论