diff options
Diffstat (limited to 'examples/nrf54l15/src/bin/rng.rs')
| -rw-r--r-- | examples/nrf54l15/src/bin/rng.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/nrf54l15/src/bin/rng.rs b/examples/nrf54l15/src/bin/rng.rs new file mode 100644 index 000000000..b2d7f906b --- /dev/null +++ b/examples/nrf54l15/src/bin/rng.rs | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_nrf::cracen::Cracen; | ||
| 6 | use rand::Rng as _; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[embassy_executor::main] | ||
| 10 | async fn main(_spawner: Spawner) { | ||
| 11 | let p = embassy_nrf::init(Default::default()); | ||
| 12 | let mut rng = Cracen::new_blocking(p.CRACEN); | ||
| 13 | |||
| 14 | // Async API | ||
| 15 | let mut bytes = [0; 4]; | ||
| 16 | rng.blocking_fill_bytes(&mut bytes); | ||
| 17 | defmt::info!("Some random bytes: {:?}", bytes); | ||
| 18 | |||
| 19 | // Sync API with `rand` | ||
| 20 | defmt::info!("A random number from 1 to 10: {:?}", rng.random_range(1..=10)); | ||
| 21 | |||
| 22 | let mut bytes = [0; 1024]; | ||
| 23 | rng.blocking_fill_bytes(&mut bytes); | ||
| 24 | let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros()); | ||
| 25 | let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones()); | ||
| 26 | defmt::info!("Chance of zero: {}%", zero_count * 100 / (bytes.len() as u32 * 8)); | ||
| 27 | defmt::info!("Chance of one: {}%", one_count * 100 / (bytes.len() as u32 * 8)); | ||
| 28 | } | ||
