Is it possible to get randomness in a smart contract? E.g. I would like the validator to randomly choose between two conditions to use to validate a transaction. Such a capability could be used for example to implement a lottery smart contract.

If so, what are the mechanics of the randomness?

If not, how could the lack of randomness be circumvented to implement something like a lottery smart contract?

1 Answer 1


Validation is deterministic, so decidedly non-random. And this is a desirable feature, because it means you can validate a transaction off-chain before ever submitting it, thus being sure you'll never have to pay fees for a transaction that will turn out to be invalid.

In order to get randomness, one could for example use an oracle that provides entropy.

Alternatively, one could use a commitment scheme: In your example, the lottery operator publishes the hash of the random draw up-front, then later, after players have signed on, reveals it. The operator can't cheat, because he has committed already (by publishing the hash up front).


Your Answer

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

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