How to properly seed random number generator

前端 未结 9 1205
陌清茗
陌清茗 2020-11-28 20:25

I am trying to generate a random string in Go and here is the code I have written so far:

package main

import (
    \"bytes\"
    \"fmt\"
    \"math/rand\"
         


        
9条回答
  •  离开以前
    2020-11-28 20:59

    Small update due to golang api change, please omit .UTC() :

    time.Now().UTC().UnixNano() -> time.Now().UnixNano()

    import (
        "fmt"
        "math/rand"
        "time"
    )
    
    func main() {
        rand.Seed(time.Now().UnixNano())
        fmt.Println(randomInt(100, 1000))
    }
    
    func randInt(min int, max int) int {
        return min + rand.Intn(max-min)
    }
    

提交回复
热议问题