Archive native training 2026-10-08 manifest 55e57f14c331c9a0a4cab71d6c237d5436e691a2f2862b2734844db400e284c8
b60a0ad verified Download native_training_20261008/native/source/random_stream.rs from TryDotAtwo/quantum-generator: direct link, hf CLI and curl.
- Browser
- Download file 5.57 kB
-
https://huggingface.co/datasets/TryDotAtwo/quantum-generator/resolve/main/native_training_20261008/native/source/random_stream.rs
- Command line
-
hf download hf://datasets/TryDotAtwo/quantum-generator/native_training_20261008/native/source/random_stream.rs
-
curl -L -o random_stream.rs https://huggingface.co/datasets/TryDotAtwo/quantum-generator/resolve/main/native_training_20261008/native/source/random_stream.rs
5.57 kB
| //! Integer-seeded MT19937 and random()/randbelow() semantics of CPython 3.11.5. | |
| //! No interpreter or Python library is linked or executed. | |
| // MT19937 algorithm: Copyright (C) 1997 - 2002, Makoto Matsumoto and | |
| // Takuji Nishimura, All rights reserved. | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // 1. Redistributions of source code must retain the above copyright notice, | |
| // this list of conditions and the following disclaimer. | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // 3. The names of its contributors may not be used to endorse or promote | |
| // products derived from this software without specific prior written permission. | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| // POSSIBILITY OF SUCH DAMAGE. | |
| pub struct RandomStream { state: [u32;624], index: usize } | |
| impl RandomStream { | |
| pub fn new(seed:u64)->Self { | |
| let key=[seed as u32,(seed>>32) as u32]; | |
| let keys=if key[1]==0 {&key[..1]} else {&key[..]}; | |
| let mut r=Self{state:[0;624],index:624}; | |
| r.state[0]=19650218; | |
| for i in 1..624 {r.state[i]=1812433253u32.wrapping_mul(r.state[i-1]^(r.state[i-1]>>30)).wrapping_add(i as u32);} | |
| let(mut i,mut j)=(1,0); | |
| for _ in 0..624 { | |
| r.state[i]=(r.state[i]^(r.state[i-1]^(r.state[i-1]>>30)).wrapping_mul(1664525)).wrapping_add(keys[j]).wrapping_add(j as u32); | |
| i+=1;j+=1; | |
| if i==624 {r.state[0]=r.state[623];i=1;} | |
| if j==keys.len(){j=0;} | |
| } | |
| for _ in 0..623 { | |
| r.state[i]=(r.state[i]^(r.state[i-1]^(r.state[i-1]>>30)).wrapping_mul(1566083941)).wrapping_sub(i as u32); | |
| i+=1;if i==624 {r.state[0]=r.state[623];i=1;} | |
| } | |
| r.state[0]=0x80000000;r | |
| } | |
| pub fn next_u32(&mut self)->u32 { | |
| if self.index==624 { | |
| for i in 0..624 { | |
| let y=(self.state[i]&0x80000000)|(self.state[(i+1)%624]&0x7fffffff); | |
| self.state[i]=self.state[(i+397)%624]^(y>>1)^if y&1!=0 {0x9908b0df} else {0}; | |
| } | |
| self.index=0; | |
| } | |
| let mut y=self.state[self.index];self.index+=1; | |
| y^=y>>11;y^=(y<<7)&0x9d2c5680;y^=(y<<15)&0xefc60000;y^=y>>18;y | |
| } | |
| pub fn random(&mut self)->f64 { | |
| let a=self.next_u32()>>5;let b=self.next_u32()>>6; | |
| (a as f64*67108864.0+b as f64)/9007199254740992.0 | |
| } | |
| pub fn getrandbits(&mut self,k:u32)->u64 { | |
| assert!(k<=64); | |
| match k {0=>0,1..=32=>(self.next_u32()>>(32-k)) as u64, | |
| _=>{let low=self.next_u32() as u64;low|(((self.next_u32()>>(64-k)) as u64)<<32)}} | |
| } | |
| pub fn randbelow(&mut self,n:u64)->u64 { | |
| assert!(n>0); | |
| let k=64-n.leading_zeros(); | |
| loop {let value=self.getrandbits(k);if value<n{return value;}} | |
| } | |
| pub fn shuffle<T>(&mut self,items:&mut [T]) { | |
| for i in (1..items.len()).rev(){let j=self.randbelow((i+1) as u64) as usize;items.swap(i,j);} | |
| } | |
| /// Unweighted sample(range(n), k), preserving both source selection branches. | |
| pub fn sample_range(&mut self,n:usize,k:usize)->Vec<usize> { | |
| assert!(k<=n); | |
| let mut threshold=21usize; | |
| if k>5 {let mut power=1usize;while power<k.checked_mul(3).unwrap(){power=power.checked_mul(4).unwrap();}threshold=threshold.checked_add(power).unwrap();} | |
| let mut result=Vec::with_capacity(k); | |
| if n<=threshold { | |
| let mut pool:Vec<usize>=(0..n).collect(); | |
| for i in 0..k {let j=self.randbelow((n-i) as u64) as usize;result.push(pool[j]);pool[j]=pool[n-i-1];} | |
| } else { | |
| let mut selected=std::collections::HashSet::with_capacity(k); | |
| for _ in 0..k {loop {let j=self.randbelow(n as u64) as usize;if selected.insert(j){result.push(j);break;}}} | |
| } | |
| result | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| fn seed_zero_known_uniform_vector(){ | |
| let mut r=RandomStream::new(0); | |
| for expected in [0.8444218515250481f64,0.7579544029403025,0.420571580830845,0.25891675029296335,0.5112747213686085] { | |
| assert_eq!(r.random().to_bits(),expected.to_bits()); | |
| } | |
| } | |
| fn seed_zero_known_uint32_vector(){ | |
| let mut r=RandomStream::new(0); | |
| for expected in [3626764237,1654615998,3255389356,3823568514,1806341205] {assert_eq!(r.next_u32(),expected);} | |
| } | |
| fn seed_one_known_uniform(){assert_eq!(RandomStream::new(1).random().to_bits(),0.13436424411240122f64.to_bits());} | |
| fn zero_bits_does_not_consume_state(){let mut r=RandomStream::new(0);assert_eq!(r.getrandbits(0),0);assert_eq!(r.next_u32(),3626764237);} | |
| } | |