<blockquote>
<p>Is it possible to generate a random number in Plutus on-chain code?</p>
</blockquote>
<p>No. On-chain code is deterministic, so there is no way to generate a number on-chian that someone off-chain couldn't predict.</p>
<p>The way around this would be to use an RNG oracle. Essentially outsourcing the generation of the random number to some third party, off-chain. You can then use that oracle's (or multiple oracles') data as an input for any games that needs random values.</p>

CC BY-SA 4.0
2021-08-13T14:27:23.737Z
Thats what I thought. Rather than using an oracle, would it be possible to simulate a RNG by somehow combining data available in UTXOs? If we would require the input transactions to have random datums and then combine them for all UTXOs would that be sufficiently random?
Piotr Gorzelany
CC BY-SA 4.0
2022-11-30T21:28:17.250Z
That would create a sort of lottery, where people would be looking for UTxOs that meet a certain requirement, so not exactly an RNG.
Mitchell Turner
CC BY-SA 4.0
2021-08-12T17:12:43.443Z
2021-08-12T17:12:43.443Z
Mitchell Turner
<p>Is it possible to generate a random number in Plutus on-chain code? For example, if I would like to implement a smart contract that works like gambling game, where a person deposits ADA and bets on a random outcome and then the contract generates that random outcome to determine if the player won?</p>

<plutus><plutus-pioneer-program>
Generate random number in Plutus on-chain code
# Generate random number in Plutus on-chain code

(Asked by: username_0 on 2021-08-12)

Is it possible to generate a random number in Plutus on-chain code? For example, if I would like to implement a smart contract that works like gambling game, where a person deposits ADA and bets on a random outcome and then the contract generates that random outcome to determine if the player won?

**Tags:** plutus, plutus-pioneer-program

---

## 1 answers

### Answer by username_1 on 2021-08-12. Score: 3 | Accepted answer

> Is it possible to generate a random number in Plutus on-chain code?

No. On-chain code is deterministic, so there is no way to generate a number on-chian that someone off-chain couldn't predict.

The way around this would be to use an RNG oracle. Essentially outsourcing the generation of the random number to some third party, off-chain. You can then use that oracle's (or multiple oracles') data as an input for any games that needs random values.

> **Comments:**
> **username_0**: Thats what I thought. Rather than using an oracle, would it be possible to simulate a RNG by somehow combining data available in UTXOs? If we would require the input transactions to have random datums and then combine them for all UTXOs would that be sufficiently random?  
> **username_1**: That would create a sort of lottery, where people would be looking for UTxOs that meet a certain requirement, so not exactly an RNG.  

---

