I am trying to generate random number using the tutorial:
#[pallet::config]
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type RandomnessSource: Randomness<Self::Hash, Self::BlockNumber>;
}
fn get_and_increment_nonce() -> Vec<u8> {
let nonce = <Nonce<T>>::get();
<Nonce<T>>::put(nonce.wrapping_add(1));
nonce.encode()
}
for _ in draws_in_round..end_index {
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 u64; qed");
println!("random_number, {:?}", random_number);
}
But I don't get random number when I run the tests. I get
random_number, 0
random_number, 1
random_number, 2
random_number, 3
random_number, 4
How to generate u64 random numbers?.
RandomnessSource
type in the runtime?Randomness = TestRandomness
orRandomness = RandomnessCollectiveFlip
in yourmock.rs
? TheTestRandomness
is not doing much, annoyed me in the past as well.