javascript - Combination of n numbers into two unique groups -


i have implement combination game players randomly selected 2 group n players in javascript.

for example:

if there 4 players - b c d

ab vs cd - first round team ac vs bd - second round team ad vs bc - third round team

if there 6 players - b c d e f

abc vs def ade vs bcf

each time player playing unique player in such way no 2 players can play game same players 3 times in row. there way generate such kind of combination.

thank answer.

its easy archiv can make function pass in 4 or 6 names argument or better array random mix array

the de-facto unbiased shuffle algorithm fisher-yates (aka knuth) shuffle.

see https://github.com/coolaj86/knuth-shuffle

you can see great visualization here (and original post linked this)

function shuffle(array) {   var currentindex = array.length     , temporaryvalue     , randomindex     ;    // while there remain elements shuffle...   while (0 !== currentindex) {      // pick remaining element...     randomindex = math.floor(math.random() * currentindex);     currentindex -= 1;      // , swap current element.     temporaryvalue = array[currentindex];     array[currentindex] = array[randomindex];     array[randomindex] = temporaryvalue;   }    return array; } 

used so

var arr = [2, 11, 37, 42]; shuffle(arr); console.log(arr); 

some more info about algorithm used.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -