Skip to content

Instantly share code, notes, and snippets.

Tim Huff timhuff

Block or report user

Report or block timhuff

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@timhuff
timhuff / keno.js
Created Dec 8, 2019
Keno Monte Carlo
View keno.js
/*
Output:
Pick 1 winnings per game: -0.499308
Pick 2 winnings per game: -0.39711
Pick 3 winnings per game: -0.377799
Pick 4 winnings per game: -0.387787
Pick 5 winnings per game: -0.381384
Pick 6 winnings per game: -0.430624
Pick 7 winnings per game: -0.440436
Pick 8 winnings per game: -0.407179
View scrambles.json
[
"L2 F2 R2 D' L2 F2 U' L2 D' L2 U B D B2 D R U' F' L B' F R",
"L2 D2 B2 U R2 F2 L2 D2 B2 D B2 F U' L U2 B2 D' L F2 L U2",
"D F2 U' F2 R2 B2 L2 U' B2 R2 U' L2 R D' B U' F U' B' L R U",
"D2 B2 U' B2 U' R2 D F2 U2 L2 F2 D' R' U' R2 D' B2 F' U L2 D2 F'",
"B2 U F2 D F2 D2 F2 D B2 L2 B2 R D2 F U' L' F' D' L F R F2",
"D' B2 R2 F2 U' R2 D' B2 U2 F2 D' U2 F' U B' R' F L' B2 R2 U' F",
"L2 B2 U F2 U' B2 F2 U2 B2 L2 U B2 F' U' L' B' F' L' U' B2 L2 U2",
"B2 D2 L2 F2 D2 L2 U B2 F2 U2 B' R' B' U F' R B' F2 U F2 D2 R",
"R2 D2 L2 U B2 D' B2 D' L2 U L2 U' B R' D' R' D' R2 D2 U2 B R'",
View randomMocks.test.js
const crypto = require('crypto')
describe('Predictable Randomness', () => {
describe('Math.random', () => {
const firstResult = 0.37454011430963874
const secondResult = 0.7965429842006415
beforeEach(() => Math.__clearChances__())
it('should produce these two numbers first by default', () => {
expect(Math.random()).toEqual(firstResult)
expect(Math.random()).toEqual(secondResult)
View pi.pyth
A.Q@c6csm?-i+OG1+1OG1Z1HH2
Two commands:
A .Q - Parse the input (.Q), assign 1st line (max dice number) to G and 2nd line (number of trials) to H
@c6csm?-i+OG1+1OG1Z1HH2 - The output
The output:
@c6csm?-i+OG1+1OG1Z1HH2
@ c6csm?-i+OG1+1OG1Z1HH 2 - Take the square root of the middle chunk
c 6 csm?-i+OG1+1OG1Z1HH - Divide 6 by the 3rd chunk
View interpolation.coffee
numBars = 73
bars = [0..numBars-1].map -> 0
getDisplayBars = (audioData)->
deltaT = audioData.length/numBars
bars[0] = audioData[0]
for barNum in [1..numBars-1]
timeIndex = barNum*deltaT
leftIndex = Math.floor timeIndex
View golf.js
y = function(b){
nums = []
for(i=0; i<=Math.floor(Math.log(b)/Math.log(2)); i++)
nums.push(i);
d = nums.reduce(function(n, a){1<<n&b?3+a:4+a},0)
if(b == d)
return b
else
return y(b)
}
View explanation.pyth
L?-b=dsm?.&.<1db3 4hlbydb@mXdH1mydSQ0
L create lambda function named y with param b
? ternary if
- minus used for expressing "if d != b"
b lambda param
= assign
d d =
s sum
m map
View fixed_explain.coffee
# set l to Math.log because you use it twice
l = Math.log
# create a recursive function, r, to iterate on a chain until it reaches a fixed point
# n is the input for the current iteration
r = (n)->
###
This next line is a mouthful. "l(n)/l 2" computes the log base 2 of n. This is the index of the highest set bit in n
The reduce function is basically a sum across the bits of the number.
It adds 3 to the sum if the bit is set ("one".length) and 4 if the bit is not set ("zero".length)
So, in a nutshell, z is set to the number of letters in the binary phrase for the number n
View fixed.coffee
l=Math.log;r=((n)->z=[0..l(n)/l 2].reduce(((a,b)->`1<<b&n?3+a:4+a`),0);`z==n?n:r(z)`);c={};[1..1000000].map((n)->c[r n]?=0;c[r n]++);c
View gist:ca1366cb9b954d1b48d9
prefixString = "* + 2 3 4"
evaluatePrefix = (prefix)->
operator = prefix.shift()
leftValue = if isNaN parseFloat prefix[0] then evaluatePrefix prefix else prefix.shift()
rightValue = if isNaN parseFloat prefix[0] then evaluatePrefix prefix else prefix.shift()
eval leftValue+operator+rightValue
prefix = prefixString.split ' '
console.log evaluatePrefix prefix
You can’t perform that action at this time.