C++

How to Generate Random Floats in C++

Several techniques may be used in C++ to produce random floats. For a variety of applications, including simulations and producing random numbers for testing, it might be beneficial to generate random floats in C++. This article will go through different techniques for creating random floats in C++.

Random Float Numbers in C++

Random floats can be generated in C++ using two ways.

1: Using rand() Function

We can generate random floats by using the C++ library’s standard rand() function. We can generate an integer via rand() function, convert it to a float, and then use it to generate random floats. To do this, we can divide the generated integer by a big number, to obtain a number between 0 and 1. A random float can then be obtained by multiplying this value by the desired range of floats. For instance, the following code may be used to produce a random float between 0 and 1:

#include <iostream>
#include <random>
#include <ctime>

int main()
{
    srand(time(NULL));

    for (int i = 0; i < 10; i++) {
        float r = rand() / static_cast<float>(RAND_MAX);
        std::cout << r << std::endl;
    }

    return 0;
}

 
In the above code, the rand() function is used to generate 10 random floating-point values between 0 and 1, and the time() function is used to seed the random number generator. The rand() method generates a number that is completely random and lies between 0 and RAND_MAX. To guarantee that the random number sequence is unique each time the program is run, the srand(time(NULL)) function creates the seed for the random number generator depending on the current time.

Output

 

However, utilizing the rand() technique to generate random floats has a number of limitations. The numbers are not truly random, as an algorithm was employed to generate them. The distribution of the values generated is also not uniform since certain values are produced more frequently than others.

2: Using std::uniform_real_distribution

Using the C++11 random library is an additional method of producing random floats. To produce random numbers of different sorts, this library offers several distributions. We can use the uniform_real_distribution<float> distribution to produce random floats. This distribution produces random floats within a given range using two arguments: a minimum value and a maximum value.

Take a look at this code to generate random floats using uniform_real_distribution.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    default_random_engine rand_number;
    uniform_real_distribution<double> distribution(0.0,5.0);

    for (int i = 1; i < 10; i++) {
        cout << distribution(rand_number) << '\n';
    }

    return 0;
}

 
In the above code, we are using the random library that creates 5 random floating-point values between 0 and 4. It generates a uniform distribution object distribution with a range of [0.0, 5.0] and a default random number engine object rand_number. The for loop uses the distribution object and the gen engine to produce 5 random integers, and then uses cout to display them to the console.

Output

 

The problem with using std::uniform_real_distribution is that we are unable to produce any random sequences, which results in similar sequences each time. The frequency or probability in a specific range may be calculated using this approach, but over a large number of tests.

Conclusion

In C++, there are several approaches to produce random floats. One strategy can be more appropriate than the others depending on the circumstances. The uniform real distribution class in the C++11 random library provides a member function that generates uniformly distributed continuous values or random real numbers from an input range. The rand() function offers a straightforward method for producing random floats. The technique selected will ultimately depend on the needs of the programmer and the demands of their software.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.