如何生成随机字符串? [复制]

Posted

技术标签:

【中文标题】如何生成随机字符串? [复制]【英文标题】:How to generate random string? [duplicate] 【发布时间】:2018-01-06 23:06:20 【问题描述】:

我只是想问一下,喜欢从:Random.randint(a, b)获取随机数

我只是想问一下如何像randint 一样获取随机字符串,但这次是随机字符串。到底有没有?

#This program will play a little game

import random

a = ''
b = ''
c = ''
d = ''
e = ''
f = ''

print('Hi. Please enter your name in letters')
name = str(input())
print('Hi ' + name + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.')
print('But do not worry. I am going to let you to enter 6 names and i will choose one of them.')
print('After that you have to answer the correct name that i am thinking right now')
print('Please enter the first name in letters')
name1 = str(input())
print('Please enter the second name in letters')
name2 = str(input())
print('Please enter the third name in letters')
name3 = str(input())
print('Please enter the fourth name in letters')
name4 = str(input())
print('Please enter the fifth name in letters')
name5 = str(input())
print('Please enter the sixth name in letters')
name6 = str(input())
name1 = a
name2 = b
name3 = c
name4 = d
name5 = e
name6 = f

print('Alright ' + name + ' . Thank you for entering the names.')
secretname = random.randint(a, f)
for i in range(2):
        print('Now enter the name that i am thinking of.')
        ans = str(input())
        if ans != secretname:
                print('Wrong. Guess another name')

if ans == secretname:
        print('Good job ' + name)
else:
        print('Wrong. The name i was thinking of was ' + secretname)

这是一个小游戏,它要求你输入 6 个名字,然后游戏会在你输入的这 6 个数字之间猜一个数字,但它总是给我一个错误。想用随机字符串来做。

我的工作版本

import random

print('Hi. Please enter your name in letters')
yourname = str(input())
print('Hi ' + yourname + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.\nBut do not worry. I am going to let you to enter 6 names and i will choose one of them.\nAfter that you have to answer the correct name that i am thinking right now\nPlease enter the first name in letters')

name = ["", "", "", "", "", ""]
number = ["first", "second", "third", "fourth", "fifth", "sixth"]


def insert(n):
    temp = name.copy()
    name[n] = str(input("name " + str(n + 1) + ": "))
    if name[n] in temp:
        print("[error]: ---> This name exists yet, try again")
        print(">>>", end='')
        insert(n)

for n in range(6):
    insert(n)
    print('Please enter the ' + number[n] + ' name in letters')

print('Alright ' + yourname + ' . Thank you for entering the names.')
secretname = name[random.randint(0, 6)]
print("Soluzion: ", secretname)
print("-" * 10)
for i in range(2):
    print('Now enter the name that i am thinking of.')
    ans = str(input())
    if ans != secretname:
        print('Wrong. Guess another name')

if ans == secretname:
    print('Good job ' + yourname)
else:
    print('Wrong. The name i was thinking of was ' + secretname)

输出

Hi. Please enter your name in letters Gio Hi Gio. I am going to play a
little game. In this game you have to guess a specific name i am
thinking right now. But do not worry. I am going to let you to enter 6
names and i will choose one of them. After that you have to answer the
correct name that i am thinking right now Please enter the first name
in letters 
name 0: Giovanni
Please enter the first name in letters
name 1: Marta
Please enter the second name in letters
name 2: Giovanni
[error]: ---> This name exists yet, try again
>>>name 2: Giovanni
[error]: ---> This name exists yet, try again
>>>name 2: Carlo
Please enter the third name in letters 
name 3: Mimmo Please enter the fourth name in letters 
name 4: June
Please enter the fifth name in letters
name 5: Caterina Please enter the sixth name in letters
Alright Gio . Thank you for entering the names.
Solution: Mimmo
----------
Now enter the name that i am thinking of.
M 
Wrong. Guess another name Now enter the name that i am thinking of.
Mimmo 
Good job Gio

【问题讨论】:

你的代码有很多问题......不需要在input()周围使用str,你的名字变量在输入后通过name1=a等被删除......你根本没有使用random.randint....我建议您在将它们全部加入程序之前尝试更小的部分,看看它们是否有效。如果您有具体问题,请免费打开有关实际错误的更详细问题,但只有 minimal 示例 【参考方案1】:

您可以使用 random.randint 来做到这一点:

my_string = "abcdefghijklmnopqrstuvwxyz"
index = random.randint(0,25)
letter = my_string[index]

你只需要循环它就可以从字母构建字符串

【讨论】:

【参考方案2】:

试试这个,

import string
alpha = string.ascii_uppercase
num = string.digits
''.join(random.choice(alpha + num) for _ in range(5)) #number you want

【讨论】:

【参考方案3】:

我建议使用一个包含输入名称的列表,而不是将名称保存到不同的变量中。它可能看起来像这样:

name_list = []
print('Please enter the first name in letters')
name_list.append(input())
print('Please enter the second name in letters')
name_list.append(input())
...

然后您可以使用random.choice 随机选择一个列表项,例如

import random
secretname = random.choice(name_list)

【讨论】:

【参考方案4】:

更新:

对于您描述的特定情况,您可以简单地创建一个包含您允许的所有字符的字符列表。那么您可以使用它来创建一个长度为X 的单词(替换为实际长度:

base_chars = [ 'a', 'b', 'c', '1', '2', '3' ]
word_length = 10 #change this to the desired word length
[random.choice(base_chars) for _ in range(word_length)]

原文:

随机字符串 - 定义你的字符

您应该首先决定要使用哪种字符。 例如,如果您想使用从 0 到 127 的 ASCII 字符,或者只是 a-z 和 A-Z。还要考虑数字、空格等。所以你应该首先决定要从中随机选择的字符集。

随机选择一个字符

如果你想在 ASCII 表中使用 0 到 127 的范围,你可以使用这个:

char = chr(random.randint(0, 127))

生成随机词

现在,要创建一个单词,您应该首先确定它的大小。这也可以是随机的。例如,我们会随机选择一个单词的大小(范围有限):

rand_str = ""
for _ in range(10):
     rand_str += chr(random.randint(0, 127))

【讨论】:

【参考方案5】:

您好,M. Husnain,

Chr() 函数

chr(i) 返回表示其 Unicode 代码点为整数 i 的字符的字符串。例如,chr(97) 返回字符串 'a',而 chr(8364) 返回字符串 '€'。这是 ord() 的倒数。

参数的有效范围是从 0 到 1,114,111(基数为 16 的 0x10FFFF)。如果 i 超出该范围,将引发 ValueError。

解决方案

试试下面的代码,

#This program will play a little game

import random

a = ''
b = ''
c = ''
d = ''
e = ''
f = ''

print('Hi. Please enter your name in letters')
name = raw_input()


print('Hi ' + name + '. I am going to play a little game. In this game you have to guess a specific name i am thinking right now.')
print('But do not worry. I am going to let you to enter 6 names and i will choose one of them.')
print('After that you have to answer the correct name that i am thinking right now')
print('Please enter the first name in letters')
name1 = raw_input()

print('Please enter the second name in letters')
name2 = raw_input()

print('Please enter the third name in letters')
name3 = raw_input()

print('Please enter the fourth name in letters')
name4 = raw_input()

print('Please enter the fifth name in letters')
name5 = raw_input()

print('Please enter the sixth name in letters')
name6 = raw_input()

print('Alright ' + name + ' . Thank you for entering the names.')

lists1 = [name1,name2,name3,name4,name5,name6]

secretname = lists1[random.randint(0,len(lists1))]

for i in range(2):
        print('Now enter the name that i am thinking of.')
        ans = raw_input()
        if ans != secretname:
                print('Wrong. Guess another name')

if ans == secretname:
     print('Good job ' + name)
else:
    print('Wrong. The name i was thinking of was ' + secretname)

希望我的回答对你有所帮助。 如有任何疑问,请发表评论。

【讨论】:

【参考方案6】:
import string
import random
def random_string(length):
    return ''.join(random.choice(string.ascii_letters) for m in xrange(length))

print random_string(10)
print random_string(5)

输出:

'oJyPiEbhka'
'XXwuA'

【讨论】:

以上是关于如何生成随机字符串? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何检查字符串是不是至少有 1 个字母字符? [复制]

kettle过滤生成随机数改变开始复制数量

如何用VB.NET生成一个随机字符串

Jmeter-函数生成参数

如何从字符串列表中保存随机字符串? [复制]

C语言 如何随机生成字符串