contract Random is SafeMath {
    // Generates a random number from 1 to max based on the last block hash.
    function getRandomFromBlockHash(uint blockNumber, uint max)
    public
    constant 
    returns(uint) {
        // block.blockhash(uint blockNumber)
        //    returns
        //    (bytes32):
        //        hash of the given block
        // !! only works for 256 most recent blocks excluding current !!
        return(add(uint(sha3(block.blockhash(blockNumber))) % max, 1));
    }
}
/**
 * Random number generator from mined block hash.
 */
function getRandomFromBlockHash(uint blockNumber, uint max)
public
constant 
returns(uint) {
    // block.blockhash(uint blockNumber)
    //    returns
    //    (bytes32):
    //        hash of the given block
    // !! only works for 256 most recent blocks excluding current !!
    return(add(uint(sha3(block.blockhash(blockNumber))) % max, 1));
}

// Generates a random number from 1 to max based on the last block hash.