I am building a vending machine where the users have to first send ADA to some address then my backend selects NFTs and sends back to the user who payed. I have been wondering if is there a way to do it using smart contracts?

The NFTs are all locked in a contract and we make the tx to the contract from the front end to unlock them. But my problem is: How could I make that the NFTs are randomly seleceted?

The front end might randomly select some. But when the Browser Wallet ask for sign the transaction it will show which NFTs are selected and then the user might decline or accept the transaction depending of the NFTs.

The main problem is that NFT must be randomly selected. How can I solve this problem?

1 Answer 1


Lars already gave a pretty good answer to the randomness problem here. Basically, an oracle is probably the easiest and best way to achieve randomness with entropy.

If you want to avoid an oracle, you might be able to seed your randomness with the transaction ID. There could be some concerns with reverse engineering there, but depending on the scale of your project those concerns might be unrealistic.

  • Supouse the oracle gives the random number (in some sense not specified), then? what should i do ? what transactions should i do and how? Feb 8 at 1:36
  • There’s a chance I misunderstood you, but my assumption here is that your third paragraph constitutes an issue you want to avoid, not a feature you want to include. If that’s the case, I think you only need 2 transactions. The first would be a List transaction which allows you to place an NFT you want to sell in a UTXO at the vending machine's script address. The second would be a Buy transaction, which allows anyone to pay an amount to get NFTs from the machine. Here you use your oracle to decide which NFT UTXOs to consume, sending the NFTs they contain back to the buyer. Feb 10 at 8:54
  • Yes I think two transactions are needed Feb 10 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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