How do i do this in the rug crate? random range

i am using this rn, but i heard this is better, but i dont know how to use it, also how can i take advantage of smid

/*
[dependencies]
bnum = { version = "0.11", features = ["rand"] }
rand = "0.8"
*/

use bnum::types::U256;
use rand::Rng;

fn generate_private_key() -> U256 {
    const LOW: U256 = U256::parse_str_radix(
        "18909BDE11F67C97A53C62F45E632EAB58EA0D73A5FAC9EB50295FAD40A57EB5",
        16,
    );
    const HIGH: U256 = U256::parse_str_radix(
        "DD10559E1285B3EE0303B159B8D6D8D0B88E6168D1C1D6000000000000000000",
        16,
    );

    rand::thread_rng().gen_range(LOW..HIGH)
}

fn generate_private_key_hex() -> String {
    format!("0x{:064x}", generate_private_key())
}

Looks like the rug::rand module only generates u32 so it can't be used to implement your generate_private_key function and return U256 without losing a lot of precision compared to your current implementation.

When you said "i heard this is better" I assume you mean rug is better (although you used the word "this" for multiple things in one sentence). But rug seems to mainly provide arbitrary-precision numbers, not random number generation. What did you hear exactly about rug and where did you hear it? Please post the quote or link to the information you heard.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.