go math/rand

go package math/rand 遇到的问题


在进入正文之前,请先看看以下将产生什么结果
    for i := 0; i < 5; i++ {
        fmt.Printf("current time.Now().Unix():%s , rand Value : ", "XXXXXXXXXX")
        rand.Seed(time.Now().Unix())
        fmt.Println(rand.Intn(100))
    }

1、大家经常使用math/rand来获取随机数,那我们一起跟着基础使用走一遍

1.1基础用法跟踪

# 首先给rand一个种子,
rand.Seed(time.Now().Unix())

# 然后将随机获取半闭半开0~10之间的数值
rand.Intn(10)

1.2 我们现来看看rand.Seed()

// 1.2.0 首先我们先看看主要的结构体、接口、常量

var globalRand = New(&lockedSource{src: NewSource(1).(Source64)})

type Rand struct {
    src Source
    s64 Source64
    readVal int64
    readPos int8
}

type rngSource struct {
    tap  int
    feed int
    vec  [rngLen]int64
}

type Source interface {
    Int63() int64
    Seed(seed int64)
}

type Source64 interface {
    Source
    Uint64() uint64
}

const (
    rngLen   = 607
    rngTap   = 273
    rngMax   = 1 << 63
    rngMask  = rngMax - 1
    int32max = (1 << 31) - 1
)


// 1.2.1 首先Seed 将会从一个全局变量来设置一个种子值,为了后续的随机算法
func Seed(seed int64) { globalRand.Seed(seed) }

// 1.2.2
var globalRand = New(&lockedSource{src: NewSource(1).(Source64)})

// 1.2.3 此时将生成一个Rand结构体的指针
func New(src Source) *Rand {
    s64, _ := src.(Source64)
    return &Rand{src: src, s64: s64}
}

// 1.2.4 这时候我们来看看创建的NewSource函数 将是生成了rngSource 结构体变量指针,我们可以看到1.2.2的步骤又将此变量转成了Source64接口类型。
func NewSource(seed int64) Source {
    var rng rngSource
    rng.Seed(seed)
    return &rng
}

// 1.2.5 最终将种子值 经过 相应算法【感兴趣可以自行研究】放入rng.vec 中,且 用的是常量rngLen : 607

func (rng *rngSource) Seed(seed int64) {
    rng.tap = 0
    rng.feed = rngLen - rngTap

    seed = seed % int32max
    if seed < 0 {
        seed += int32max
    }
    if seed == 0 {
        seed = 89482311
    }

    x := int32(seed)
    for i := -20; i < rngLen; i++ {
        x = seedrand(x)
        if i >= 0 {
            var u int64
            u = int64(x) << 40
            x = seedrand(x)
            u ^= int64(x) << 20
            x = seedrand(x)
            u ^= int64(x)
            u ^= rngCooked[i]
            rng.vec[i] = u
        }
    }
}

# 1.2.6 最终使用rand.Intn() 来获取随机数值,此时大家再来看看刚刚题的值

current:XXXXXXXXXX , rand Value : 28
current:XXXXXXXXXX , rand Value : 28
current:XXXXXXXXXX , rand Value : 28
current:XXXXXXXXXX , rand Value : 28
current:XXXXXXXXXX , rand Value : 28

这时候大家应该能知道由于每次rand.Seed()将每次生成一个新的Rand指针,由于种子值是相同的,因此每次都是一个新的Rand指针取rng.vec 中的相同下标中的值。

再此说明下只需将rand.Seed()的值,作为全局变量使用即可,内置的锁,在并发量很大的情况下,锁的影响其实并不大。

例如: etcd 服务发现得出服务列表数据,将可以通过全局变量 globalRand 来随机获取值,全局变量可防止请求在并发下击中某台服务器,将随机选取健康服务来继续。
2、 有时大家也会使用另外一种方式
var globalRand *rand.Rand
globalRand = rand.New( rand.NewSource(time.Now().Unix()) )
globalRand.Intn(10)

不过此 globalRand没有锁机制,在并发下偶尔会出现panic。要么自行实现锁机制,要么默认使用系统提供的globalRand创建方式。
author: clown
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容