From 46a1639990c7e696181196b8120cf68571e239ab Mon Sep 17 00:00:00 2001 From: John Date: Thu, 25 Jul 2024 23:59:41 -0500 Subject: [PATCH] sample-code: Have fun with random number generators --- sample-code/rand.cl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sample-code/rand.cl diff --git a/sample-code/rand.cl b/sample-code/rand.cl new file mode 100644 index 0000000..4915e5b --- /dev/null +++ b/sample-code/rand.cl @@ -0,0 +1,37 @@ +//! Pseudo-random number generation using a LFSR algorithm + +static state: u64 = 0xdeadbeefdeadbeef; + +pub fn seed(seed: u64) { + state = seed; +} + +pub fn lfsr_next() { + state ^= state >> 7; + state ^= state << 9; + state ^= state >> 13; +} + +/// Returns a pseudorandom byte +pub fn rand() -> u8 { + for _ in 0..8 { + lfsr_next() + } + state & 0xff +} + +// Prints a maze out of diagonal box drawing characters, ['╲', '╱'] +fn mazel(width: u64, height: u64) { + let walls = ['\u{2571}', '\u{2572}']; + rand_rect(width, height, walls) +} + +// Prints a rectangle with the provided walls +fn rand_rect(width: u64, height: u64, walls: [char; 2]) { + for _ in 0..height { + for _ in 0..width { + print(walls[rand() % 2]) + } + println() + } +}