Selecting Chess player from a pool - Algorithm
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 p4You 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
精彩评论