Generate a random Binary String of length N
Given a positive integer N, the task is to generate a random binary string of length N.
Examples:
Input: N = 7
Output: 1000001Input: 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:
- Initialize an empty string, say S.
- Iterate over the range [0, N – 1] and perform the following steps:
- Store a random number in the range [0, 1] using rand() function.
- Append the randomly generated 0 or 1 to the end of the string S.
- After completing the above steps, print the string S as the resulting binary string.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>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 = 7generateBinaryString(N)# This code is contributed by sanjoy_62 |
Javascript
<script> // Javascript program for the above approach // Function to find a random // number between 0 or 1 function findRandom() { // Generate the random number let num = (1 + parseInt((Math.random() * 100))) % 2; // Return the generated number return num; } // Function to generate a random // binary string of length N function generateBinaryString(N) { // Stores the empty string let S = ""; // Iterate over the range [0, N - 1] for (let i = 0; i < N; i++) { // Store the random number let x = findRandom(); // Append it to the string S += (x).toString(); } // Print the resulting string document.write(S) } // Driver Code let N = 7; generateBinaryString(N); // This code is contributed by Hritik </script> |
0101101
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


