Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/crumbs/src/gen/rng.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / crumbs / src / gen / rng.rs` 99 lines ยท 2950 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Deterministic, named random streams (xoshiro256** seeded from BLAKE3). 17 //! A stream is fully determined by its name and integer parts, so every 18 //! generator draw can be replayed exactly years later. 19 20 use crate::digest::{digest, Domain}; 21 22 #[derive(Clone, Debug)] 23 pub struct Rng { 24 s: [u64; 4], 25 } 26 27 impl Rng { 28 pub fn named(name: &str, parts: &[u64]) -> Rng { 29 let mut bytes = Vec::with_capacity(name.len() + 1 + parts.len() * 8); 30 bytes.extend_from_slice(name.as_bytes()); 31 bytes.push(0); 32 for p in parts { 33 bytes.extend_from_slice(&p.to_le_bytes()); 34 } 35 let d = digest(Domain::Rng, &bytes).0; 36 let mut s = [0u64; 4]; 37 for (i, w) in s.iter_mut().enumerate() { 38 *w = u64::from_le_bytes(d[i * 8..i * 8 + 8].try_into().unwrap()); 39 } 40 if s == [0; 4] { 41 s[0] = 1; 42 } 43 Rng { s } 44 } 45 46 pub fn next_u64(&mut self) -> u64 { 47 let s = &mut self.s; 48 let result = s[1].wrapping_mul(5).rotate_left(7).wrapping_mul(9); 49 let t = s[1] << 17; 50 s[2] ^= s[0]; 51 s[3] ^= s[1]; 52 s[1] ^= s[2]; 53 s[0] ^= s[3]; 54 s[2] ^= t; 55 s[3] = s[3].rotate_left(45); 56 result 57 } 58 59 /// Uniform in [0, n); n == 0 means the full u64 range. 60 pub fn below(&mut self, n: u64) -> u64 { 61 if n == 0 { 62 return self.next_u64(); 63 } 64 let limit = u64::MAX - (u64::MAX % n); 65 loop { 66 let v = self.next_u64(); 67 if v < limit { 68 return v % n; 69 } 70 } 71 } 72 73 /// Uniform in [lo, hi] inclusive. 74 pub fn range(&mut self, lo: u64, hi: u64) -> u64 { 75 if hi <= lo { 76 return lo; 77 } 78 let span = hi - lo; 79 if span == u64::MAX { 80 return self.next_u64(); 81 } 82 lo + self.below(span + 1) 83 } 84 85 pub fn pick<'a, T>(&mut self, items: &'a [T]) -> &'a T { 86 &items[self.below(items.len() as u64) as usize] 87 } 88 89 pub fn chance(&mut self, num: u64, den: u64) -> bool { 90 self.below(den) < num 91 } 92 93 pub fn shuffle(&mut self, v: &mut [T]) { 94 for i in (1..v.len()).rev() { 95 let j = self.below(i as u64 + 1) as usize; 96 v.swap(i, j); 97 } 98 } 99 }