r*d
3 楼
不知道标准的shuffle card是什么算法
我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
面一半和后面一般间隔混起来?
void fun()
{
.....
for ( int i =1; i {
card_after[i]=card_before[i];
card_after[i+1]=card_before[i+n/2];
}
}
我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
面一半和后面一般间隔混起来?
void fun()
{
.....
for ( int i =1; i
card_after[i]=card_before[i];
card_after[i+1]=card_before[i+n/2];
}
}
I*s
4 楼
http://en.wikipedia.org/wiki/Shuffling
"Shuffling algorithms
In a computer, shuffling is equivalent to generating a random permutation of
the cards. There are two basic
algorithms for doing this, both popularized by Donald Knuth.
The first is simply to assign a random number to each card, and then to sort
the cards in order of their random
numbers. This will generate a random permutation, unless any of the random
numbers generated are the
same as any others (i.e. pairs, triplets etc). This can be eliminated either
assigning new random numbers to
these cases, or reduced to an arbitrarily low probability by choosing a
sufficiently wide range of random
number choices. If using efficient sorting such as mergesort or heapsort,
this is an O(n log n) algorithm.
The second, generally known as the Knuth shuffle or Fisher–Yates shuffle,
is a linear-time algorithm which
involves moving through the pack from top to bottom, swapping each card in
turn with another card from a
random position in the part of the pack that has not yet been passed through
(including itself). Providing that
the random numbers are unbiased, this will always generate a random
permutation."
【在 w*****3 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: java
: 不准用
: math.random()
"Shuffling algorithms
In a computer, shuffling is equivalent to generating a random permutation of
the cards. There are two basic
algorithms for doing this, both popularized by Donald Knuth.
The first is simply to assign a random number to each card, and then to sort
the cards in order of their random
numbers. This will generate a random permutation, unless any of the random
numbers generated are the
same as any others (i.e. pairs, triplets etc). This can be eliminated either
assigning new random numbers to
these cases, or reduced to an arbitrarily low probability by choosing a
sufficiently wide range of random
number choices. If using efficient sorting such as mergesort or heapsort,
this is an O(n log n) algorithm.
The second, generally known as the Knuth shuffle or Fisher–Yates shuffle,
is a linear-time algorithm which
involves moving through the pack from top to bottom, swapping each card in
turn with another card from a
random position in the part of the pack that has not yet been passed through
(including itself). Providing that
the random numbers are unbiased, this will always generate a random
permutation."
【在 w*****3 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: java
: 不准用
: math.random()
w*3
5 楼
这题是MS的onsite题,
我觉得random是必须的吧,
难道这题是压力测试?
我觉得random是必须的吧,
难道这题是压力测试?
E*a
6 楼
you are talking about perfect shuffling, like A1A2A3B1B2B3 A1B1A2B2A3B3
I guess lz talked about random shuffling
【在 r******d 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 不知道标准的shuffle card是什么算法
: 我每次洗牌都把牌分成两半, 然后左边一张, 右边一张地把牌混起来。
: 那用一个array放入所有的牌, 反复call一个洗牌的函数。 在那个函数里面把牌的前
: 面一半和后面一般间隔混起来?
: void fun()
: {
: .....
: for ( int i =1; i
: card_after[i]=card_before[i];
相关阅读
有经验IT找支持New H1B的工作猎头的职位薪水如何?Yelp offer求教,附面经salesforce怎么样?老板连续4年,每年开一个人,全是老中Bloomberg职位问题 G家以前过了HC的一年内再申请还需要重新面试吗?签了pip, 会影响下个工作的background check吗?【ms面试题】用不均匀的骰子掷出均匀的点数intern vs full time又被据了请教 C++ exception 面试问题FLAG 公司前景求教一道题目公司查上班时个人上网记录和邮件这个hr是啥意思?onsite可能默剧吗?小女请教下 如果leetcode只刷了100题 可以找工作吗?急求google 硬件工程师hardware engineering 内推google对gpa有要求么,3.5以下的还有必要试么?