# C++ random number between 0 and 1 Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes.

In this tutorial, we will generate random number between 0 and 1 in C++.

The numbers generated are fractions, so they will be either float or double values.

## Using the `rand()` function to generate random number between 0 and 1 in C++

The `rand()` function is used to generate a random number in C++. To work with this function we need to import the `random` library in C++. We can specify the range for the `rand()` function.

To generate random number between 0 and 1, we use the following code.

Output:

0.840188
0.394383
0.783099
0.79844
0.911647

In the above code,

• We generated 5 random numbers using the `rand()` function and running it 5 times using the `for` loop.
• The value for `RAND_MAX` constant is implementation-dependent and changes from compiler to compiler.
• We typecast the final number to a `float` value.

It should be noted that whenever this code is run, the outcome will always be the same 5 random numbers. To fix this, we can use the `srand()` function, which sets the starting point for random number generation.

It is not advisable to use a constant value for the `srand()` function, so instead we use the `time()` function which helps in generating a different sequence every time.

See the following code.

Output:

0.257404
0.980979
0.616666
0.0109556
0.953417

Another thing to remember is that the above method will include 0 and 1 as random numbers. We can exclude 1 from it, by adding 1.0 in the denominator making it `(float)(RAND_MAX + 1.0)`

## Using the `std::uniform_real_distribution()` function to generate random number between 0 and 1 in C++

This function is used to generate float values between a given range. We can generate random number between 0 and 1 using this function as shown below.

Output:

0.0935004
0.864206
0.830996
0.756512
0.206154

In the above example,

• The `random_device` initializes the seed for the random number generation engine.
• We specify the `<float>` datatype, otherwise, the final output can be in `double` or `long double` type also.
• The range for the numbers is specified in the `dis()` argument.
That’s all about C++ random number between 0 and 1.

## Related Posts

• 08 October

### Get Number of Elements in Array in C++

Table of ContentsGet Number of Elements in Array in C++Using the sizeof() functionUsing the pointersUsing the size() functionGet the frequency of each element in array in C++Using nested for loopUsing maps One of the most fundamental collections in any programming language is an array. It stores a similar type of data at specific indices that […]

• 19 July

### Trim String in C++

Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

• 19 July

### JSON Parser in C++

Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

• 19 July

### Convert enum to string in C++

Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

• 07 June

### Check if string contains substring in C++

Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

• 24 May

### Convert float to String in C++

Learn about how to convert float to String in C++ in different wayds.

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.