C/C++ Random number generation method
1. Use rand() and srand()
-
Library:
<>
or<cstdlib>
- Features: Pseudo-random number generator, simple and easy to use.
- Example:
#include <> #include <> int main() { srand(time(NULL)); // Initialize the random number generator int random_number = rand() % 100; // Generate random numbers between 0 and 99 printf("Random number: %d\n", random_number); return 0; }
2. Use the <random> library
-
Library:
<random>
- Features: Provides a variety of random number generators and distributions.
- Example:
#include <random> #include <iostream> int main() { std::random_device rd; // True random number generator std::mt19937 gen(rd()); // Mersenne Twister Engine std::uniform_int_distribution<> dis(0, 99); // Generate integers between 0 and 99 int random_number = dis(gen); std::cout << "Random number: " << random_number << std::endl; return 0; }
3. Use /dev/random and /dev/urandom
- Library: No special libraries are needed, they are read directly through file operations.
- Features: Provides true random numbers.
- Example:
#include <> #include <> int main() { FILE *file = fopen("/dev/urandom", "rb"); unsigned char random_byte; if (fread(&random_byte, 1, 1, file) != 1) { perror("fread"); exit(EXIT_FAILURE); } fclose(file); int random_number = random_byte % 100; printf("Random number: %d\n", random_number); return 0; }
4. Generate instructions using hardware random numbers
-
Library:
<>
- Features: Provides true random numbers at the hardware level.
- Example:
#include <> #include <> int main() { unsigned int random_number; _rdrand32_step(&random_number); // It may take multiple attempts to succeed printf("Random number: %u\n", random_number % 100); return 0; }
5. Use the OpenSSL library
-
Library:
<openssl/>
- Features: Provides high-quality random numbers for encryption.
- Example:
#include <openssl/> #include <> int main() { unsigned char random_bytes[4]; // Generate 4 byte random number if (!RAND_bytes(random_bytes, sizeof(random_bytes))) { printf("RAND_bytes failed\n"); return 1; } unsigned int random_number = *(unsigned int *)random_bytes; printf("Random number: %u\n", random_number % 100); return 0; }
Select the appropriate random number generation method
-
Pseudo-random number generator (
rand()
,<random>
): Suitable for occasions where there is little requirement for randomness, such as simple random events in game development. -
True random number generator (
/dev/urandom
, OpenSSL, Hardware Random Number Generator): Suitable for occasions with high security requirements, such as cryptography applications and encryption key generation.
Things to note
- use
/dev/random
When paying attention, if the entropy in the system entropy pool is not enough, read/dev/random
May be blocked until there is enough entropy generated. - For safety-sensitive applications, use should be avoided.
rand()
andsrand()
, because they generate pseudo-random numbers and are not suitable for encryption. - When selecting a random number generator, the balance between performance and security is taken into account.
These methods cover different scenarios from simple pseudo-random numbers to high-quality true random numbers generation.
This is the end of this article about five methods of random number generation of C/C++. For more related content on random number generation of C/C++, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!