2

I want to use rand package, specially WeightedIndex distribution in substrate.

https://paritytech.github.io/substrate/master/rand/distributions/struct.WeightedIndex.html

This is the code so far:

        use rand::distributions::WeightedIndex;
        use rand::prelude::*;
        use rand::{rngs::StdRng, SeedableRng};

        fn draw_juror_for_citizen_profile_function(
            citizen_id: u128,
            length: usize,
        ) -> DispatchResult {

            let nonce = Self::get_and_increment_nonce();

            let random_seed = T::RandomnessSource::random(&nonce).encode();
            let random_number = u64::decode(&mut random_seed.as_ref())
            .expect("secure hashes should always be bigger than u32; qed");
            let rng: StdRng = SeedableRng::seed_from_u64(random_number);
            Ok(())
        }

        fn get_and_increment_nonce() -> Vec<u8> {
            let nonce = <Nonce<T>>::get();
            <Nonce<T>>::put(nonce.wrapping_add(1));
            nonce.encode()
        }

But it is giving following error in no_std environment:

In cargo.toml:

[dependencies]
rand = { version = "0.8.4", default-features = false }

Error:

--- stderr
     Compiling pallet-template v3.0.0-monthly-2021-10 (/home/amiya/Documents/workspace/substrate/shivarthu/substrate-node-template/pallets/template)
  error[E0432]: unresolved import `rand::distributions::WeightedIndex`
    --> /home/amiya/Documents/workspace/substrate/shivarthu/substrate-node-template/pallets/template/src/lib.rs:35:6
     |
  35 |     use rand::distributions::WeightedIndex;
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `WeightedIndex` in `distributions`

  error[E0432]: unresolved import `rand::rngs::StdRng`
    --> /home/amiya/Documents/workspace/substrate/shivarthu/substrate-node-template/pallets/template/src/lib.rs:37:16
     |
  37 |     use rand::{rngs::StdRng, SeedableRng};
     |                ^^^^^^^^^^^^ no `StdRng` in `rngs`
Amiya Behera
  • 2,210
  • 19
  • 32

0 Answers0