How can we generate random numbers in Scrypto if floating point libraries are not allowed be used? I want to be able to generate unique IDs for NFTs.
How can we generate random values in Scrypto?
151 Views Asked by TalesOfBeem At
2
There are 2 best solutions below
1
On
You can generate a pseudo random NFT id using the built-in NonFungibleId::random() method.
let new_nft_id: NonFungibleId = NonFungibleId::random();
Reference: https://radixdlt.github.io/radixdlt-scrypto/scrypto/resource/struct.NonFungibleId.html
There are 2 ways to solve this:
vec.len() + 1as the generated ID, making things more trivial.Runtime::generate_uuidwhich is a generated number format in Uuid which should guarantee uniquenessWe can also generate values given a max range: