如何创建一个包含5个字符的随机字符串? (1,2,3,4,5)并定义这些字符的百分比。
我想在新的random_char
中定义%[/ 1“,”2“,”3“,”4“,”5“]中每个字符的%次数。例如:
当然不超过100%。
import random
string = ["1","2","3","4","5"]
random_char=''
for i in range(0,100):
random_char+=random.choice(string)
print(random_char)
答案 0 :(得分:2)
我不确定我理解你的问题,但这是猜测:
import random
num_chars = 100
characters = "12345"
percentages = .50, .20, .10, .15, .05
counts = [int(percent*num_chars) for percent in percentages]
random_chars = ['' for _ in range(num_chars)] # Initialize to empty chars.
for char, count in zip(characters, counts):
for _ in range(count):
while True:
index = random.randrange(0, num_chars) # Pick random spot.
if not random_chars[index]: # Empty?
random_chars[index] = char
break
random_chars = ''.join(random_chars)
print('random_chars:', random_chars)
# Verify result is correct.
if len(random_chars) != num_chars:
print('Generated string is wrong length ({} instead of {})'.format(
len(random_chars), num_chars))
# Check distribution of characters.
for char in characters:
count = sum(1 for i in range(num_chars) if random_chars[i] == char)
print('Character {!r} is in result: {} times'.format(char, count))
示例输出:
random_chars: 1422121211111141334154411511142221114211131212342242222332511241351121114314151431434111111111121111
Character '1' is in result: 50 times
Character '2' is in result: 20 times
Character '3' is in result: 10 times
Character '4' is in result: 15 times
Character '5' is in result: 5 times
但是,正如我在评论中所说,使用random.shuffle()
会更有效率和直截了当:
num_chars = 100
characters = "12345"
percentages = 50, 20, 10, 15, 5
random_chars = [char*round(percent/100*num_chars)
for char, percent in zip(characters, percentages)]
random.shuffle(random_chars)
random_chars = ''.join(random_chars)
print('random_chars:', random_chars)
答案 1 :(得分:2)
如果你总是想要一个100-char-long字符串中的固定比例,你可以简单地用你想要的字符串洗牌:
import random
s = list('1' * 50 + '2' * 20 + '3' * 10 + '4' * 15 + '5' * 5)
random.shuffle(s)
''.join(s)
这给你一些类似的东西:
'1112213241213121114254411211141541342111141411411152423322114133124141512212211521213131111111311214'
如果您希望它是随机的并且有任何长度,那么您可以使用choices
代替。
options = '12345'
weights = [.5, .2, .1, .15, .05]
length = 10 # how many characters long
''.join(random.choices(options, weights, k=length))
结果如下:
'5514232551'
答案 2 :(得分:1)
函数<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
是python 3.6中的新功能,但您可以使用random.choices
轻松实现它:
random.shuffle
示例输出:
import random
def choices(options, percentages, length):
result = []
for (char, weight) in zip(options, percentages):
n = int(weight / 100.0 * length)
result.extend(char * n)
random.shuffle(result)
return ''.join(result)
def test(options, percentages, length):
string = choices(options, percentages, length)
print(string)
for char in options:
count = string.count(char)
perc = count / length * 100.0
print('Character {!r} is in result: {} times = {}%'.format(char, count, perc))
print()
percentages = 50, 20, 10, 15, 5
test(['1', '2', '3', '4', '5'], percentages, 40)
test(['a', 'b', 'c', 'd', 'e'], percentages, 80)
test(['q', 'w', 'e', 'r', 't'], percentages, 100)