Reputation: 567
I've got a combinations question. I have designed this "Secret Santa" app that lets users join gift-groups in which they exchange gifts for a set dollar amount. When a user logs in, he can see all the groups he is in, along with the members in each group. Now, I want to design the groups so that each group randomly pairs up people for gift giving. I want to the algorithm to work so that I'm guaranteed that everyone in the group both gets assigned a person to give gifts to, and a person to receive gifts from. So for example, if a group has three members, [A, B, C]
, I want A->B, B->C, C->A
.
Any ideas how that could work? It doesn't matter if it will use just the one, or two arrays. Any help or suggestions is appreciated!
Upvotes: 0
Views: 586
Reputation: 2495
Lets n
- number of persons in a group
So just make pairs of {persons[i], persons[(i+1)%n]}
for all persons
Upvotes: 1