Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create random pairs
#1
I am trying to create random pairings from a list of players.
I first need to randomize the entire list into pairs and then divide that list into halves.
I then want to calculate which of the fist group of pairings will play against the others.

example. List 1,2,3,4,5,6,7,8,9,10

Random Pairs 2 and 4, 1 and 3,5 and 9, 10 and 7, 6 and 8,

Then divide this list in two and create new pair of pairs

Example 2 and 4 play 1 and 3, 5 and 9 play 10 and 7 and so on.....

I have worked out the first pairs, but am stuck on the second part.

How do I complete the code?

import random

# Input the actual number of players registered to play today
num_players = int(input("How many players are there today? "))

# Add 1 to num_players to make the number correct
num_players += 1

# Create the list of players from "1" to number entered in input
my_list = list(range(1, num_players))
l = my_list

# Create pairs
pairs = {}


while len(l) > 1:

    #Using the randomly created indices, respective elements are popped out
    r1 = random.randrange(0, len(l))
    elem1 = l.pop(r1)

    r2 = random.randrange(0, len(l))
    elem2 = l.pop(r2)

    # now the selected elements are paired in a dictionary
    pairs[elem1] = elem2

#The variable 'pairs' is now a dictionary of randomised pairs


##We can now print the elements of the dictionary in your desired format:
i = 1

for key, value in pairs.items():

# Print out the result
    print("Rink {:2d}: {:6d}  and  {:3d}".format(i, key, value))
    i += 1
Reply
#2
Not sure what 2 and 4 play 1 and 3 mean, but in my opinion you don't need 2-step procedure. selecting each 4 elements in random is enough. then first pair play the second pair

import random
players = set(range(1,21))
while players:
    pairs = random.sample(players,4)
    print(pairs)
    players -= set(pairs)


# an alternative
print('\n\nNow the alternative')
players = range(1,21)
random.shuffle(players)
while players:
    pairs=[players.pop() for _ in range(4)]
    print (pairs)
Output:
[19, 7, 14, 16] [20, 6, 4, 18] [15, 12, 10, 11] [5, 13, 3, 2] [17, 9, 8, 1] Now the alternative [7, 10, 17, 1] [2, 14, 6, 8] [12, 13, 3, 20] [5, 15, 18, 4] [16, 19, 11, 9] >>>
if we use your language 19 and 7 play 14 and 16
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The randomizing of pairs is just what I need. Thanks.
However, the output needs to be in the format of my original code output as I have to allocate rink numbers to the pairings.
The people using this output would not understand a list of 4 elements.
I would need to print it as
Rink 1: 19 and 7 play 14 and 16
This output would then be used manually by the organisers.
Is there a way I can modify your code to present the data this way?
I am a novice at Python and appreciate any ideas.
Reply
#4
import random

def draw(num_players):
    players = set(range(1,num_players+1))
    while players:
        pairs = random.sample(players,4)
        players -= set(pairs)
        yield pairs

for rink, pairs in enumerate(draw(num_players=20), start=1):
    print('Rink {}: {} and {} play {} and {}'.format(rink, *pairs))

Actually, let's make it in more proper way:
import random

def draw(num_players):
    players = set(range(1,num_players+1))
    while players:
        pairs = random.sample(players,4)
        players -= set(pairs)
        yield pairs

if __name__ == '__main__':
    while True:
        try:
            num_players = int(input('How many players are there today? '))
            if num_players % 4:
                print('Number of players must be divisible by 4')
            else:
                break
        except ValueError:
            print('This is not a valid input')

    for rink, pairs in enumerate(draw(num_players=num_players), start=1):
        print('Rink {}: {} and {} play {} and {}'.format(rink, *pairs))
Output:
How many players are there today? a This is not a valid input How many players are there today? 13 Number of players must be divisible by 4 How many players are there today? 12 Rink 1: 2 and 1 play 5 and 4 Rink 2: 12 and 7 play 3 and 11 Rink 3: 8 and 9 play 6 and 10
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Pairs Trading Simulation Kiitoos 0 246 Feb-19-2024, 08:27 PM
Last Post: Kiitoos
  Sample random, unique string pairs from a list without repetitions walterwhite 1 463 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,528 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Pairs of multiplied prime number--->N Frankduc 13 3,558 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  Extracting unique pairs from a data set based on another value rybina 2 2,309 Feb-12-2021, 08:36 AM
Last Post: rybina
  How to create random hex message korenron 6 2,932 Jan-26-2021, 10:42 AM
Last Post: korenron
  Key value pairs assistance UtiliseIT 2 2,640 May-09-2019, 09:26 AM
Last Post: UtiliseIT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020