英文:
How to calculate a random number in 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")
}
}
这是我目前的代码,但我一直收到"imported and not used "math/rand""的错误提示,还有一个未定义的Rand是什么意思?我该怎么办?
英文:
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")
}
}
This is what I have so far but I keep getting imported and not used "math/rand" and also a undefined Rand what does that mean? And what should I do?
答案1
得分: 2
请参考math/rand
包的文档以了解如何生成随机数。例如:
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.
}
英文:
See the math/rand
package documentation for how to generate random numbers. e.g.:
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.
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论