LoginSignup
1
1

More than 5 years have passed since last update.

PHPエンジニアのためのGoLang便利Tips〜その2(mt_rand)

Last updated at Posted at 2014-11-26

phpの標準関数 mt_rand をGoで自前実装したらこんな感じ。


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

func Random(min int, max int) int {
    if min < 0 {
        min = 0
    }
    if max < 0 {
        max = 0
    }
    if min >= max {
        return max
    }
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(max - min) + min
}
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1