Open In App

Generate a Random Number between 0 and 1

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:

Below is the implementation of the above approach:




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Using srand() with time(0) to change
    // the random values everytime
    srand(time(0));
 
    // Loop to check that we get different values
    // on every iteration
    for (int i = 0; i < 10; i++) {
 
        // Typecasting the random value
        // obtained into a double and
        // then dividing it with RAND_MAX
        cout << ((double)rand()) / RAND_MAX << endl;
    }
 
    return 0;
}




/*package whatever //do not write package name here */
import java.io.*;
import java.util.Random;
import java.util.UUID;
 
class GFG {
    public static void main (String[] args) {
       for(int i = 0; i < 10; i++)
        {
            Random rand = new Random();
            System.out.println(String.format("%.6f",
                                             (double)Math.abs(rand.nextInt())/Integer.MAX_VALUE));
        }
    }
}
 
// This code is contributed by ksam24000.




using System;
 
public class GFG {
 
    static public void Main()
    {
 
        // Code
        for (int i = 0; i < 10; i++) {
            Random rand = new Random();
            Console.WriteLine(string.Format(
                "{0:F6}", (double)Math.Abs(rand.Next())
                              / Int32.MaxValue));
        }
    }
}
 
// This code is contributed by lokeshmvs21.




// driver code
for(let i = 0; i < 10; i++)
{
   document.write(Math.random())
}
 
// This code is contributed by garg28harsh.




import random
import time
 
random.seed(time.time())
 
for i in range(10):
    print(random.random())

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:




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // using srand() with time(0) to change the random values everytime when the program
    // is ran
    srand(time(0));
 
    // for loop to check that we get different values on every iteration
    for (int i = 1; i < 11; i++) {
 
        // typecasting the random value obtained into a double and then dividing it
        // with INT_MAX
        cout << ((double)rand()) / INT_MAX << endl;
    }
}




// Java code for above approach
import java.io.*;
import java.util.Random;
import java.util.UUID;
 
class GFG {
    public static void main (String[] args) {
    // for loop to check that we get different values on every iteration
    for (int i = 1; i < 11; i++)
    {
            Random rand = new Random();
            // typecasting the random value obtained into a double and then dividing it
            // with INT_MAX
            System.out.println(String.format("%.6f",
                                            (double)Math.abs(rand.nextInt())/Integer.MAX_VALUE));
        }
    }
}
 
// This code is contributed by Pushpesh Raj.




// C# code for above approach
using System;
 
class Program {
    static void Main(string[] args)
    {
        // for loop to check that we get different values on
        // every iteration
        for (int i = 1; i < 11; i++) {
            Random rand = new Random();
            // typecasting the random value obtained into a
            // double and then dividing it with int.MaxValue
            Console.WriteLine("{0:F6}",
                              (double)Math.Abs(rand.Next())
                                  / int.MaxValue);
        }
    }
}
// This code is contributed by Prajwal Kandekar

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:

Below is the Implementation of the above approach:




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    srand(time(0));
    for (int i = 0; i < 10; i++) {
        // defining a range of numbers and then dividing the obtained random number
        // with the largest number in the range + 1
        cout << ((double)(rand() % 100000000) + 1) / 100000001 << endl;
    }
}

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.


Article Tags :