Random Scheduling

cgreen

Active Member
Joined
Aug 14, 2002
Messages
293
I have 14 teams playing for 26 weeks. The teams play each other only twice in the 26-week frame. Is there a way to do a random schedule per week, then freeze (or do manual calculation) so that the schedule does not change for the 26 weeks unless I command it to? There will be 7 games per week, and again, each team can only play each other twice in the 26-week frame. The number listed first is understood to be where the game will be played, so it has to be random as well.

example(teams are identified as 1 thru 14):

Week One
2 and 13
3 and 4
11 and 6
5 and 14
1 and 7
8 and 12
10 and 9

Week Two
14 and 1
2 and 6
3 and 8
5 and 4
7 and 10
12 and 9
11 and 13
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Looking at this but, don't have a good solution yet.
This message was edited by Mark W. on 2002-08-27 14:24
 
Upvote 0
Hi,

I am right there with Mark on this one.

Here is one VBA solution, although there is a chance that a team can play more than twice in the same week.<pre>Sub test()
Dim x As Integer, y As Integer
Dim i As Integer, j As Integer, k As Integer, counter As Long
Dim m As Long, n As Long, p As Long

Dim fn As WorksheetFunction
Dim ScheduleArray
Dim Schedule As New Collection

Randomize

Set fn = Application.WorksheetFunction

x = 14
y = 2

m = fn.Permut(x, y) * y / x
n = x / y

ReDim ScheduleArray(1 To m, 1 To n) As String


For i = 1 To x
For j = 1 To x
If i<> j Then Schedule.Add i & " vs. " & j
Next j
Next i

For i = 1 To x
For j = 1 To x
If i<> j Then


If counter Mod 7 = 0 Then
k = k + 1
counter = 1
Else
counter = counter + 1
End If

p = Int(Rnd() * Schedule.Count) + 1
ScheduleArray(k, counter) = Schedule.Item(p)
Schedule.Remove (p)
End If
Next j
Next i

'Range("A1").Resize(m, n) = ScheduleArray
Range("A1").Resize(n, m) = fn.Transpose(ScheduleArray)
End Sub</pre>

Two ways to list the results are shown (one commented out of the procedure, but will probably be better for larger sets).

Still working on a better routine...

_________________
Bye,
Jay

EDIT:

A little better, but it still doesn't have any semblance of "separation" between games -- conceivably a team can play 7 games in one week.<pre>Sub test()
Dim x As Integer, y As Integer
Dim i As Integer, j As Integer, k As Integer, counter As Long
Dim m As Long, n As Long, p As Long, q As Long

Dim fn As WorksheetFunction
Dim ScheduleArray
Dim Schedule As New Collection

Randomize

Set fn = Application.WorksheetFunction

x = 14
y = 2

m = fn.Permut(x, y) * y / x
n = x / y

ReDim ScheduleArray(1 To m, 1 To n) As String


For i = 1 To x
For j = 1 To x
If i<> j Then Schedule.Add i & " vs. " & j
Next j
Next i

For q = 1 To m * n
If counter Mod 7 = 0 Then
k = k + 1
counter = 1
Else
counter = counter + 1
End If

p = Int(Rnd() * Schedule.Count) + 1
ScheduleArray(k, counter) = Schedule.Item(p)
Schedule.Remove (p)
Next q

'Range("A1").Resize(m, n) = ScheduleArray
Range("A1").Resize(n, m) = fn.Transpose(ScheduleArray)
End Sub</pre>
This message was edited by Jay Petrulis on 2002-08-27 14:58
 
Upvote 0
I created a "brute force" model, but I'm finding that there are just too many variables to satisfy simultaneously. I'm now trying to get individual weekly schedules to "fall out". Here's a peek at the 1st 18 of 183 rows in my model...
This message was edited by Mark W. on 2002-08-28 16:46
 
Upvote 0
Hi,

Latest post before I head home -- no output yet, and you might get caught in a loop -- hit Esc to break out.

Also, I have Excel 97 at work, so I had to use the Split97 UDF function. For those on 2000 or XP, Excel has a native Split function.

OK, more to come as this gets refined...

<pre>Sub test()
Dim x As Integer, y As Integer
Dim i As Integer, j As Integer, k As Integer, counter As Long
Dim m As Long, n As Long, p As Long, q As Long
Dim z As String

Dim UsedList

Dim fn As WorksheetFunction
Dim ScheduleArray
Dim Schedule As New Collection

Randomize

Set fn = Application.WorksheetFunction

x = 14
y = 2

m = (x - 1) * 2
n = x / y

ReDim ScheduleArray(1 To m, 1 To n, 1 To 2) As Integer
ReDim UsedList(1 To x)

For i = 1 To x
For j = 1 To x
If i <> j Then
z = i & "," & j
Schedule.Add Split97(z, ",")
End If
Next j
Next i

For q = 1 To m * n
If counter Mod 7 = 0 Then
k = k + 1
Application.StatusBar = k
counter = 1
Else
counter = counter + 1
End If

If counter = 1 Then
ReDim UsedList(1 To x)
p = Int(Rnd() * Schedule.Count) + 1
ScheduleArray(k, counter, 1) = CInt(Schedule.Item(p)(0))
ScheduleArray(k, counter, 2) = CInt(Schedule.Item(p)(1))
UsedList(1) = CInt(Schedule.Item(p)(0))
UsedList(2) = CInt(Schedule.Item(p)(1))
Else
ReSample:
p = Int(Rnd() * Schedule.Count) + 1
If Not IsError(Application.Match(CInt(Schedule.Item(p)(0)), UsedList, 0)) Then GoTo ReSample
If Not IsError(Application.Match(CInt(Schedule.Item(p)(1)), UsedList, 0)) Then GoTo ReSample

ScheduleArray(k, counter, 1) = CInt(Schedule.Item(p)(0))
ScheduleArray(k, counter, 2) = CInt(Schedule.Item(p)(1))
UsedList(2 * counter - 1) = CInt(Schedule.Item(p)(0))
UsedList(2 * counter) = CInt(Schedule.Item(p)(1))

Schedule.Remove (p)
End If
Next q

'Range("A1").Resize(m, n) = ScheduleArray
'Range("A1").Resize(n, m) = fn.Transpose(ScheduleArray)
Application.StatusBar = False
End Sub

Function Split97(sString As String, Optional sDelim As String = " ", _
Optional ByVal Limit As Long = -1, _
Optional Compare As Long = vbBinaryCompare) As Variant

''''''''''''''''''''''''''''
' Split97 mirrors the Split function introduced in XL2000
' Author Myrna Larson
' posted to microsoft.public.excel.programming 13 Nov 2001

Dim vOut As Variant, StrLen As Long
Dim DelimLen As Long, Lim As Long
Dim n As Long, p1 As Long, p2 As Long

StrLen = Len(sString)
DelimLen = Len(sDelim)
ReDim vOut(0 To 0)

If StrLen = 0 Or Limit = 0 Then
' return array with 1 element which is empty
ElseIf DelimLen = 0 Then
vOut(0) = sString ' return whole string in first array element
Else
Limit = Limit - 1 ' adjust from count to offset
n = -1
p1 = 1

Do While p1 <= StrLen
p2 = InStr(p1, sString, sDelim, Compare)
If p2 = 0 Then p2 = StrLen + 1
n = n + 1
If n > 0 Then ReDim Preserve vOut(0 To n)
If n = Limit Then
vOut(n) = Mid$(sString, p1) ' last element contains entire tail
Exit Do
Else
vOut(n) = Mid$(sString, p1, p2 - p1) ' extract this piece of string
End If
p1 = p2 + DelimLen ' advance start past delimiter
Loop
End If
Split97 = vOut
End Function</pre>
 
Upvote 0
Would it be reasonable to generate one half of the season, i.e. 13 fixtures and then just transpose the teams for the second half. So for example in week 1 team 2 is the home team against team 7, in week 14, team 7 is the home team against team 2.

I think Jay knows my preferred method of knocking up random selections like this. I've got a bit of work to do right now or else I would take the time to post a solution just now. (Which is what I will try to do after my work)
 
Upvote 0
You guys amaze me!

I'm not familer with visional basic, so will have to experiment with Jay’s solution. How do I make it work on Excel?

Mark, I think having the random pick in the first 13 weeks and then transposing the numbers for the next 13 weeks is a good idea. What would be the best way to do that?
 
Upvote 0
Hi,

This is difficult. My approach gets all the games and then scrambles them. However, I have yet to "solve" the puzzle a single time, as there inevitably comes a time when the only game I have left is not an available option.

For instance, suppose there are only 6 teams: A, B, C, D, E, F

Now suppose I have the following

Week 1
A/B C/D E/F

Week 2
A/C B/D F/E

Week 3
A/D B/C ?? F and E together are already eliminated from consideration.

How can this be overcome?
 
Upvote 0
Screw VBA, create a table with that is 14 rows X 14 columns. Number them 1 to 14. . i.e. team 1 v team 1, team 2 v team 2 etc etc. Create a random order for the 14 teams and down the column with 1 to 14. Use the same random order along the row at the top. In the diagonal from top left to bottom right, put X's to represent an invalid game. Use this matrix to determine the first 13 weeks of the season. Then se the same matrix and switch home team to away team. I still don't have time to knock up this example. :)

Your only challenge now is to create a random order for 14 numbers, using each number only 1 time. (sounds familiar : )
_________________<font color = green> Mark O'Brien
This message was edited by Mark O'Brien on 2002-08-27 22:58
 
Upvote 0
Yeah, I can, I hope this HTMLMaker works, nope can't get it:

<pre>
3 1 6 8 7 9 11 2 10 4 12 14 13 5
3 x Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
1 Week 1 x Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
6 Week 1 Week 2 x Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
8 Week 1 Week 2 Week 3 x Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
7 Week 1 Week 2 Week 3 Week 4 x Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
9 Week 1 Week 2 Week 3 Week 4 Week 5 x Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
11 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 x Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
2 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 x Week 8 Week 9 Week 10 Week 11 Week 12 Week 13
10 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 x Week 9 Week 10 Week 11 Week 12 Week 13
4 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 x Week 10 Week 11 Week 12 Week 13
12 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 x Week 11 Week 12 Week 13
14 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 x Week 12 Week 13
13 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 x Week 13
5 Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9 Week 10 Week 11 Week 12 Week 13 x
</pre>
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top