go - 如何在 Go 中计算随机数?

标签 go

<分区>

package main

import "fmt"

import "math/rand"

func main() {

    var milesdriven float64
    var enginerunningloud bool = true
    var changeoil bool = true

    if milesdriven >= 3000.0 || enginerunningloud == false {

        float64 := (milesdriven * Rand)
        changeoil = true
        fmt.Println("Change that oil duuuuuude")

    }

}

这是我到目前为止所拥有的,但我一直在导入而不是使用“math/rand”以及未定义的 Rand 这是什么意思?我该怎么办?

最佳答案

参见 math/rand package documentation了解如何生成随机数。例如:

package main

import "math/rand"
import "time"

func main() {
  seed := time.Now().UnixNano()
  random := rand.New(rand.NewSource(seed))
  println(random.Float64()) // => +8.277637e-001
  println(random.Int())     // => 7112143871403206581
  // etc.
}

关于go - 如何在 Go 中计算随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710685/

相关文章:

go - golang request.ParseForm() 是否与 "application/json"Content-Type 一起工作

go - oauth2 问题,创建时无法获取 access_token

dictionary - 分配给 nil 映射中的条目

go - 为什么 App Engine Flexible Enviroment 不允许 WebSockets 和 HTTP/2 流量?

concurrency - 为什么我的并发函数在 Go 中过早退出?

firefox - Golang https证书错误: remote error: tls: unknown certificate authority

github - Go,如何从 github 导入包并在不使用 go get 的情况下构建

戈朗 : odd big Int behavior

printing - 使用 bytes.replace 时有没有办法使用通配符?

go - golang中net/http连接源码疑惑