.Random
Introduction
.Random is a solution designed to provide a secure and reliable source of randomness for the Radix ledger. Whether you're developing raffles, minting NFTs, gaming applications, or any other components on Radix, our randomness service ensures fairness and unpredictability.
The Problem
There is no way to produce any random outcome on the Radix ledger. The current time cannot be used as a source of randomness because available time precision (up to a second) is insufficient. The transaction hash is random enough, but the client generates it, so the User can "mine" any desired TX hash. Bringing something random from outside the ledger and passing it to your Component is possible; however, having a Transaction Manifest and previewing transactions in the Wallet means that a user can reject submitting the transaction if the projected outcome is not good enough for them.
The Solution
The only way to solve this is to bring something random from outside the ledger onto the ledger in a separate transaction. This way, we can guarantee that the User doesn't know the result until they have committed the transaction, and there is no going back.
How it Works
To make it work, we have developed a framework that consists of three parts:
RandomComponent- an on-ledger component that allows you to request randomness.Watcher- an off-ledger service that monitors all invocations ofRandomComponentand brings the random bytes to the ledger.Random- a small 1Kb library that you can include in your component to conveniently convert random bytes into any number of pseudo-random outcomes.

Examples
You can find examples on GitHub.
Authentication
Ideally, you would like to allow only RandomComponent to call the callback method on YourComponent, preventing anyone else from misusing it. You can choose of the two ways to ensure this:
Grant access for each particular callback transaction to
RandomComponent(recommended). You pass a bucket with some token torequest_random(). Then you can verify that the same bucket is returned back to you in the callback. See an example in bucket_transfer_auth.rs.Allow
RandomComponentto call the method on YourComponent freely. You protect the callback method withmethod_auth, requiring our special badge (find the resource ID here). See an example in badge_auth.rs.
Testing
You can test with TestRunner (see examples above) or TestEnv. RESim is not supported at the moment.
Service Fee
Every time request_random() is invoked, we charge a $0.06 royalty fee (1 XRD based on current network settings)
Transaction Fees
When your callback method is executed in the second transaction, someone has to pay the transaction fees. There are two options:
You pay for the transaction, by executing
lock_feeinside your callback method, even if the transaction fails (remember that the methods should be authenticated).You do nothing and the transaction cost will be put on users.
request_random()invocations will require additional royalties to pay for the future transaction, based on the average total transaction costs incurred when executing your callback.
Last updated