golang rand() 是否使用 libc rand()

我在 golang 和 C 中使用相同的种子,但得到不同的随机数我知道 php 使用 libc rand(),golang 怎么样?


//golang:

rand.Seed(12345); 

rand.Uint32();


//C:

srand(12345); 

rand();



一只斗牛犬
浏览 107回答 4
4回答

小怪兽爱吃肉

不,rand包根本不使用 C 标准库,您可以通过查看每个源文件来判断它不使用 CGO。exp.go :import (     "math"     )正常.goimport (         "math")随机数import "sync"rng.go没有进口zipf.go:import "math"

守着星空守着你

C 标准对以下行为施加了有限的要求rand()- 您通常得到的是具有合理周期性(与 相关RAND_MAX)的线性同余生成器,并srand允许您为该生成器播种。因此,即使使用相同的起始种子,它也没有理由返回与另一个线性同余生成器或其他方式相同的序列。如果你想让你的跨语言生成器匹配,那么你可能想推出自己的。

宝慕林4294392

我在 golang 和 C 中使用相同的种子,但得到不同的随机数我知道 php 使用 libc rand(),golang 怎么样?//golang:rand.Seed(12345);&nbsp;rand.Uint32();//C:srand(12345);&nbsp;rand();C去随机的我已经通过 golang 实现了函数 rand 和 srand 。与 PHP/C 相比,它可以获得相同的结果。package fcryptvar index intvar r []intfunc mysrand(seed int) {&nbsp; &nbsp; r = append(r, seed)&nbsp; &nbsp; for i := 1; i < 31; i++ {&nbsp; &nbsp; &nbsp; &nbsp; r = append(r, (16807*r[i-1])%2147483647)&nbsp; &nbsp; &nbsp; &nbsp; if r[i] < 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r[i] = r[i] + 2147483647&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for i := 31; i < 34; i++ {&nbsp; &nbsp; &nbsp; &nbsp; r = append(r, r[i-31])&nbsp; &nbsp; }&nbsp; &nbsp; for i := 34; i < 344; i++ {&nbsp; &nbsp; &nbsp; &nbsp; r = append(r, r[i-31]+r[i-3])&nbsp; &nbsp; }}func myrand() uint32 {&nbsp; &nbsp; i := 344 + index&nbsp; &nbsp; r = append(r, r[i-31]+r[i-3])&nbsp; &nbsp; index = index + 1&nbsp; &nbsp; return uint32(r[i]) >> 1}

繁星淼淼

golang rand() 是否使用 libc rand()不。
打开App,查看更多内容
随时随地看视频慕课网APP