相关疑难解决方法(0)

试图生成一系列独特的随机数

对不起,如果这是显而易见的,我很新.

这是代码.它应该永远不会打印出我理解的相同的两件事,但它有时会这样做.关键是p1为1应该防止p2为1,如果p2为1,则p2应该以相同的p1值再次运行,但是应该生成一个新的随机数.它可能再次为1,但是函数应该继续返回并运行直到它们不同,对吧?

#Random Test with Exclusion
P1Item = 'Empty'
P2Item = 'Empty'
import random
import time

def P1():
    global P1Item
    global P2Exclusion
    P1Local = random.randint(1,3)
    if P1Local == 1:
        P1Item = 'Candy'
        P2(P1Local)
    elif P1Local == 2:
        P1Item = 'Steak'
        P2(P1Local)
    elif P1Local == 3:
        P1Item = 'Vegetables'
        P2(P1Local)


def P2(A):
    global P2Item
        P2Local = random.randint(1,3)
        if P2Local == 1 and A != 1:
            P2Item = 'Candy'
        elif P2Local == 2 and A != 2:
            P2Item = 'Steak'
        elif P2Local …
Run Code Online (Sandbox Code Playgroud)

python random

2
推荐指数
2
解决办法
2482
查看次数

标签 统计

python ×1

random ×1