Hello I'm well aware there are plenty of threads around discussing different methods. Just curious and wanted to ask directly here because feel like the situation might be different. Essentially I'm trying make it so whenever someone calls out funds from the contract the amount they get varies from 0 to 100, but obviously my current method is flawed depending on now.

I've been reading into ChainLink VRF but looks like the fees could be a bit annoying. Just curious if there's any other methods anyone knows of, otherwise my other option was to lock the balance if it goes close to current block reward and users will just have to cash out more often then. It's doubtful many users will even get a balance above block reward, but I'd like to explore my options.

  • Just to clarify when I say lock the balance I mean for each user, not of the whole contract.
    – Alienbyte
    Jul 13, 2020 at 21:53
  • You don't have to rely only on now as a "seed" for randomness. You can also rely on msg.sender. In other words, you can use something like abi.encodePacked(now, msg.sender). Jul 14, 2020 at 8:05
  • I already have already tried that sadly a malicious user could create another contract to get the same number outcome with their address I believe? My thought was they could keep doing this calculation until they got the desired number and wait to call during that time.
    – Alienbyte
    Jul 14, 2020 at 23:05

2 Answers 2


If you generate a random number in solidity, you're going to need to look offchain at some point, otherwise the miners could manipulate any type of random number generator using the blockhash.

A naive approach to get a random number in an application where you're OK with it being 'hackable':

function random() private view returns(uint){
    return uint(keccak256(abi.encodePacked(block.difficulty, now, number)));

Otherwise you're going to want to use a Chainlink VRF. Here is an article on the topic if you'd like to learn more.

  • 1
    Thinking about using Rhombus's Network oracle but trying to get more info, thank you!
    – Alienbyte
    Jul 21, 2020 at 5:14

If Chainlink fees are too much, you can always also create your own random beacon that posts random numbers to a blockchain. Then run this at $5 DigitalOcean node and some ETH on the hot wallet to post numbers frequently enough for your use case.

  • How would this number be input to the blockchain / contract? Seems like a better option for sure but guessing some users wouldn't prefer it. Thank you!
    – Alienbyte
    Jul 14, 2020 at 7:26
  • The easiet way is that the server has a timer job that writes a new random number to your smart contract every day / week / month. Also you do not know if users care until you ask :) Jul 14, 2020 at 7:29
  • True :) thanks for your help! When building dapps just like to stick to full decentralization if possible too so maybe more personal.
    – Alienbyte
    Jul 14, 2020 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.