How to Generate a Random Float Number in C++

  1. Use C++11 <random> Library to Generate a Random Float
  2. Use rand Function to Generate a Random Float

This article will explain several methods on how to generate a random float number in C++.

Use C++11 <random> Library to Generate a Random Float

This method is the recommended way of generating high-quality random numbers in contemporary C++. At first, the std::random_device object should be initialized. It produces non-deterministic random bits for random engine seeding, which is crucial to avoid producing the same number sequences. In this example we use std::default_random_engine to generate pseudo-random values, but you can declare specific algorithm engine (see the full list here). Next, we initialize a uniform distribution and pass min/max values as optional arguments.

As a result, we output 5 random floats to the console.

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

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

constexpr int FLOAT_MIN = 10;
constexpr int FLOAT_MAX = 100;

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

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

    return EXIT_SUCCESS;
}

Output:

19.54383877
92.41870106
92.42645927
93.53035308
39.09127952

The previous version actually generates double-precision floating point values (64 bits). You can customize float type by specifying float, double, or long double to std::uniform_real_distribution< T >. Note though that if the argument is not one of these it yields undefined behavior. The following example generates single-precision floating point numbers:

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

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

constexpr int FLOAT_MIN = 10;
constexpr int FLOAT_MAX = 100;

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(10)
             << distr(eng) << "\n";
    }

    return EXIT_SUCCESS;
}

Use rand Function to Generate a Random Float

rand function comes from the C library and is not recommended if the quality randomness is required. This 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, numbers generated by rand have constrained randomness. Note that this function should be seeded with std::srand (preferably passing the current time argument), and then we can generate random floating point values with some cumbersome arithmetic.

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

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

constexpr int FLOAT_MIN = 10;
constexpr int FLOAT_MAX = 100;

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(10)
             << distr(eng) << "\n";
    }
    cout << endl;

    std::srand(std::time(nullptr));
    for (int i = 0; i < 5; i++)
        cout << setprecision(10)
            << FLOAT_MIN + (float)(rand()) / ((float)(RAND_MAX/(FLOAT_MAX - FLOAT_MIN)))
            << endl;
    return EXIT_SUCCESS;
}

Related Article - C++ Float

  • How to Print Numbers With Specified Decimal Points in C++
  • comments powered by Disqus