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++ program for the above approach #include <bits/stdc++.h> 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 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 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 |
// C# program for the above approach using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
// Function to find a random // number between 0 or 1 static 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 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 + x.ToString();
}
// Print the resulting string
Console.WriteLine(S);
} // Driver Code public static void Main ( string [] args)
{ int N = 7;
generateBinaryString(N);
} } // This code is contributed by code_hunt. |
<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) In the above-given approach, there is one loop for iterating over string which takes O(N) time in worst case. Therefore, the time complexity for this approach will be O(N).
Auxiliary Space: O(N)// an extra variable store all the characters of the string and in worst case all characters will be unique hence algorithm takes up linear space