daily pastebin goal
7%
SHARE
TWEET

set.js

a guest Jul 24th, 2013 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.shuffle =  function() {
  2.     var c = this.length, temp, i;
  3.     while (c > 0) {
  4.         i = Math.floor(Math.random() * c);
  5.         c--;
  6.         temp = this[c];
  7.         this[c] = this[i];
  8.         this[i] = temp;
  9.     }
  10.     return this;
  11. };
  12.  
  13. var cards = (function(n) {
  14.     var list = [];
  15.     for (var i = 0; i < n; i++) {
  16.         list.push([
  17.             ~~(i / 27) % 3,
  18.             ~~(i /  9) % 3,
  19.             ~~(i /  3) % 3,
  20.             ~~(i /  1) % 3
  21.         ]);
  22.     }
  23.     return list;
  24. }(81));
  25.  
  26. function isSet(a, b, c) {
  27.     var result = true;
  28.     for (var i = 0; i < 4; i++) {
  29.         if (!(a[i] === b[i] && b[i] === c[i]) &&
  30.             !(a[i] !== b[i] && a[i] !== c[i] && b[i] !== c[i])) {
  31.             result = false;
  32.             break;
  33.         }
  34.     }
  35.     return result;
  36. }
  37.  
  38. function hasSet(cards) {
  39.     var len = cards.length, result = false;
  40.     for (var i = 0; !result && i < len - 2; i++) {
  41.         for (var j = i + 1; !result && j < len - 1; j++) {
  42.             for (var k = j + 1; !result && k < len - 0; k++) {
  43.                 result = isSet(cards[i], cards[j], cards[k]);
  44.             }
  45.         }
  46.     }
  47.     return result;
  48. }
  49.  
  50. function monteCarlo(count, n) {
  51.     var contains = 0;
  52.     for (var i = 0; i < n; i++) {
  53.         contains += hasSet(cards.shuffle().slice(0, count)) ? 1 : 0;
  54.     }
  55.     return contains / n;
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top