Generate a random Binary String of length N

• Difficulty Level : Medium
• Last Updated : 01 Dec, 2021

Given a positive integer N, the task is to generate a random binary string of length N.

Examples:

Input: N = 7
Output: 1000001

Input: N = 5
Output: 01001

Approach: The given problem can be solved by using the rand() function that generates a random number over the range [0, RAND_MAX] and with the help of the value returned by this function, any number in any range [L, R] can be generated as (rand() % (R – L + 1)) + L. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++

 // C++ program for the above approach #include using namespace std; // Function to find a random// number between 0 or 1int findRandom(){    // Generate the random number    int num = ((int)rand() % 2);     // Return the generated number    return num;} // Function to generate a random// binary string of length Nvoid generateBinaryString(int N){    srand(time(NULL));     // Stores the empty string    string S = "";     // Iterate over the range [0, N - 1]    for (int i = 0; i < N; i++) {         // Store the random number        int x = findRandom();         // Append it to the string        S += to_string(x);    }     // Print the resulting string    cout << S;} // Driver Codeint main(){    int N = 7;    generateBinaryString(N);     return 0;}

Java

 // Java program for the above approachclass GFG{     // Function to find a random// number between 0 or 1static int findRandom(){         // Generate the random number    int num = (1 + (int)(Math.random() * 100)) % 2;     // Return the generated number    return num;} // Function to generate a random// binary string of length Nstatic void generateBinaryString(int N){         // Stores the empty string    String S = "";     // Iterate over the range [0, N - 1]    for(int i = 0; i < N; i++)    {                 // Store the random number        int x = findRandom();         // Append it to the string        S = S + String.valueOf(x);    }     // Print the resulting string    System.out.println(S);} // Driver Codepublic static void main (String[] args){    int N = 7;         generateBinaryString(N);}} // This code is contributed by AnkThon

Python3

 # Python3 program for the above approachimport random # Function to find a random# number between 0 or 1def findRandom():         # Generate the random number    num = random.randint(0, 1)     # Return the generated number    return num # Function to generate a random# binary string of length Ndef generateBinaryString(N):         # Stores the empty string    S = ""     # Iterate over the range [0, N - 1]    for i in range(N):                 # Store the random number        x = findRandom()         # Append it to the string        S += str(x)         # Print the resulting string    print(S) # Driver CodeN = 7 generateBinaryString(N) # This code is contributed by sanjoy_62

C#

 // C# program for the above approachusing System;using System.Collections.Generic;using System.Linq; public class GFG {     // Function to find a random// number between 0 or 1static int findRandom(){       // For random generator    Random rand = new Random();         // Generate the random number    int num =  rand.Next() % 2;      // Return the generated number    return num;}  // Function to generate a random// binary string of length Nstatic void generateBinaryString(int N){          // Stores the empty string    string S = "";      // Iterate over the range [0, N - 1]    for(int i = 0; i < N; i++)    {                  // Store the random number        int x = findRandom();          // Append it to the string        S = S + x.ToString();    }      // Print the resulting string    Console.WriteLine(S);}  // Driver Codepublic static void Main (string[] args){         int N = 7;    generateBinaryString(N);}} // This code is contributed by code_hunt.

Javascript


Output:
0101101

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

My Personal Notes arrow_drop_up