C++中rand()无法生成大数问题求助
Hey there! Let's break down why your random number generator isn't spitting out numbers as large as you set your max value to be, and fix it up.
问题根源
The core issue here is the hard limitation of the old rand() function. On most systems, the maximum value rand() can return (defined as RAND_MAX) is 32767—that's 2¹⁵ - 1, around 30,000. No matter how big you set your max variable, rand() % max can never exceed RAND_MAX—that’s why even when you set max=10000000, your output numbers still top out at around 30k.
解决方案1:使用现代C++随机数库(推荐)
C++11 and later introduced a far more robust <random> library that avoids all the limitations of rand(). It supports way larger ranges, better random distribution, and more flexibility. Here's how to rewrite your function:
#include <iostream> #include <random> #include <chrono> void Random() { // Use current time as a seed to avoid repeating sequences on each run unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); std::mt19937 generator(seed); // Mersenne Twister generator: high performance, long cycle int amount; std::cout << "Choose the amount of the random values to create: "; std::cin >> amount; int max; std::cout << "Choose the max limit of the random numbers: "; std::cin >> max; // Define a distribution that generates integers from 0 to max-1 std::uniform_int_distribution<int> distribution(0, max - 1); int* numbers = new int[amount]; for (int i = 0; i < amount; ++i) { int randomNumber = distribution(generator); numbers[i] = randomNumber; std::cout << randomNumber << std::endl; } // Don't forget to free allocated memory to avoid leaks! delete[] numbers; }
std::mt19937has a maximum value of 2³²-1, which is way larger than the oldRAND_MAX, so it can handle your largemaxvalues easily.- The distribution ensures uniform randomness, which
rand() % maxcan fail to do for largemaxvalues.
解决方案2:扩展rand()的范围(不推荐,仅作过渡)
If you really need to stick with rand() for some reason, you can combine multiple rand() calls to create a larger number. Note that this is a workaround and not as reliable as the modern library:
#include <iostream> #include <cstdlib> #include <ctime> void Random() { srand(time(NULL)); int amount; std::cout << "Choose the amount of the random values to create: "; std::cin >> amount; int max; std::cout << "Choose the max limit of the random numbers: "; std::cin >> max; int* numbers = new int[amount]; for (int i = 0; i < amount; ++i) { // Combine two rand() results to extend the range (watch for overflow!) long long extended_rand = (static_cast<long long>(rand()) << 15) | rand(); int randomNumber = static_cast<int>(extended_rand % max); numbers[i] = randomNumber; std::cout << randomNumber << std::endl; } delete[] numbers; }
- This combines two 15-bit
rand()outputs into a 30-bit number, which can cover largermaxvalues (up to around 1 billion). - Be careful with integer overflow—using
long longprevents that during the combination step.
额外提醒
Don't forget to free the dynamically allocated numbers array with delete[] numbers;! Your original code doesn't do this, which will cause memory leaks over time.
内容的提问来源于stack exchange,提问作者Utkan

