2021-06-29 09:26:16 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use embassy::executor::Spawner;
|
2021-06-30 04:55:30 +02:00
|
|
|
use embassy_nrf::rng::Rng;
|
2022-06-12 22:15:44 +02:00
|
|
|
use embassy_nrf::{interrupt, Peripherals};
|
2021-06-29 09:26:16 +02:00
|
|
|
use rand::Rng as _;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 04:35:06 +02:00
|
|
|
|
2021-06-29 09:26:16 +02:00
|
|
|
#[embassy::main]
|
|
|
|
async fn main(_spawner: Spawner, p: Peripherals) {
|
2022-01-05 23:59:28 +01:00
|
|
|
let mut rng = Rng::new(p.RNG, interrupt::take!(RNG));
|
2021-06-29 09:26:16 +02:00
|
|
|
|
|
|
|
// Async API
|
|
|
|
let mut bytes = [0; 4];
|
2022-01-13 23:10:24 +01:00
|
|
|
rng.fill_bytes(&mut bytes).await;
|
2021-06-29 09:26:16 +02:00
|
|
|
defmt::info!("Some random bytes: {:?}", bytes);
|
|
|
|
|
|
|
|
// Sync API with `rand`
|
|
|
|
defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10));
|
2021-06-30 04:55:30 +02:00
|
|
|
|
|
|
|
let mut bytes = [0; 1024];
|
2022-01-13 23:10:24 +01:00
|
|
|
rng.fill_bytes(&mut bytes).await;
|
2021-06-30 04:55:30 +02:00
|
|
|
let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros());
|
|
|
|
let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones());
|
2022-06-12 22:15:44 +02:00
|
|
|
defmt::info!("Chance of zero: {}%", zero_count * 100 / (bytes.len() as u32 * 8));
|
|
|
|
defmt::info!("Chance of one: {}%", one_count * 100 / (bytes.len() as u32 * 8));
|
2021-06-29 09:26:16 +02:00
|
|
|
}
|