Pauan

Calculate Faeria card draw probabilities

May 19th, 2017
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://stackoverflow.com/a/6274398/449477
  2. function shuffle(array) {
  3.   let counter = array.length;
  4.  
  5.   // While there are elements in the array
  6.   while (counter > 0) {
  7.     // Pick a random index
  8.     let index = Math.floor(Math.random() * counter);
  9.  
  10.     // Decrease counter by 1
  11.     counter--;
  12.  
  13.     // And swap the last element with it
  14.     let temp = array[counter];
  15.     array[counter] = array[index];
  16.     array[index] = temp;
  17.   }
  18.  
  19.   return array;
  20. }
  21.  
  22. function calculateProb(x) {
  23.   var cards = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29];
  24.  
  25.   var length = cards.length - x.drawn;
  26.  
  27.   var success = 0;
  28.   var failure = 0;
  29.  
  30.   next: for (var i = 0; i < 70000000; ++i) {
  31.     shuffle(cards);
  32.  
  33.     var index = 0;
  34.  
  35.     for (var j = 0; j < x.needed.length; ++j) {
  36.       var need = x.needed[j];
  37.  
  38.       var drawn = 0;
  39.  
  40.       for (var h = 0; h < need.deck; ++h) {
  41.         if (cards.indexOf(index, length) !== -1) {
  42.           ++drawn;
  43.         }
  44.  
  45.         ++index;
  46.       }
  47.  
  48.       if (drawn < need.draw) {
  49.         ++failure;
  50.         continue next;
  51.       }
  52.     }
  53.  
  54.     ++success;
  55.   }
  56.  
  57.   return (success / (success + failure)) * 100;
  58. }
  59.  
  60.  
  61. console.log(calculateProb({
  62.   drawn: 19,
  63.   needed: [{
  64.     draw: 1,
  65.     deck: 3
  66.   }]
  67. }));
Advertisement
Add Comment
Please, Sign In to add comment