Create a "random even distribution" over multiple "sets"

391 Views Asked by At

I'm a math layman (but an ok programmer). Sorry if my wording raises eyebrows, please correct it if you feel the need.

I want to host a racing event with 2 to 30 drivers (num_drivers). The event has 2 to about 12 races (num_races), and I want each driver to have a fair chance according to their position at the start using somewhat random positioning. The positioning should result in as many drivers as possible to have very different positions. So if a race consists of 11 drivers, I really want to avoid that one driver always starts from the middle of the starting grid, or one driver always starts alternating from first and last position.

This is for a hobby project and will be implemented in Python, but this is not really important. I'm mostly looking for a good algorithm.

The problem is, which starting positions do I assign to each driver for each race?

An example: for an event with num_races=3 and num_drivers=4 (named "A" to "D") a more or less good setup would be

Race 1: A B C D
Race 2: C D B A
Race 3: D B A C

Only player B starts from around the middle all the time, but I guess, that's fine in this case.

My current approach would be: The pole position would have a value of 1, the second position 2, and so on. So this setup gives quite equal values to each driver:

A: 1+4+3 = 8
B: 2+3+2 = 7
C: 3+1+4 = 8
D: 4+2+1 = 7

In the end the sum of positions of each driver should ideally be the same as for every other driver, and the individual positions should vary as much as possible for each driver. What would be a good generic algorithm (maybe even in pseudo code) for my problem when the number of drivers and number of races can vary? Is there even already an algorithm somewhere to look at?