Make bone values ​​not repeated in if statement

I would like my values ​​in the bones not to be repeated, because when this happens, it registers an incorrect input in my program (and not a failure, just a string message with the message "Your input was incorrect"). This is a board game, so I do not want the same values ​​to be repeated, for example, 6.0 repeated twice or even three times. Is there a way to save bone values, or is there anything I can do to select new random values ​​each time?

dice = random.randint(0,3) ans = network.receive() if dice == 0: guess = str(random.randint(0,4))+','+str(random.randint(0,4)) elif dice == 1: guess = str(random.randint(0,4))+','+str(random.randint(4,9)) elif dice == 2: guess = str(random.randint(4,9))+','+str(random.randint(0,4)) else: guess = str(random.randint(4,9))+','+str(random.randint(4,9)) 

Required Conclusion:

 6,0 4,5 8,1 1,7 

with no repetition, for example:

 6,0 8,2 6,0 #this is a repeat, I do not want this to happen 3,9 
+5
source share
3 answers

Or you can flip over and over again until a new combination appears. It also implies that you need to make some account of the combinations already made. And you must make sure that at least one possible combination remains, otherwise the cycle will not end.

 combis = [] dice = random.randint(0,3) ans = network.receive() while True: if dice == 0: guess = str(random.randint(0,4))+','+str(random.randint(0,4)) elif dice == 1: guess = str(random.randint(0,4))+','+str(random.randint(4,9)) elif dice == 2: guess = str(random.randint(4,9))+','+str(random.randint(0,4)) else: guess = str(random.randint(4,9))+','+str(random.randint(4,9)) if not guess in combis: combis.append(guess) break 
+3
source

You can use a dictionary that matches dice with random.randint call random.randint :

 >>> mapping = { ... 0: [0, 4, 0, 4], # if dice == 1 ... 1: [0, 4, 4, 9], # elif dice == 2 ... 2: [4, 9, 0, 4], # elif dice == 3 ... } >>> mapping[0] [0, 4, 0, 4] >>> a, b, c, d = mapping[0] >>> a 0 >>> b 4 >>> c 0 >>> d 4 

Further, using collections.defaultdict , you do not need to handle the else case specifically.

 from collections import defaultdict dice = random.randint(0, 3) ans = network.receive() dice_random_mapping = defaultdict(lambda: [4, 9, 4, 9], { # else 0: [0, 4, 0, 4], # if dice == 1 1: [0, 4, 4, 9], # elif dice == 2 2: [4, 9, 0, 4], # elif dice == 3 }) if ans == None: start1, stop1, start2, stop2 = dice_random_mapping[dice] guess = str(random.randint(start1, stop1))+','+str(random.randint(start2, stop2)) 
+4
source

This function can perform all the cubes for you. It is a bit optimized.

 import random def roll_dice(low=1, high=6, count=1, allow_repeats=True): if not allow_repeats and count > (high - low): raise ValueError('Impossible not to have repeats with so many dice.') if not allow_repeats: possible_rolls = range(low, high) + [high] return random.sample(possible_rolls, count) return [random.randint(low, high) for _ in range(count)] 

samples:

 >>> roll_dice() [1] >>> roll_dice(count=2) [5, 3] >>> roll_dice(3, 10, count=2) [8, 3] >>> roll_dice(count=5, allow_repeats=False) [6, 3, 2, 1, 4] >>> roll_dice(count=5, allow_repeats=True) [6, 6, 1, 5, 3] 
+2
source

Source: https://habr.com/ru/post/1259007/


All Articles