Related Articles
Generate a random Binary String of length N
• Last Updated : 22 Apr, 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 1``int` `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 N``void` `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 Code``int` `main()``{``    ``int` `N = 7;``    ``generateBinaryString(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG{``    ` `// Function to find a random``// number between 0 or 1``static` `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 N``static` `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 Code``public` `static` `void` `main (String[] args)``{``    ``int` `N = ``7``;``    ` `    ``generateBinaryString(N);``}``}` `// This code is contributed by AnkThon`

## Python3

 `# Python3 program for the above approach``import` `random` `# Function to find a random``# number between 0 or 1``def` `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 N``def` `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 Code``N ``=` `7` `generateBinaryString(N)` `# This code is contributed by sanjoy_62`

## Javascript

 ``
Output:
`0101101`

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up