HOWTO · C++

How to Generate a Random Float Number in C++

This article demonstrates how to generate a random float number in C++.

On this page

Use C++11’s <random> library to generate a pseudo-random floating-point value in a chosen range. std::uniform_real_distribution accepts the lower and upper bounds, while a random engine supplies the sequence. Use float or double according to the precision your application needs; use rand only when maintaining legacy code.

Generate a random floating-point value with C++11

This complete program generates five values in the half-open interval [10, 100). The fixed seed makes the engine’s sequence repeatable with the same standard-library implementation, which is useful while debugging. The exact printed values can differ across implementations because the distribution algorithm is not specified to produce a portable sequence.

Run this example:

#include <iomanip>
#include <iostream>
#include <random>

int main() {
  std::mt19937 engine(2026);
  std::uniform_real_distribution<double> distribution(10.0, 100.0);

  for (int i = 0; i < 5; ++i) {
    std::cout << std::setprecision(10) << distribution(engine) << '\n';
  }
}
78.99481399
96.50696843
96.65823206
73.63312607
11.77415091

The distribution’s template argument selects the result type. It defaults to double; the standard-supported choices are float, double, and long double. Choose one explicitly so the type is clear at the call site; the example uses double, while std::uniform_real_distribution<float> requests float values. The bounds are real values, and the upper bound is excluded by the distribution. This half-open interval is useful for scaling values into ranges where an exact upper endpoint would overlap the next bucket. For example, a range from 0.0 to 1.0 can represent a normalized proportion, while a range from 10.0 to 100.0 can represent a measurement in chosen units.

Use float when its precision is sufficient and storage or bandwidth matters; choose double for the usual balance of precision and range. long double is available when its additional precision is useful, though the amount of extra precision depends on the implementation. Keep the bound values aligned with the distribution type where practical, especially when changing the template argument, so the intended range is easy to inspect. The distribution does not guarantee the same printed sequence across different standard-library implementations, even when the engine seed is fixed.

Keep the engine alive and reuse it when generating a sequence. Constructing a newly seeded engine for every value can restart or degrade the sequence. For repeatable results, seed with a fixed value as above. For a different sequence between runs, std::random_device can provide seed material when the implementation supports nondeterministic output; that behavior is not guaranteed on every system. Neither approach makes this general-purpose pseudo-random generator suitable for security-sensitive values. A seed initializes the engine once; it is not a request to generate a new independent engine for each draw. If an application needs reproducible results across runs, record the seed along with other inputs. If it needs security-sensitive tokens, use a cryptographic random source instead of this distribution and engine.

Use rand as a limited legacy alternative

The C library’s rand() returns a pseudo-random integer from 0 through the implementation-dependent RAND_MAX, inclusive. To map it to a floating-point range, divide by RAND_MAX using floating-point arithmetic before scaling. This example maps to [10, 100], including the upper endpoint when rand() returns RAND_MAX:

#include <cstdlib>
#include <iostream>

int main() {
  const double lower = 10.0;
  const double upper = 100.0;
  std::srand(2026);

  for (int i = 0; i < 5; ++i) {
    const double value = lower + (upper - lower) *
        (static_cast<double>(std::rand()) / RAND_MAX);
    std::cout << value << '\n';
  }
}
60.2772
88.4741
67.8989
11.5099
83.7273

rand() has limited state and statistical quality; a fixed seed makes this example repeatable for the same C library implementation, but does not make it unpredictable or secure. Prefer <random> for new C++ code because it separates the engine from the distribution and lets you choose the result type and range directly.