我的朋友邀请我回家玩秘密圣诞老人的游戏,我们应该在那里画很多画和画画。为小组中的朋友扮演“圣诞老人”的角色。
所以,我们写下所有的名字并随机选择一个名字。如果我们中的任何一个人最终选择了自己的名字,那么我们就会重新洗牌并重新选择名字(理由是一个人不能成为自己的圣诞老人)。
我们有七个人在玩,所以我认为最终的“圣诞老人分配”是 (1:7) 自身的排列,有一些限制。
我想就如何使用 Mathematica 或任何编程语言甚至算法来提出各种想法:
- 列出/打印所有“有效”圣诞老人分配
- 随着玩“秘密圣诞老人”的朋友数量的增长,可扩展
My friends invited me home to play the game of Secret Santa, where we are supposed to draw a lot & play the role of 'Santa' for a friend in the group.
So, we write all our names and pick a name randomly. If any of us ends up having their own name picked, then we reshuffle and pick names all over again (the rationale being that one can not be one's own Santa).
There are seven of us while playing so I thought of the final 'Santa-allocation' as a permutation of (1:7) onto itself, with some restrictions.
I would like to invite various ideas about how we could use Mathematica in particular or any programming language or even an algorithm to:
- List/print out ALL the 'valid' Santa-allocations
- Is scalable as the number of friends playing 'Secret Santa' grows
发布评论
评论(6)
你要找的东西叫做混乱(另一个可爱的拉丁词,比如放血和出窗外)。
所有排列中属于混乱的排列比例接近 1/e = 大约 36.8%——因此,如果您生成随机排列,只需继续生成它们,您就很有可能在 5 或 10 个选项中找到一个。随机排列。 (在 5 个随机排列中找不到一个的几率为 10.1%,每增加 5 个排列,未发现混乱的几率就会降低 10 倍)
这个演示非常实际,给出了直接生成混乱的递归算法,而不是而不是必须拒绝不是混乱的排列。
What you're looking for is called a derangement (another lovely Latinate word to know, like exsanguination and defenestration).
The fraction of all permutations which are derangements approaches 1/e = approx 36.8% -- so if you are generating random permutations, just keep generating them, and there's a very high probability that you'll find one within 5 or 10 selections of a random permutation. (10.1% chance of not finding one within 5 random permutations, every additional 5 permutations lowers the chance of not finding a derangement by another factor of 10)
This presentation is pretty down-to-earth and gives a recursive algorithm for generating derangements directly, rather than having to reject permutations that aren't derangements.
我建议:
这比 Heike 的函数快得多。
忽略代码的透明度,这仍然可以快几倍:
I propose this:
This is significantly faster than Heike's function.
Ignoring transparency of code, this can be made several times faster still:
没有元素映射到自身的排列是混乱。随着 n 的增加,紊乱的比例接近常数 1/e。因此,如果随机选择排列,则(平均)需要 e 尝试获得混乱。
维基百科文章包含用于计算小 n 的显式值的表达式。
A permutation that maps no element to itself is a derangement. As n increases, the fraction of derangements approaches the constant 1/e. As such, it takes (on average) e tries to get a derangement, if picking a permutation at random.
The wikipedia article includes expressions for calculating explicit values for small n.
在 Mathematica 中,您可以执行类似操作
,其中
n
是池中的人数。然后例如secretSanta[4]
返回Edit
看起来 Mathematica 中的
Combinatorica
包实际上有一个Derangements
函数,所以你也可以做类似的事情,尽管在我的系统上
Derangements[Range[n]]
比上面的函数慢大约 2 倍。In Mathematica you could do something like
where
n
is the number of people in the pool. Then for examplesecretSanta[4]
returnsEdit
It looks like the
Combinatorica
package in Mathematica actually has aDerangements
function, so you could also do something likealthough on my system
Derangements[Range[n]]
is about a factor 2 slower than the function above.这并没有回答你关于计算有效混乱的问题,但它提供了一种算法来生成具有以下属性的混乱(这可能是你想要的):
这里的算法是:
This does not answer your question about counting the valid derangements, but it gives an algorithm to generate one (which might be what you want) with the following properties:
Here the algorithm:
我在文档中发现了内置的 Subfactorial 函数,并更改了其中一个示例以生成:
可以使用 Subfactorial 来检查该函数。
正如 Mr.Wizard 的回答一样,我怀疑可以通过 SparseArray 优化
teleSecretSanta
。不过,我现在喝得太醉了,无法尝试这种恶作剧。 (开玩笑……我其实是太懒太笨了。)I came across the built-in
Subfactorial
function in the documentation and altered one of the examples to produce:One can use
Subfactorial
to check the function.As in Mr.Wizard's answer, I suspect
teleSecretSanta
can be optimized via SparseArray. However, I'm too drunk at the moment to attempt such shenanigans. (kidding... I'm actually too lazy and stupid.)