Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Random 从Go切片中拾取一个随机值_Random_Go_Slice - Fatal编程技术网

Random 从Go切片中拾取一个随机值

Random 从Go切片中拾取一个随机值,random,go,slice,Random,Go,Slice,情况: 我有一个值切片,需要从中随机选择一个值。然后我想用一个固定的字符串连接它。这是我目前的代码: func main() { //create the reasons slice and append reasons to it reasons := make([]string, 0) reasons = append(reasons, "Locked out", "Pipes broke", "Food poisoning", "Not feeling we

情况:

我有一个值切片,需要从中随机选择一个值。然后我想用一个固定的字符串连接它。这是我目前的代码:

func main() {
//create the reasons slice and append reasons to it
reasons := make([]string, 0)
reasons = append(reasons,
    "Locked out",
    "Pipes broke",
    "Food poisoning",
    "Not feeling well")

message := fmt.Sprint("Gonna work from home...", pick a random reason )
}
问题:


是否有一个内置函数,可以通过执行“选择随机原因”部分来帮助我?

只需选择一个随机整数mod slice length:

rand.Seed(time.Now().Unix())
reasons := []string{
    "Locked out",
    "Pipes broke",
    "Food poisoning",
    "Not feeling well",
}
n := rand.Int() % len(reasons)
fmt.Print("Gonna work from home...", reasons[n])
操场:。(注意关于
rand.Seed
的推荐)

使用
rand
包中的函数选择随机索引

import (
  "math/rand"
  "time"
)

// ...

rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
message := fmt.Sprint("Gonna work from home...", reasons[rand.Intn(len(reasons))])
另一种解决方案是使用
Rand
对象

s := rand.NewSource(time.Now().Unix())
r := rand.New(s) // initialize local pseudorandom generator 
r.Intn(len(reasons))

由于谷歌在Golang random string generation的最热门搜索结果中仍然显示了这一点,所以我想与大家分享我一直在做的工作

以下是我正在使用的解决方案:

package main

import (
  "fmt"
  "strings"
  "time"
)

var (
  opts  = strings.Split("option1,option2,option3", ",")
  start = time.Now()
)

func main() {
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
}

func getRandomOpt() string {
  len := len(opts)
  n := uint32(0)
  if len > 0 {
    n = getRandomUint32() % uint32(len)
  }
  return opts[n]
}

func getRandomUint32() uint32 {
  x := time.Now().UnixNano()
  return uint32((x >> 32) ^ x)
}
结果:

option2 665ns
option1 41.406µs
option1 44.817µs
option3 47.329µs
option1 49.725µs
option3 52µs
option2 54.393µs
option2 56.798µs
option1 59.098µs
option3 11.865µs
option2 48.415µs
option3 52.809µs
option1 55.536µs
option3 58.191µs
option3 60.793µs
option1 63.391µs
option2 65.982µs
option2 68.601µs
就源代码而言,我从fastrand复制了getRandomUint32():

以及上述提出的解决方案。性能并没有什么不同,但我想分享结果

package main

import (
  "fmt"
  "math/rand"
  "strings"
  "time"
)

var (
  opts  = strings.Split("option1,option2,option3", ",")
  start = time.Now()
)

func main() {
  rand.Seed(start.Unix())
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
  fmt.Println(getRandomOpt(), time.Since(start))
}

func getRandomOpt() string {
  return opts[rand.Intn(len(opts))]
}
结果:

option2 665ns
option1 41.406µs
option1 44.817µs
option3 47.329µs
option1 49.725µs
option3 52µs
option2 54.393µs
option2 56.798µs
option1 59.098µs
option3 11.865µs
option2 48.415µs
option3 52.809µs
option1 55.536µs
option3 58.191µs
option3 60.793µs
option1 63.391µs
option2 65.982µs
option2 68.601µs

这些结果只在本地运行了几次,我得到了似乎是中间结果的结果。当然,在迭代和其他方面还有更多的工作要做,但我只想分享我所做的。

相关的,你可能会发现这个问题和答案很有用:它将你的问题作为一个子任务,也就是从一段符文或字符中随机选择字母。它可能会给你一些关于围棋“随机”世界的好提示,背景中发生了什么以及不同解决方案的效率。你的问题的答案在math/rand软件包文档中:-检查第一个代码示例;)小贴士:把你的代码记录下来,兰德。种子。。。也需要。这不如
rand.Intn(len(reasons))
。仅使用一个
%
对[0,len(原因)]的每个值的概率都不相等。真的!模块偏差!“[调用
种子
]需要…自行执行”。您只为随机数生成器设定一次种子。作为一次性启动调用,它所花费的时间应该是无关的。您的意思是什么?如果您没有在每个randomOpt()调用上设定随机数,您将从randomOpt()获得相同的结果。每次调用都需要随机生成该数字。我的解决方案的结果:
{option3}7.7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 1}98.493µs{option1}110.601µs{option1}122.225µs{option1}133.76µs{option1}145.352µs
这是
func randomOpt()字符串{rand.Seed(start.Unix())返回opts[rand.Intn(len(opts))]}
“如果没有在每个randomOpt()调用上植入随机数”…这不是种子设定的工作方式。你给一个随机数生成器种子设定一次,然后重复调用同一个生成器,得到不同的值。啊,明白了。将种子放入主()中如果我把每一个请求都不调用seed称为缓慢的原因,那是不正确的。修复seed,这个解决方案仍然比我提供的慢:
{option2}10.01µs{option1}68.63µs{option3}72.933µs{option3}75.834µs{option1}97.274µs{option3}100.264µs{option2}102.983{option3}105.97}s}108.537µs