Home > Article > Backend Development > rand.Seed(SEED) is deprecated, how to use NewRand(NewSeed( ) )?
php editor Xigua is here to share a solution with you. When we use rand.Seed(SEED) is deprecated, we can use NewRand(NewSeed()) instead . The NewSeed() function can generate a new seed, and the NewRand() function can use this new seed to generate random numbers. This approach helps us continue to use random number generation functionality without the limitations of rand.Seed(SEED) being deprecated. This way we can easily solve this problem and continue using the random number generation functionality.
I am currently learning go.
I am an example, I have this line
rand.seed(seed)
But the vscode extension about go told me
rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access. (SA1019)
I can't understand how to use newrand(newsource(seed))
as suggested.
I found the documentation about newsource https://pkg.go.dev/math/rand#newsource
But there is no documentation about the newrand
function
What is the new recommended rand.seed(seed)
equivalent?
go 1.20 seed documentation has a spelling error. Use rand.new(rand.newsource(seed))中的说明使用 a> and
go 1.20 Release Notes as described in the Latest Documentation
Create a random source and use methods on the source instead of calling package functions:
r := rand.New(rand.NewSource(seed)) fmt.Println(r.Uint64()) fmt.Println(r.Uint64())
The above is the detailed content of rand.Seed(SEED) is deprecated, how to use NewRand(NewSeed( ) )?. For more information, please follow other related articles on the PHP Chinese website!