Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Rand ¶
Rand returns a new pseudo-random number generator, *rand.Rand, which is initialized with a unique seed value obtained from the Seed function. The Seed function uses a mix of various system and context-specific elements to generate a unique seed value. This guarantees that the sequence of pseudo-random numbers generated by the returned *rand.Rand object is distinct.
func RandS ¶
RandS is a singleton and thread-safe variant of the Rand function. It returns a globally unique instance of a *rand.Rand, a pseudo-random number generator. This generator is initialized with a unique seed value obtained from the Seed function, which utilizes various system and context-specific elements. The singleton nature of this function ensures that the same *rand.Rand instance is returned across multiple calls, across all threads. The thread-safety guarantees that the singleton instance is correctly initialized even in a multi-threaded context.
func RandSA ¶
RandSA initializes an array of *rand.Rand instances, if not already done, and returns a *rand.Rand chosen from the array using pseudo-random selection.
func Seed ¶
func Seed() int64
Seed is a function that generates a seed for pseudo-random number generation. It uses various unique and variable elements from the system as a basis for the seed, which is then used to generate unique random numbers across multiple calls.
The seed is initially set to the current Unix timestamp in nanoseconds. A new random source is then created based on this timestamp, which is used for further calculations.
The function gathers system-specific data, including the current hostname, process ID, user ID, group ID, working directory, and the number of logical CPUs on the system.
It also gathers some context-specific data by calling the runtime.Caller function. This returns information about the function that is invoking Seed, including its file name, line number, and a counter that represents the current instruction address.
The function then reads a random number from the crypto/rand package to further enhance the randomness of the seed.
After that, the function iterates through each environment variable, converting each one to an integer hash. The generated hash is used in a random mathematical operation (addition, subtraction, or multiplication) to modify the seed value.
Each piece of system-specific and context-specific data, as well as the randomly generated number from crypto/rand, is also used in the same way, to further modify the seed value.
The resulting seed is a complex, unique value that can be used to seed a pseudo-random number generator (such as the one in the math/rand package) to ensure that the generated sequence of numbers is as random and unique as possible across different contexts and systems.
Types ¶
This section is empty.