<p>Validation is deterministic, so decidedly <em>non-random</em>. 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.</p>
<p>In order to get randomness, one could for example use an oracle that provides entropy.</p>
<p>Alternatively, one could use a commitment scheme: In your example, the lottery operator publishes the <em>hash</em> 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).</p>

CC BY-SA 4.0
2021-05-07T19:24:56.980Z
2021-05-07T19:24:56.980Z
Lars Brünjes
<p>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.</p>
<p>If so, what are the mechanics of the randomness?</p>
<p>If not, how could the lack of randomness be circumvented to implement something like a lottery smart contract?</p>

<smart-contract><randomness>
Randomness in a smart contract
# Randomness in a smart contract

(Asked by: username_0 on 2021-05-07)

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?

**Tags:** smart-contract, randomness

---

## 1 answers

### Answer by username_1 on 2021-05-07. Score: 28 | Accepted answer

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).


---

