Search code examples
type-conversionbooleansoliditychainlinkuint

Convert uint value from chainlink VRF to boolean SOLIDITY


I am writing a coin toss contract using Chainlink VRF to request a random number. When using chainlink, the random number generated is a uint. I am trying to convert such uint into a boolean value to obtain whether 0 for heads or 1 for tails.

I have already managed to obtain the random number and transform it into 0 or 1 using modulo (%). But the number I get is still a uint. How can I convert it to a boolean value?

        struct round {
           uint roundId;
           uint betAmount;
           bool playerChoice; // 0 = heads; 1 = tails
           bool draw;
           bool win; 
        }
    
        function fulfillRandomWords(
            uint256, /* requestId */
            uint256[] memory randomWords
        ) internal override {

// this where I get the error "TypeError: Type uint256 is not implicitly convertible to expected type bool."
            myRound.draw = (randomWords[0] % 2); 
        }

Thanks for the help guys!


Solution

  • You can compare the resulting value with 0 or 1.

    // comparison returns a bool
    myRound.draw = (randomWords[0] % 2) == 1;