开发者

Selecting Chess player from a pool - Algorithm

开发者 https://www.devze.com 2023-03-06 14:59 出处:网络
Hii, i have this problem There are n[even] number of players who want to play chess How can i get randomize player and opponents ?[every player will get only one chance]

Hii, i have this problem

There are n[even] number of players who want to play chess

How can i get randomize player and opponents ? [every player will get only one chance]

Think there are 6开发者_StackOverflow player - p1,p2,p3,p4,p5,p6

I want to do a code which will do such routine for me

p1 vs p5

p2 vs p6

p3 vs p4


You can assign a unique random number to every player, sort the player list using that number and select pairs 1-2, 3-4, 5-6 and so on. Should be very fast because of built-in sorting that almost every language has now.


Do all combinations and shuffle them Fisher-Yates


store players in array a of size n
let p be a pair(w, b)
let l be a list of pairs
while n > 0
    let x be a random number between 0 and n
    add player at index x in a to p as w
    remove index x from a
    n--
    let y be a random number between 0 and n
    add player at index y in a to p as b
    remove index y from a
    n--
    add p to l
endwhile
0

精彩评论

暂无评论...
验证码 换一张
取 消