Skip to content

Instantly share code, notes, and snippets.

@oppai
Created February 20, 2023 13:24
Show Gist options
  • Save oppai/55ff6159f4a132598a197d2c0cc2abde to your computer and use it in GitHub Desktop.
Save oppai/55ff6159f4a132598a197d2c0cc2abde to your computer and use it in GitHub Desktop.
function Deck() {
const deck = new Array<string>()
const suits = ["s", "h", "d", "c"]
suits.forEach( su => {
[2, 3, 4, 5, 6, 7, 8, 9, "T", "J", "Q", "K", "A"].forEach(num => {
deck.push(num+su)
})
});
return deck;
}
function getRank(card: string) : number {
switch(card[0]) {
case 'A':
return 1;
case '2':
return 2
case '3':
return 3
case '4':
return 4
case '5':
return 5
case '6':
return 6
case '7':
return 7
case '8':
return 8
case '9':
return 9
case 'T':
return 10
case 'J':
return 0
case 'Q':
return 0
case 'K':
return 0
default:
}
throw(`Invalid case getRank: ${card}`)
}
function Shuffle(items: any[]) {
const array = items;
for (var i = array.length; 1 < i; i--) {
const k = Math.floor(Math.random() * i);
[array[k], array[i - 1]] = [array[i - 1], array[k]];
}
return array;
}
const iteration = 1_000_000
const histgram = [...Array(50)].map(() => 0);
for (let i=0; i<iteration; i++) {
const deck = Shuffle(Deck());
const rank = deck
.slice(0, 5)
.map(c => getRank(c))
.reduce((acc, x)=> acc + x ,0)
histgram[rank] += 1;
}
histgram.forEach( (x, i) => {
console.log(`${i}, ${x}`)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment