Generate a random Binary String of length N | Java 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
class GFG { static int findRandom ( ) { int num = ( 1 + ( int ) ( Math . random ( ) * 100 ) ) % 2 ; return num ; } static void generateBinaryString ( int N ) { String S = " " ; for ( int i = 0 ; i < N ; i ++ ) { int x = findRandom ( ) ; S = S + String . valueOf ( x ) ; } System . out . println ( S ) ; } public static void main ( String [ ] args ) { int N = 7 ; generateBinaryString ( N ) ; } }