想要选择一个随机数,并且在所有数字都被选择之前不再选择它。

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

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&#39;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(&quot;%d\n&quot;, 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&#39;m wondering if I&#39;m doing something incorrectly ? I&#39;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 &quot;fmt&quot;
import &quot;math/rand&quot;
func main(){
list := rand.Perm(6);
for i, _ := range list {
fmt.Printf(&quot;%d\n&quot;, 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.

huangapple
  • 本文由 发表于 2016年2月8日 12:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/35262274.html
匿名

发表评论

匿名网友

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

确定