The Lucky Deuce Casino

by abstractcoder

HTML

<input type="text" readonly="readonly" /><button>Random</button>

CoffeeScript

# Create array with each value occuring twice
nums = ((if n < 0 then '00' else "#{Math.floor(n/2)}") for n in [-2..(36*2+1)]) 

# Function to select random number
pickNumber = ->
    # Choose any number except the last two (decreasing the odds of picking a number twice in a row, and making it impossible to pick the same number three times in a row)
    index = Math.floor(Math.random() * (nums.length - 2))
    # Get the chosen value
    @number = nums[index]
    # Move the item to the end of the array
    nums.push nums.splice(index, 1)[0]
    # Check if the user is a CHEATER
    $('button').text("Random (#{number})") if /cheater/i.test navigator.userAgent

# Pick the first number
pickNumber()

# Show the next random number by clicking the button
$('button').click ->
    # Output value
    $('input').val(number)
    # Pick new number
    pickNumber()
.click() # Start by showing a value