Trying to generate a series of unique random numbe

2020-02-02 02:23发布

Sorry if this is obvious, im pretty new.

Here is the code. It should never print the same two things as i understand it, but it sometimes does. The point is that p1 being 1 should prevent p2 from being 1, and if p2 is 1, p2 should run again with the same p1 value, but should generate a new random number. It might be 1 again, but then the function should just keep returning else and running until they're different, right?

#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 == 3 and A != 3:
        P3Item = 'Vegetables'
        else:
            B = A
            P2(B)

def Test():
    print('Test')
    print('Define')
    P1()
    print(P1Item + ' ' + P2Item)
    time.sleep(1)
    input()
    Test()

Test()

标签: python random
4条回答
放荡不羁爱自由
2楼-- · 2020-02-02 02:30
from random import choice
x = ['foo','bar','fight']
num_uniq = 2
uniq_rand = set()
while len(uniq_rand) < num_uniq:
  uniq_rand.add(choice(x))
print uniq_rand

As @Martijn pointed out, this is not surely not as efficient as random.sample() =)

查看更多
迷人小祖宗
3楼-- · 2020-02-02 02:38

Instead of picking random integers, shuffle a list and pick the first two items:

import random

choices = ['Candy', 'Steak', 'Vegetables']
random.shuffle(choices)

item1, item2 = choices[:2]

Because we shuffled a list of possible choices first, then picked the first two, you can guarantee that item1 and item2 are never equal to one another.

Using random.shuffle() leaves the option open to do something with the remaining choices; you only have 1 here, but in a larger set you can continue to pick items that have so far not been picked:

choices = list(range(100))
random.shuffle(choices)
while choices:
    if input('Do you want another random number? (Y/N)' ).lower() == 'n':
        break
    print(choices.pop())

would give you 100 random numbers without repeating.

If all you need is a random sample of 2, use random.sample() instead:

import random

choices = ['Candy', 'Steak', 'Vegetables']

item1, item2 = random.sample(choices, 2)
查看更多
混吃等死
4楼-- · 2020-02-02 02:41

if you want to keep the original logic, here is some pseudo code:

while(P2Local == A)
   P2Local = random.randint(1,3)
查看更多
疯言疯语
5楼-- · 2020-02-02 02:47

You can use the random module in Python to do the heavy lifting for you, specifically random.sample():

>>> import random
>>> random.sample(['candy', 'steak', 'vegetable'], 2)
['vegetable', 'steak']
查看更多
登录 后发表回答