Python Program to Generate Random binary string
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
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- 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