生成随机二进制字符串的Python程序
给定一个数字 n,任务是生成一个长度为 n 的随机二进制字符串。
例子:
Input: 7
Output: Desired length random binary string is: 1000001
Input: 5
Output: Desired length random binary string is: 01001
方法
- 初始化一个空字符串,比如 key
- 使用 random 包中的 randint函数随机生成“0”或“1”。
- 将随机生成的“0”或“1”附加到字符串, key
- 重复步骤 2 和 3 以获得所需的字符串长度
下面是实现。
Python3
# Python program for random
# binary string generation
import random
# Function to create the
# random binary string
def rand_key(p):
# Variable to store the
# string
key1 = ""
# Loop to find the string
# of desired length
for i in range(p):
# randint function to generate
# 0, 1 randomly and converting
# the result into str
temp = str(random.randint(0, 1))
# Concatenation the random 0, 1
# to the final result
key1 += temp
return(key1)
# Driver Code
n = 7
str1 = rand_key(n)
print("Desired length random binary string is: ", str1)
输出:
Desired length random binary string is: 1000001