Given a number n, the task is to generate a random binary string of length n.
Input: 7 Output: Desired length random binary string is: 1000001 Input: 5 Output: Desired length random binary string is: 01001
- Initialize an empty string, say key
- Generate a randomly either “0” or “1” using randint function from random package.
- Append the randomly generated “0” or “1” to the string, key
- Repeat step 2 and 3 for the desired length of the string
Below is the implementation.
Desired length random binary string is: 1000001
- Python | Generate random string of given length
- Python | Generate random number except K in list
- Python - Generate random number except K in list
- Python | Generate random numbers within a given range and store in a list
- Secrets | Python module to Generate secure random numbers
- Program to generate all possible valid IP addresses from given string
- Python Program to Convert any Positive Real Number to Binary string
- Python | Program to generate one-time password (OTP)
- Generate two output strings depending upon occurrence of character in input string in Python
- random.random() function in Python
- Python | Check if a given string is binary string or not
- Python | Convert String to Binary
- Convert binary to string using Python
- Python program to convert floating to binary
- Python Program for Binary Insertion Sort
- Python Program for Binary Search (Recursive and Iterative)
- Python program to modify the content of a Binary File
- Python program to convert decimal to binary number
- Generating random strings until a given string is generated
- Python Program to Count number of binary strings without consecutive 1's
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.