How to Generate a Random Number Between 0 and 1 in C++

  1. Use C++11 <random> Library to Generate a Random Number Between 0 and 1
  2. Use std::rand Function to Generate a Random Float Between 0 and 1 in C++

This article explains several C++ methods on how you can generate a random float in 0 to 1 interval.

Use C++11 <random> Library to Generate a Random Number Between 0 and 1

The C++11 version provides classes/methods for random and pseudorandom number generation. These standard library facilities are recommended for handling random number generation in the modern C++ codebase.

std::random_device object is initialized first; it’s a non-deterministic uniform random bit generator used to seed the random number engine std::default_random_engine, which is initialized on the next line. This way it’s ensured that the engine won’t produce the same number sequences.

There are several random number engines that are implemented in the C++ <random> library, and they differ in a few factors like time/space requirements (see the full list here). In the following example below, we demonstrate random float generation using std::default_random_engine. We can specify a certain algorithm engine as required by the application constraints. The next step is we initialize a uniform distribution and pass min/max values as arguments. Finally, we print 5 random float values in [0-1] interval to the console.

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

using std::cout;
using std::endl;
using std::setprecision;

constexpr int FLOAT_MIN = 0;
constexpr int FLOAT_MAX = 1;

int main()
{
    std::random_device rd;
    std::default_random_engine eng(rd());
    std::uniform_real_distribution<float> distr(FLOAT_MIN, FLOAT_MAX);

    for (int n = 0; n < 5; ++n) {
        cout << setprecision(6)
             << distr(eng) << "\n";
    }

    return EXIT_SUCCESS;
}

Output:

0.22808
0.343274
0.408204
0.687902
0.572947

Note that std::uniform_real_distribution< T > yields undefined behavior if one of the following types is not passed as a template parameter: float, double, or long double to.

Use std::rand Function to Generate a Random Float Between 0 and 1 in C++

rand function is a part of the C standard library random number generation facilities. It’s not recommended for applications requiring high-quality randomness. Still, it can be utilized for testing purposes or cryptographically non-sensitive number generation.

rand function generates a pseudo-random integer between ​0​ and RAND_MAX (both included). Since the RAND_MAX value is implementation-dependent, and the guaranteed minimum value is only 32767, generated numbers have constrained randomness. Keep in mind that this function must be seeded with std::srand (preferably passing the current time using std::time). Finally, we can generate floating-point values in the interval of [0-1].

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

using std::cout;
using std::endl;
using std::setprecision;

constexpr int FLOAT_MIN = 0;
constexpr int FLOAT_MAX = 1;

int main()
{
    std::srand(std::time(nullptr));
    for (int i = 0; i < 5; i++)
        cout << setprecision(6)
             << FLOAT_MIN + (float)(rand()) / ((float)(RAND_MAX/(FLOAT_MAX - FLOAT_MIN)))
             << endl;

    return EXIT_SUCCESS;
}

Output:

0.483029
0.252537
0.264023
0.90233
0.573071

Related Article - C++ Float

  • How to Convert Float to String in C++