# 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

• 19 May

### Get Type of Object in C++

Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++ConclusionWas this post helpful? C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, […]

• 19 May

### Convert Vector to Array in C++

Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

• 23 April

### How to initialize an Array in Constructor in C++

Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusionWas this post helpful? This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays […]

• 31 March

### Write String to File in C++

Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++ConclusionWas this post helpful? The files are used to store the data permanently on a disk. This mitigates […]

• 23 March

### Print Array in C++

Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusionWas this post helpful? An […]

• 23 March

### Print Vector in C++

Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusionWas this post helpful? This article discusses the vector and how to print vector in […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.