VOOZH about

URL: https://www.geeksforgeeks.org/cpp/generate-a-random-number-between-0-and-1/

⇱ Generate a Random Number between 0 and 1 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generate a Random Number between 0 and 1

Last Updated : 13 Mar, 2023

The Task is to generate a random number between 0 and 1.

It is obvious that the number between 0 and 1 will be a floating point number. To generate a random number between 0 and 1, we will make use of the rand() function. The rand() function creates a random number.

Approach: Generating a Random Number between 0 and 1 with RAND_MAX value

RAND_MAX value is basically the maximum value that can be obtained by the rand() function. So, first of all, we will be using the srand() function for creating different values on every run.

Follow the steps mentioned below to implement the idea:

  • Start a loop to check whether we are getting different random numbers on every iteration.
  • Then, typecast the rand() function value in a double value as the number we want should be a decimal point number. 
  • inside the loop, we will output the random value obtained by rand() function divided by the RAND_MAX value.
  • As the RAND_MAX value will be greater than the random number obtained by rand() function most of the time, we will get the result as '0.n'. 

Below is the implementation of the above approach:


Output
0.288481
0.338828
0.512149
0.810278
0.560978
0.683509
0.0417002
0.332409
0.0942754
0.938507

Time Complexity: O(1) 
Auxiliary Space: O(1)

Another Approach: 

The same thing can be done using INT_MAX instead of using RAND_MAX.

Below is the implementation of the above approach:


Output
0.645138
0.249392
0.313409
0.628442
0.491985
0.902581
0.400526
0.123816
0.397556
0.520698

Time Complexity: O(1) 
Auxiliary Space: O(1)

Approach for generating a random number by defining a range:

The idea of this approach is that we will use the rand() function to get the random numbers between the defined range and then dividing it with a slightly larger number than the largest number in the defined range. 

Follow the steps mentioned below to implement the idea:

  • Run a loop and Inside the loop, set the rand() function to create a random number between the defined range. 
  • Then take range from 1 to 109. We are taking a big range of numbers as the number should not repeat most of the time. 
    • If the number repeats, we will get the same number between 0 and 1 every time the number repeats. Taking a small range of numbers will cause the program to output the same random values most of the time. 
  • After defining the range, we will be dividing the obtained number by n + 1, with n being the largest number in the defined range.

Below is the Implementation of the above approach:


Output
0.682537
0.213042
0.429056
0.583094
0.294206
0.327242
0.309042
0.165038
0.665797
0.542586

Time Complexity: O(1), since loop is traversed only from 1 to 10, and rand() method used O(1) time
Auxiliary Space: O(1), since only variables are created.

Comment