I needed a way to randomly order a list of people for my fantasy football league. This was pretty simple to do using RandomSample[], but the final ouput left something to be desired!
RandomSample[{"Jim", "Bob", "Dave"}, 3]
Out= {"Dave", "Bob", "Jim"}
So, wanting something that could be a little cleaner, I created a more robust function that actually creates the full draft list for
nrounds, assuming you wanta snake (rather than linear) draft order.
DraftOrder[players_, rounds_] := {
playerorder = RandomSample[players, Length[players]];
Do[
Print[Style[StringJoin["Round ", IntegerString[currentround]],
"Section"]];
Do[
prefacestring =
StringJoin["Draft pick number ", IntegerString[i], " goes to "];
finalstring =
StringJoin[prefacestring, ToString[playerorder[[i]]]];
Print[Style[finalstring, "Text"]], {i, 1, Length[players], 1}];
playerorder = Reverse[playerorder];
, {currentround, 1, rounds}]
}
[/i][/i]
The function can then be called with
DraftOrder[{"Bill", "Jim", "Dave", "Sam", "Adam", "Matt"}, 18]
If anyone wants to lend a helping hand, there was one sticky issue I couldn't work around. I wanted the rounds parameter to have a default value, so that if you only pass in a list of players it automatically gives you, say, 14 rounds. Using a function definition like
DraftOrder[players_, rounds_:1] :=
didn't work, and I couldn't seem to find a reason why.
Anyway, I hope someone besides me gets at least a little use out of this bit of code.