Generate a random Binary String of length N | Python3 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
import random NEW_LINE def findRandom ( ) : NEW_LINE INDENT num = random . randint ( 0 , 1 ) NEW_LINE return num NEW_LINE DEDENT def generateBinaryString ( N ) : NEW_LINE INDENT S = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = findRandom ( ) NEW_LINE S += str ( x ) NEW_LINE DEDENT print ( S ) NEW_LINE DEDENT N = 7 NEW_LINE generateBinaryString ( N ) NEW_LINE