API reference
We provide libraries in multiple languages for verifying and deriving random numbers from our network, as well as performing common gaming tasks for randomness that are easy to get wrong.
Installation​
- Rust
- Solidity
- Typescript
Make sure you have an up-to-date version of cargo, and add the following to your Cargo.toml
:
[dependencies]
randamu-rand = "0.0.1"
We recommend using forge to manage your dependencies. Then run:
forge install randa-mu/randomness-solidity
You can install the library by running:
npm install @randamu/randomness-js
It comes with Typescript types included.
Pick a random integer​
- Rust
- Solidity
- Typescript
use drand_sdk_rs::KeccakSdk;
// Fetch the random bytes from chain or somewhere else:
let randomness: Vec<u8> = .. ;
// Initialise the SDK using Keccak for derivation (for compatibility with EVM smart contracts).
// Other hash functions and XOF are available.
let mut rng = KeccakSdk::new().new_int_rng();
// Now we can get the same random number as every other SDK user would.
// The bitsize will depend on which generic parameter we pass
let random_int: u64 = rng.next_unsigned<u64>()?;
import { nextUint8 } from "../lib/RNGFromBytes.sol"
// We get our random bytes from a callback or somewhere else
bytes memory randomness = .. ;
// Now we get the same random number as every other SDK user
uint8 randomValue = nextUint8(randomness);
// Other bitsizes are support by similar methods
uint32 randomValue = nextUint32(randomness);
import { KeccakRng } from "@randamu/randomness
// Fetch the randomness from chain using ethers.js or similar
const randomness: Uint8Array = .. ;
// Initialize the SDK using Keccak for derivation (for compatibility with EVM smart contracts)
// Other hash functions and XOF are available.
const rng = new KeccakRng(randomness)
// Now we get the same random number as every other SDK user, conveniently turned into a javascript number
const randomInt: number = rng.nextUint8()
// Note: bigger bitsizes will have different return types
const randomBigInt: bigint = rng.nextUint64()