英文:
Want to choose a random number and not have it picked again until all numbers have gone through
问题
我想从1到6中随机选择一个数字,但是如果程序选择了1,我希望确保在选择2-6之前不再使用1。我想这样做是为了有机会遍历所有选项,而不是连续几次选择相同的选项,因为只有6个选项。有什么建议吗?
// 为食谱选择随机数
rand.Seed(time.Now().UTC().UnixNano())
myrand := random(1, 6)
fmt.Println(myrand)
...
// 处理它的函数
// 随机数函数
func random(min, max int) int {
return rand.Intn(max-min) + min
}
每次运行时,我的程序使用myrand的部分与if语句相关联,并与当天将被选择的食谱相关联。
// 为食谱选择随机数
rand.Seed(time.Now().UTC().UnixNano())
myrand := random(1, 6)
fmt.Println(myrand)
// 以下是随机替换的测试
list := rand.Perm(6)
for i, _ := range list {
fmt.Printf("%d\n", list[i]+1)
}
// 食谱选择逻辑
if myrand == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe1)
} else if myrand == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe2)
} else if myrand == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe3)
} else if myrand == 4 {
fmt.Println(4)
}
}
我想知道我是否做错了什么?我不确定如何在每次运行时使用列表来生成一个新的数字(myrand),并且程序将能够记住上一次运行时的上一个数字(myrand)是什么?
<details>
<summary>英文:</summary>
I'm looking to pick a random number from 1-6 but if for example 1 is picked by the program I want to make sure it is not again used until 2-6 has been picked. I want to do this in order to have a chance to go through all options instead of having the same option 2 or maybe 3 times in a row since there are only 6 options. Any ideas please?
//choose random number for recipe
rand.Seed(time.Now().UTC().UnixNano())
myrand := random(1, 6)
fmt.Println(myrand)
...
function that processes it
//random number function
func random(min, max int) int {
return rand.Intn(max-min) + min
}
The part of my program that uses myrand from each run is used in a if statement and is tied to the recipe that will be picked for the day
//choose random number for recipe
rand.Seed(time.Now().UTC().UnixNano())
myrand := random(1, 6)
fmt.Println(myrand)
//test below of random replacement
list := rand.Perm(6)
for i, _ := range list {
fmt.Printf("%d\n", list[i]+1)
}
//logic for recipe to choose
if myrand == 1 {
fmt.Println(1)
printRecipeOfTheDay(recipe1)
} else if myrand == 2 {
fmt.Println(2)
printRecipeOfTheDay(recipe2)
} else if myrand == 3 {
fmt.Println(3)
printRecipeOfTheDay(recipe3)
} else if myrand == 4 {
fmt.Println(4)
}
}
I'm wondering if I'm doing something incorrectly ? I'm not sure how I can use the list on every run to generate a new number (myrand) and the program will be able to remember what the last number (myrand) was when it ran the previous time?
</details>
# 答案1
**得分**: 3
你可以使用`rand.Perm(6)`,然后循环遍历结果。
例如,以下是一个打印1-6随机顺序且不重复的代码示例:
```go
package main
import "fmt"
import "math/rand"
func main(){
list := rand.Perm(6)
for i, _ := range list {
fmt.Printf("%d\n", list[i]+1)
}
}
请注意,我们在每个数字上都加了1,因为Perm(6)
返回的是0-5,而你需要的是1-6。在使用完所有六个数字之后,再次调用rand.Perm(6)
以获取六个新的数字。
英文:
You can use rand.Perm(6)
and then loop through the result.
For example, here is code to print the numbers 1-6 in a random order, with no repeats:
package main
import "fmt"
import "math/rand"
func main(){
list := rand.Perm(6);
for i, _ := range list {
fmt.Printf("%d\n", list[i]+1);
}
}
Note that we've added 1 to everything, since Perm(6)
returns 0-5 and you want 1-6. After you've used all six, call rand.Perm(6)
again for six new numbers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论