contract RandomNumberConsumer is VRFConsumerBase, Ownable {
    using SafeMath for uint256;
    
    bytes32 internal keyHash;
    uint256 internal fee;
    
    bytes32 public currentRequestId;
    uint256 public randomResult;

    event RandomNumber(bytes32 requestId, uint256 randomResult, uint256[] numbers);
    
    /**
     * Constructor inherits VRFConsumerBase
     * 
     * Network: Ethereum Mainnet
     * Chainlink VRF Coordinator address: 0xf0d54349aDdcf704F77AE15b96510dEA15cb7952
     * LINK token address:                0x514910771AF9Ca656af840dff83E8264EcF986CA
     * Key Hash: 0xAA77729D3466CA35AE8D28B3BBAC7CC36A5031EFDC430821C02BC31A238AF445
     */
    constructor() 
        VRFConsumerBase(
            0xf0d54349aDdcf704F77AE15b96510dEA15cb7952, // VRF Coordinator
            0x514910771AF9Ca656af840dff83E8264EcF986CA  // LINK Token
        )
    {
        keyHash = 0xAA77729D3466CA35AE8D28B3BBAC7CC36A5031EFDC430821C02BC31A238AF445;
        fee = 2 * 10 ** 18; // 2 LINK (Varies by network)
    }

    function changeVRFParams(bytes32 _keyHash, uint256 _fee) external onlyOwner {
        keyHash = _keyHash;
        fee = _fee;
    }
    
    /** 
     * @dev Requests randomness 
     */
    function requestRandomNumber() external onlyOwner returns (bytes32 requestId) {
        require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
        return requestRandomness(keyHash, fee);
    }

    /**
     * @dev Function to get random numbers
     */
    function getNumbers(uint256 _limit, uint256 _count) external onlyOwner {
        uint256[] memory numbers = new uint256[](_count);

        for (uint256 i = 0; i < _count; i++) {
            numbers[i] = uint256(keccak256(abi.encode(randomResult, i))).mod(_limit);
        }

        emit RandomNumber(currentRequestId, randomResult, numbers);
    }

    /**
     * @dev Callback function used by VRF Coordinator
     */
    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        currentRequestId = requestId;
        randomResult = randomness;
    }

    /**
     * @dev Function to withdraw link token
     */
    function withdrawLink(uint256 _amount) external onlyOwner {
        LINK.transfer(msg.sender, _amount);
    }
}
function withdrawLink(uint256 _amount) external onlyOwner {
    LINK.transfer(msg.sender, _amount);
}

/**
 * @dev Function to withdraw link token
 */