Generating Randomness

The StarkLinkRandomness contract, also known as the StarkLink Randomness Oracle, is the easiest and most convenient way to generate random numbers on StarkNet.
Addresses to the StarkLinkRandomness contract can be found in our Deployed Contracts page.
The StarkLinkRandomness contract handles requests for randomness from any number of independent applications running on StarkNet.
To use StarkLinkRandomness you need to follow two steps:
  1. 1.
    Declare the StarkLinkRandomness interface in your smart contract. Line 10-14 in the basic code example
@contract_interface
namespace IStarkLinkRandomness {
func request_randomness(beacon_address: felt) -> (requestId: felt) {
}
}
2. Call the request_rng method of the StarkLinkRandomess contract. Line 40-42 in the basic example
let (request_id) = IStarkLinkRandomness.request_rng(
contract_address=oracle, beacon_address=_beacon_address
);
3. Implement a valid will_recieve_rng function for the StarkLinkRandomness contract to call once the randomness has been generated. Line 46-58 in the basic example
@external
func will_recieve_rng{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
rng: BigInt3, request_id: felt
) {
let (oracle) = oracle_address.read();
let (caller_address) = get_caller_address();
assert oracle = caller_address;
// # Do something with RNG
return ();
}
`