'Most efficient way to assign students to random and nonrepeating teams across multiple lab sessions?

I came across this question on social media:

Tasked with creating random teams of 4 out of 23 students to do labs every class. have to make sure even distribution of assignments and that teams are never the same. help?

At first glance, it seems similar to a typical CS question like "How many unique five-card hands of Poker can be dealt?", but in this case you need to ensure that ever 'card' is dealt each round, and that from round-to-round, no two hands are the same.

I tried to generalize the question: For a range of integers 1...N, dividing the range into sets of length P such that all integers 1...N are in a set, how many unique permutations of the range 1...N exist such that no sets repeat (contain the same integers in any order as any other set)?

I wrote some Python that solves the question, however it seems really inefficient. Is there a particular algorithm that addresses questions like these with reasonable time complexity?

Currently my approach is basically to have an outer loop for each lab session and an inner loop to populate the session with teams, using one list to track used team arrangements to avoid (sorted, joined, hashed lists of integers), one list to to track students remaining to be assigned to a team within a given session, and one list to track what teams have been developed for the current lab session, calling list(itertools.combinations()) repeatedly to eventually get all students assigned to some random team arrangement within the session, handling for when you can't neatly divide into teams:

Calculating roster for session 1
[(3, 6, 12, 19), (7, 11, 9, 8), (10, 17, 2, 13), (20, 1, 4, 16), (21, 18, 14, 22), (23, 5, 15)]
Calcuating roster for session 2
[(9, 17, 21, 23), (16, 1, 20, 8), (5, 10, 12, 14), (18, 4, 3, 2), (15, 7, 19, 13), (22, 11, 6)]
Calcuating roster for session 3
[(7, 12, 17, 18), (9, 4, 15, 10), (13, 8, 23, 14), (22, 11, 1, 6), (2, 21, 5, 16), (3, 20, 19)]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source