After a Windows update a lot of machines in the company fleet rebooted at roughly the same time. Regardless, you should NOT be reseeding your random number generator between iterations, it was never designed to be used that way.Įdit: I used to work in ITAM/SAM and the client we built (then) used a clock based seed. If you don't need that you can avoid it by creating your own Source and use that in a non-concurrent way. The rand package utility functions are convenient but they also use locks under the hood to prevent the source from being used concurrently. You can create your own rand.Source using this method to avoid the cost of having locks protecting the source. Math_rand.Seed(int64(64(b)))Īs a side note but in relation to your question. Panic("cannot seed math/rand package with cryptographically secure random number generator") It will give you that non-deterministic quality that you are probably looking for in your random numbers (even if the actual implementation itself is limited to a set of distinct and deterministic random sequences). The degree that this matters to you will vary but you can avoid pitfalls of clock based seed values by simply using the crypto/rand.Read as source for your seed. All the while the high bits are mostly constant!? Roughly ~24 bits of entropy over a day which is very brute forceable (which can create vulnerabilities). That's 20 bits of entropy that are not used. I see increments of about 1000000 ns, so 1 ms increments. This program should not be run on the Go playground but if you run it on your machine you get a rough estimate on what type of precision you can expect. For example, while the system clock is maybe represented in nanoseconds, the system's clock precision isn't nanoseconds. This has in my experience never been a good idea. I don't understand why people are seeding with a time value. Note also that I think you can simplify your string building: package main UnixNano returns t as a Unix time, the number of nanoseconds elapsed since JanuUTC. Move the rand.Seed(time.Now().UTC().UnixNano()) line from the randInt function to the start of the main and everything will be faster. In your case, as you're calling your randInt function until you have a different value, you're waiting for the time (as returned by Nano) to change.Īs for all pseudo-random libraries, you have to set the seed only once, for example when initializing your program unless you specifically need to reproduce a given sequence (which is usually only done for debugging and unit testing).Īfter that you simply call Intn to get the next random integer. So of course if you're setting the seed to the time in a fast loop, you'll probably call it with the same seed many times. Each time you set the same seed, you get the same sequence.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |