dimanche 26 mai 2019

create a fixed length random byte array between range

I need to generate an array of random 20 bytes between given range of arrays..

I want to do something like:

use rand::prelude::*;

fn main() {
    let low = [0u8; 20];
    let high = [2u8; 20];
    let value = rand::thread_rng().gen_range(low, high);
    println!("{:?}", value);
}

but I get following error:

error[E0277]: the trait bound `[u8; 20]: rand::distributions::uniform::SampleUniform` is not satisfied
 --> src\main.rs:6:36
  |
6 |     let value = rand::thread_rng().gen_range(low, high);
  |                                    ^^^^^^^^^ the trait `rand::distributions::uniform::SampleUniform` is not implemented for `[u8; 20]`

error: aborting due to previous error

I tried implementing SampleUniform and UniformSampler without much success.

Is there a simple way to implement this?




Aucun commentaire:

Enregistrer un commentaire