Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Created July 24, 2023 13:16
Show Gist options
  • Save ayoubzulfiqar/325a9b72b61a1784452206f49eb70e4a to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/325a9b72b61a1784452206f49eb70e4a to your computer and use it in GitHub Desktop.
The Pseudorandom Number Generators in Go

The Pseudorandom Number Generators in Go

Normal Algorithm

Generating pseudorandom numbers typically involves using algorithms or processes that produce sequences of numbers that appear to be random but are actually determined by a specific formula or seed value. There are various methods for generating pseudorandom numbers, and I'll provide a comprehensive list of some common techniques:

  1. Linear Congruential Generator (LCG): One of the oldest and simplest methods, it generates numbers using a linear congruential equation. The sequence depends on three parameters: modulus, multiplier, and increment.

  2. Mersenne Twister: A widely used pseudorandom number generator that has a very long period and good statistical properties. It uses a large state space and produces high-quality random numbers.

  3. XORshift: A family of pseudorandom number generators that use bitwise XOR and bit shifts to produce random-like numbers. They are known for their simplicity and speed.

  4. WELL: The Well Equidistributed Long-period Linear (WELL) generator is an improvement over LCG and offers good statistical properties and long periods.

  5. PCG (Permuted Congruential Generator): An algorithm that combines the speed of LCG with the high-quality output of other generators, achieved by using permutation operations.

  6. Middle Square Method: A simple method where the seed is squared, and the middle digits are taken as the next random number. It has limitations and is not widely used due to poor statistical properties.

  7. Von Neumann Extractor: A method for extracting random bits from a sequence of numbers, often used in combination with other generators to improve their randomness.

  8. Lagged Fibonacci Generator: This method combines the Fibonacci sequence with a lagged generator to produce pseudorandom numbers.

  9. Additive Lagged Fibonacci Generator: An extension of the Lagged Fibonacci Generator that adds previous values from the sequence instead of taking their modulo.

  10. Permuted Congruential Generator (PCG): A family of pseudorandom number generators known for their high-quality output and ease of use.

  11. Multiply-With-Carry (MWC): An algorithm that involves multiplying previous numbers with a constant and carrying over some bits to the next iteration.

  12. WELLRNG512a: An improved version of the WELL generator with a larger state and better equidistribution properties.

  13. Xorshift128+: A member of the XORshift family with a state size of 128 bits and good statistical properties.

  14. Permuted Linear Congruential Generator (PLCG): An LCG variant that applies permutations to the state and output to improve randomness.

  15. LFSR (Linear Feedback Shift Register): A shift register with feedback that produces a pseudorandom sequence of bits.

  16. Tausworthe Generator: A method based on bitwise operations and exclusive OR that generates pseudorandom numbers.

  17. Park-Miller RNG: An LCG variant with parameters carefully chosen to achieve a long period.

  18. Combined Multiple Recursive Generator (CMRG): Combines multiple recursive generators to improve the quality of pseudorandom numbers.

  19. WELL512a: Another variant of the WELL generator with a smaller state size.

  20. Cryptographically Secure Pseudorandom Number Generators (CSPRNGs): Algorithms designed to pass rigorous cryptographic tests and be secure for cryptographic applications.

Hardware Random Number Generators

Hardware random number generators (HRNGs) are devices that use physical processes to generate random numbers. Unlike pseudorandom number generators, which are algorithmic and deterministic, HRNGs rely on unpredictable physical phenomena to produce true randomness. Here are some common methods used in hardware random number generators:

  1. Thermal Noise: HRNGs can exploit the random thermal noise generated by electronic components like resistors or diodes. The fluctuations in voltage caused by thermal effects create a source of randomness.

  2. Shot Noise: Shot noise occurs in electronic components due to the discrete nature of electron flow. HRNGs can leverage this phenomenon to generate random numbers.

  3. Radioactive Decay: HRNGs can use the random decay of radioactive isotopes to create random events, such as the detection of radiation particles.

  4. Atmospheric Noise: Some HRNGs use atmospheric noise, such as radio noise from lightning or cosmic rays, as a source of randomness.

  5. Quantum Processes: Quantum-based HRNGs use the inherent randomness in quantum mechanics to generate random numbers. For example, the unpredictable outcome of quantum measurements can be utilized.

  6. Avalanche Noise: In semiconductor devices like diodes, avalanche noise occurs when a reverse-biased diode experiences a sudden current increase. This noise can be exploited for random number generation.

  7. Electronic Noise: Various electronic devices, such as transistors and amplifiers, exhibit intrinsic electronic noise that can be utilized for random number generation.

  8. Optical Noise: Optical HRNGs use unpredictable properties of light, such as photon arrival times or light intensity fluctuations, to generate random numbers.

  9. Chaos-based Systems: Some HRNGs exploit chaotic systems like electronic circuits with chaotic behavior to generate random sequences.

  10. Timing Jitter: HRNGs can utilize timing variations in electronic components, like clock skew or jitter in oscillators, as a source of randomness.

  11. Spintronics: HRNGs based on spintronics leverage the randomness associated with the spin of electrons.

  12. Resistor Networks: HRNGs can use resistor networks with non-linear behavior to generate random voltages.

  13. Electrochemical Noise: Certain chemical reactions or electrochemical processes exhibit random fluctuations that can be utilized for random number generation.

  14. Vibrational Noise: HRNGs can use vibrations or noise from mechanical systems as a source of randomness.

  15. Chaotic Optical Systems: HRNGs based on chaotic optical systems exploit the unpredictable behavior of light in complex optical setups.

Each of these hardware random number generation methods has its advantages and limitations. When designing secure systems that rely on random numbers, it's crucial to select an appropriate HRNG method and to perform thorough testing and validation to ensure the generated numbers are truly random and free from biases.

Cryptographically Secure Pseudorandom Number Generators

Cryptographically secure pseudorandom number generators (CSPRNGs) are essential for generating random numbers that meet rigorous cryptographic security requirements. They must be unpredictable and have high entropy, making them suitable for cryptographic applications. Here are some common methods used to generate cryptographically secure pseudorandom numbers:

  1. Fortuna: A robust cryptographic pseudorandom number generator designed for security-critical applications. It uses multiple entropy sources and a pool-based design.

  2. Yarrow: Another cryptographic pseudorandom number generator designed for security with a focus on performance. It uses entropy accumulation and a cryptographic function.

  3. CryptGenRandom: An API provided by Microsoft Windows for generating cryptographically secure random numbers.

  4. /dev/urandom: On Unix-like systems, this special file provides access to a CSPRNG. It gathers entropy from various sources and generates random data on demand.

  5. /dev/random: Similar to /dev/urandom, this file also provides access to a CSPRNG on Unix-like systems. It blocks when entropy is exhausted to ensure high security.

  6. ISAAC: A cryptographically secure pseudorandom number generator based on a hash function and designed to be fast and secure.

  7. CryptGenRandom API (Windows): A Windows API function for generating cryptographically secure random numbers.

  8. SecureRandom (Java): A class in Java's java.security package that provides a cryptographically strong random number generator.

  9. os.urandom (Python): In Python, the os module provides access to the operating system's random number generator, which is considered cryptographically secure.

  10. BCryptGenRandom (Windows): A Windows API function specifically for generating cryptographically secure random numbers for use in bcrypt password hashing.

  11. libsodium: A popular cryptographic library that includes a secure random number generation function.

  12. OpenSSL RAND_bytes: A function in the OpenSSL library that generates cryptographically secure random bytes.

  13. CSPRNG libraries: Various libraries and APIs specifically designed for cryptographic applications, such as the Random Number Generator Utility (RNGU) in Nettle library or libsodium's randombytes.

  14. Intel's RdRand Instruction: Some modern Intel CPUs include the RdRand instruction, which can be used to generate cryptographically secure random numbers.

  15. RDRAND CPU Instruction (Intel): Similar to RdRand, RDRAND is a CPU instruction that provides hardware-generated random numbers.

  16. Entropy Key: A specialized hardware device designed to provide a reliable source of entropy for cryptographic applications.

  17. Quantum Random Number Generators: Some quantum-based devices can generate true random numbers using quantum processes like photon detection or quantum tunneling.

It's crucial to choose a reliable and well-vetted method for generating cryptographically secure pseudorandom numbers. The method you choose may depend on the programming language, platform, and specific security requirements of your application. Always use a method that has been thoroughly reviewed and tested for cryptographic security.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment