7

Rand lib is supported in rust but not supported in anchor lang. I want to generate a random number lets say between a range of 0-10. Is there is a way I can do in anchor lang. Or alternatively, if there is a way I could use candy machine random NFT functionality with url as I want to use random numbers to be passed in url.

Code snippet:

 let random_number = 0; // this is supposed to be a random number let say b/w [0-10]
 let path = "http://abc.com";
 let new_uri = format!("{}{}{}", path, random_number, ".json");

            invoke(
                &create_metadata_accounts_v2(
                    ctx.accounts.token_metadata_program.key(),
                    ctx.accounts.metadata.key(),
                    ctx.accounts.mint.key(),
                    ctx.accounts.mint_authority.key(),
                    ctx.accounts.payer.key(),
                    ctx.accounts.payer.key(),
                    num_title,
                    symbol,
                    new_uri,
                    Some(creator),
                    1,
                    true,
                    false,
                    None,
                    None,
                ),
                account_info.as_slice(),
            )?;
3
  • I am pretty sure that you can can use rust functionality on anchor lang. have you tried implementing rand in you code? Commented Nov 11, 2022 at 6:22
  • yes, I did that it is supported in rust but when using anchor lang it is not supported as the randomness of a function can be guessed which can be a security issue as well as you can run out of compute units if the random range is too large. Commented Nov 11, 2022 at 10:31
  • duplicate question stackoverflow.com/questions/70693497/…
    – Setmax
    Commented Nov 12, 2022 at 8:06

2 Answers 2

4

As defined here you can’t use random either in Rust or in Anchor if you want to generate a random number either you create your own number or you can use an oracle like a switchboard for generating a random value.

  • If you write your own random function using recent blockhash or timestamps available in the program it's most likely to be exploited.
  • So it's better to use an oracle for generating the random number. Verifiable Randomness On-chain

References

1
  • We can use random numbers in simple Rust without anchor lang using rand lib. Commented Nov 13, 2022 at 4:13
3

Found a way that we can simply use timestamps for this.

   let clock = Clock::get()?;
   let clock = clock.unix_timestamp % 10; // any number between range 0-10 

Just make sure the random number range is not too large else you may run out of compute units. In case of a very large range of numbers, you can make this implementation on the admin panel where only a transaction signed by admin wallet can call this function (if security is important).

2
  • This approach can be exploited. Commented Nov 14, 2022 at 7:04
  • Can you explain how? I was going to use this option for something monetary.
    – ZeroNine
    Commented Jan 9 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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