random.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use super::*; 20 21 impl<E: Environment, I: IntegerType> Distribution<Integer<E, I>> for Standard { 22 #[inline] 23 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Integer<E, I> { 24 Integer::new(Uniform::rand(rng)) 25 } 26 } 27 28 #[cfg(test)] 29 mod tests { 30 use super::*; 31 use alphavm_console_network_environment::Console; 32 33 use std::collections::HashSet; 34 35 type CurrentEnvironment = Console; 36 37 const ITERATIONS: usize = 10; 38 39 fn check_random<I: IntegerType>(rng: &mut TestRng) { 40 // Initialize a set to store all seen random elements. 41 let mut set = HashSet::with_capacity(ITERATIONS); 42 43 // Note: This test technically has a `(1 + 2 + ... + ITERATIONS) / MODULUS` probability of being flaky. 44 for _ in 0..ITERATIONS { 45 // Sample a random value. 46 let integer: Integer<CurrentEnvironment, I> = Uniform::rand(rng); 47 assert!(!set.contains(&integer)); 48 49 // Add the new random value to the set. 50 set.insert(integer); 51 } 52 } 53 54 #[test] 55 fn test_random() { 56 let mut rng = TestRng::default(); 57 58 check_random::<u32>(&mut rng); 59 check_random::<u64>(&mut rng); 60 check_random::<u128>(&mut rng); 61 62 check_random::<i32>(&mut rng); 63 check_random::<i64>(&mut rng); 64 check_random::<i128>(&mut rng); 65 } 66 }