Share
Explore

icon picker
Randomized Match-Ups

This doc is based on Caesar_Sengupta’s , where he wants to generate a set of 1:1 meeting matchups within a list of people.

So basically:

Start with a list of names
Shuffle the list, so that we can randomly choose a 1:1 meeting partner for each person
Rules
People can’t meet with themselves
Everyone needs to be included, and no doubles (a fully random selection of people, calculated each row, would sometimes result in certain names appearing multiple times, and certain names not appearing at all)

Approach

We want to pull random pairs of names out of a hat until there are no more pairs left
Assign new random numbers to each Person each time a button is pressed (I made a to get around of the native Random() function)
Clear out the old Matchups, if any
Sort the list by the random number column
Grab pairs of people, and set them as each other’s match in the matchup column

Shuffle Matches
People
Person
Random Number
Last Modified
Matchup
1
Alex Trebek
0.8242
44647.9962
Bob Barker
2
Bob Barker
0.6961
44647.9962
Alex Trebek
3
Charlie Rose
0.4561
44647.9962
Gene Rayburn
4
Drew Carey
0.5466
44647.9962
Fred Rogers
5
Ellen DeGeneres
0.9675
44647.9962
6
Fred Rogers
0.5741
44647.9962
Drew Carey
7
Gene Rayburn
0.2089
44647.9962
Charlie Rose
There are no rows in this table

Button Code

Comments ( // bla bla bla ) don’t work in Coda, but I’m including them here to try to explain things. If you want to copy-paste the code, it might be better to right-click on the button.
RunActions(

// Loop through the People table, working on each row one at a time
People.FormulaMap(
RunActions(
// Generate a random number for the random number column
ModifyRows(
CurrentValue, // CurrentValue means the current Person in the People table
[Random Number],
// For the seed, we're using the last modified date of the row, so that
// we get a fresh result each time we press the button; and also the ID
// of the row, so that we get a unique result for each row
Randomizer::RandomFromSeed(
CurrentValue.[Last Modified].ToNumber() + CurrentValue.RowId()
)
),
// Clear out any old matchups from last time we pressed the button
ModifyRows(
CurrentValue,
Matchup,
""
)
)
),

// Now that there are random numbers there, loop through the table in random order
WithName(
People.Sort(true, People.[Random Number]),
SortedPeople, // Every time we say SortedPeople, we mean the randomly-sorted table
// This is a "for loop"
Sequence(1,SortedPeople.Count()).FormulaMap(
// We're pulling names from the hat in pairs; so on odd pulls (e.g. 1st or
// 5th pull), we'll pair them with the next pull (2nd or 6th). That means on
// even pulls (2nd or 6th), we'll pair them with the previous pull (1st or 5th)
If(
// CurrentValue is the number of times we've been through this loop (our pull#)
CurrentValue.IsEven(),
ModifyRows(
// E.g. on our 6th pull, edit the row for the 6th Person
SortedPeople.Nth(CurrentValue),
// Edit its Matchup column
Matchup,
// Set that Matchup column to the 5th Person (6 - 1)
SortedPeople.Nth(CurrentValue-1)
),
ModifyRows(
SortedPeople.Nth(CurrentValue),
Matchup,
SortedPeople.Nth(CurrentValue+1)
)
)
)
)
)

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.