Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不同结果的多重随机选择_Python_Random - Fatal编程技术网

Python 不同结果的多重随机选择

Python 不同结果的多重随机选择,python,random,Python,Random,我试图用Python制作一个随机的NPC生成器上次我用PHP做这个,结果。奇怪的是。我希望能够使用它多次调用字符串中定义的变量。我可以通过在rangen循环中运行for I来实现这一点,但每次都会得到相同的随机选择 我一直在寻找,但我不完全确定我是否找到了多次调用该函数并每次得到不同结果的方法 import random gend = [ 'male', 'female' ] race = [ 'Human', 'Elf', 'Orc', 'Halfling', 'Gnome', 'Half-E

我试图用Python制作一个随机的NPC生成器上次我用PHP做这个,结果。奇怪的是。我希望能够使用它多次调用字符串中定义的变量。我可以通过在rangen循环中运行for I来实现这一点,但每次都会得到相同的随机选择

我一直在寻找,但我不完全确定我是否找到了多次调用该函数并每次得到不同结果的方法

import random
gend = [ 'male', 'female' ]
race = [ 'Human', 'Elf', 'Orc', 'Halfling', 'Gnome', 'Half-Elf', 'Half-Orc', 'Outsider' ]
pers = [ 'leader-like, optimistic', 'bad-tempered, irritable', 'relaxed, peaceful', 'quiet, analytical' ]
hook = [ 'a chore that needs doing', "some concerns for another's well-being", 'an item that needs retrieving/delivering',
    'a grudge', 'a person/organization in their way', 'a target on his or her back', 'an indiscretion that needs covering up',
    'a debt that needs paying' ]
randgend = random.choice(gend)
randrace = random.choice(race)
randpers = random.choice(pers)
randhook = random.choice(hook)
print("A {} {}, with a {} disposition and {}.".format(randgend, randrace, randpers, randhook))
该行:

randgend = random.choice(gend)
使randgend成为[‘男性’、‘女性’]中的一个随机选择,你基本上在写:

randgend = 'male'  # or female, whichever gets picked first
如果希望它是一个每次返回不同随机选择的函数,则需要:

randgend = lambda: random.choice(gend)
请参见,然后称之为:

print("A {} {}, with a {} disposition and {}.".format(
    randgend(),  # note parentheses
    ...,
))
或者,简单得多,只需将呼叫移动到random.choice:


如果你想要一个函数,试试这个

def myrandfunction():
    randgend = random.choice(gend)
    randrace = random.choice(race)
    randpers = random.choice(pers)
    randhook = random.choice(hook)
    return ("A {} {}, with a {} disposition and {}.".format(randgend, randrace, randpers, randhook))

>>>print(myrandfunction())
"A female Elf, with a relaxed, peaceful disposition and some concerns for another's well-being."
>>>print(myrandfunction())
'A male Half-Orc, with a leader-like, optimistic disposition and an indiscretion that needs covering up.'
若你们不想重复这个句子,就用


你也可以这样使用它

print("A {} {}, with a {} disposition and {}.".format(random.choice(gend), random.choice(race), random.choice(pers), random.choice(hook)))

你看到的问题是什么?您希望得到什么样的答案?非常感谢您的帮助!我知道这很简单。
>>>from itertools import product
>>>myrandoms = product(gend,race,pers,hook)

>>>"A {} {}, with a {} disposition and {}.".format(*[i for i in next(myrandoms)])
'A male Human, with a bad-tempered, irritable disposition and an item that needs retrieving/delivering.'
>>>"A {} {}, with a {} disposition and {}.".format(*[i for i in next(myrandoms)])
'A male Human, with a leader-like, optimistic disposition and an indiscretion that needs covering up.'
print("A {} {}, with a {} disposition and {}.".format(random.choice(gend), random.choice(race), random.choice(pers), random.choice(hook)))