Go 为什么不管种子值如何,我总是得到随机结果?

Go 为什么不管种子值如何,我总是得到随机结果?,go,Go,我有一个相当复杂的围棋应用程序。它在长链中生成许多随机结果。当HTTP请求传入时,它只被播种一次 无论种子是什么-无论是unix时间,还是我自己的字母数字种子函数-它总是生成完全随机的结果 我尝试删除我的字母数字种子函数,但这并没有改变行为。我还尝试将种子设置为1111。这没有效果 下面是一个示例(详细且直接从源代码中提取,因为这是请求的): func main(){ sentryDSN:=os.Getenv(“SENTRY\u DSN”) sentry.Init(sentry.ClientOp

我有一个相当复杂的围棋应用程序。它在长链中生成许多随机结果。当HTTP请求传入时,它只被播种一次

无论种子是什么-无论是unix时间,还是我自己的字母数字种子函数-它总是生成完全随机的结果

我尝试删除我的字母数字种子函数,但这并没有改变行为。我还尝试将种子设置为1111。这没有效果

下面是一个示例(详细且直接从源代码中提取,因为这是请求的):

func main(){
sentryDSN:=os.Getenv(“SENTRY\u DSN”)
sentry.Init(sentry.ClientOptions{
Dsn:sentryDSN,
})
sentryHandler:=sentryhttp.New(sentryhttp.Options{
雷帕尼克:是的,
})
r:=chi.NewRouter()
r、 使用(中间件.RequestID)
r、 使用(middleware.RealIP)
r、 使用(middleware.Logger)
r、 使用(中间件.恢复程序)
r、 使用(middleware.URLFormat)
r、 使用(middleware.SetHeader(“内容类型”、“应用程序/json”))
r、 使用(中间件超时(60*time.s))
r、 Get(“/buildingstyle”,sentryHandler.HandleFunc(getBuildingStyleRandom))
r、 Get(“/buildingstyle/{id}”,sentryHandler.HandleFunc(getBuildingStyle))
r、 Get(“/character”,sentryHandler.HandleFunc(getCharacterRandom))
r、 Get(“/character/{id}”,sentryHandler.HandleFunc(getCharacter))
r、 Get(“/climate”,sentryHandler.HandleFunc(getClimateRandom))
r、 Get(“/climate/{id}”,sentryHandler.HandleFunc(getClimate))
端口:=7531
Printf(“World Generator API正在运行http://localhost:%d.\n“,端口)
log.Fatal(http.ListenAndServe(fmt.Sprintf(“:%d”,端口),r))
}
func SeedFromString(源字符串)错误{
h:=md5.New()
_,err:=io.WriteString(h,源)
如果错误!=零{
err=fmt.Errorf(“未能为随机数生成器设定种子:%w”,err)
返回错误
}
种子:=binary.BigEndian.Uint64(h.Sum(nil))
兰德种子(int64(种子))
归零
}
func getClimate(w http.ResponseWriter,r*http.Request){
id:=chi.URLParam(r,“id”)
气候变量
错误:=random.SeedFromString(id)
如果错误!=零{
手柄错误(w、r、err)
回来
}
randomClimate,err:=climate.Random()
如果错误!=零{
手柄错误(w、r、err)
回来
}
o=随机气候。简化()
json.NewEncoder(w).Encode(o)
}
//生成使用给定名称生成气候
func生成(名称字符串)(气候,错误){
rawClimate,err:=ByName(名称)
如果错误!=零{
err=fmt.Errorf(“无法按名称生成气候:%w”,err)
返回气候{},错误
}
气候,错误:=rawClimate.populate()
如果错误!=零{
err=fmt.Errorf(“无法按名称生成气候:%w”,err)
返回气候{},错误
}
返回气候,零
}
func(气候)填充()(气候,错误){
gems:=mineral.gems()
昆虫:=气候。getFilteredInsects()
金属:=矿物。金属()
石头:=矿物。石头()
树:=climate.getFilteredTrees()
climate.Seasons=climate.getSeasons()
lakeChance:=兰特国际(100)
riverChance:=兰特国际(100)
oceanChance:=兰特整数(100)
wetlandsChance:=兰特国际(100)
如果lakeChance>30{
climate.HasLakes=true
}
如果riverChance>20{
climate.HasRivers=true
}
如果机会>80{
climate.HasOcean=true
}
如果wetlandsChance>80{
climate.hasweets=true
}
土壤:=气候。getFilteredSoils()
如果climate.HasLakes | | | climate.HasRivers | | climate.HasOcean{
climate.Fish=climate.getFish()
}否则{
climate.Fish=[]Fish.Fish{}
}
气候。昆虫=昆虫。随机子集(7,昆虫)
filteredMetals,err:=mineral.RandomWeightedSet(climate.MaxMetals,metals)
如果错误!=零{
err=fmt.Errorf(“无法填充气候:%w”,err)
返回气候{},错误
}
气候.金属=过滤金属
climate.Gems=mineral.Random(climate.MaxGems,Gems)
climate.OtherMinerals=mineral.OtherMinerals()
climate.Animals,err=climate.getAnimals()
如果错误!=零{
err=fmt.Errorf(“无法填充气候:%w”,err)
返回气候{},错误
}
climate.Plants,err=climate.getPlants()
如果错误!=零{
err=fmt.Errorf(“无法填充气候:%w”,err)
返回气候{},错误
}
climate.Soils=soil.Random(climate.MaxSoils,Soils)
climate.Stones=mineral.Random(climate.MaxStones,Stones)
climate.Trees=tree.RandomSubset(climate.MaxTrees,Trees)
资源:=climate.getResources()
气候资源=资源
description,err:=climate.getDescription()
如果错误!=零{
err=fmt.Errorf(“无法填充气候:%w”,err)
返回气候{},错误
}
climate.Description=说明
climate.Habitability=climate.calculateHabitability()
返回气候,零
}
许多函数,如
doStuff()
从应用了一些过滤的切片返回n个随机元素

当同一种子用于多次运行时,我希望所有这些都是一致的和相同的。然而,情况并非如此。相反,无论种子值如何,每次结果都是随机的


是否有一些我不知道的
rand.Intn()
rand.Seed()
操作的基本部分?

您正在为系统的许多部分使用的默认源进行种子设定。在一个大型、复杂的项目中,很可能有其他部分正在消耗不确定数量的随机值,这取决于环境。甚至有可能在代码的其他地方调用了
rand.Seed()

如果希望随机值独立,请创建自己的
r