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