qfall_math/utils/sample.rs
1// Copyright 2023 Jan Niklas Siemer
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module includes core functionality to sample according to random distributions.
10
11pub mod binomial;
12pub mod discrete_gauss;
13pub mod uniform;
14
15/// Returns a fresh seed for every call within the same thread, starting from the
16/// same sequence of seeds for every test, which allows to test seeded functions
17/// repeatedly, e.g. in loops, without receiving the same sample in every iteration.
18#[cfg(test)]
19pub(crate) fn test_seed() -> [u8; 32] {
20 use std::cell::Cell;
21
22 thread_local!(static COUNTER: Cell<u64> = const { Cell::new(0) });
23 let counter = COUNTER.with(|counter| {
24 let value = counter.get();
25 counter.set(value + 1);
26 value
27 });
28
29 let mut seed = [0; 32];
30 seed[..8].copy_from_slice(&counter.to_le_bytes());
31 seed
32}