1+ use std:: fmt:: { self , Display } ;
12use std:: str:: FromStr ;
23
34use anyhow:: { bail, Error , Result } ;
5+ use fastrand:: Rng ;
46
57#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
68pub enum RandomGen {
@@ -14,6 +16,21 @@ pub enum RandomGen {
1416 BigInts ,
1517}
1618
19+ impl Display for RandomGen {
20+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
21+ match self {
22+ Self :: Uniform => write ! ( f, "uniform" ) ,
23+ Self :: OneOverRand32 => write ! ( f, "one_over_rand32" ) ,
24+ Self :: SimpleUniform32 => write ! ( f, "simple_uniform32" ) ,
25+ Self :: SimpleInt32 => write ! ( f, "simple_int32" ) ,
26+ Self :: IntEInt => write ! ( f, "int_e_int" ) ,
27+ Self :: SimpleInt64 => write ! ( f, "simple_int64" ) ,
28+ Self :: BigIntDotInt => write ! ( f, "bigint_int_dot_int" ) ,
29+ Self :: BigInts => write ! ( f, "big_ints" ) ,
30+ }
31+ }
32+ }
33+
1734impl FromStr for RandomGen {
1835 type Err = Error ;
1936
@@ -45,4 +62,24 @@ impl RandomGen {
4562 "big_ints" ,
4663 ]
4764 }
65+
66+ pub fn gen ( & self , rng : & mut Rng ) -> String {
67+ match self {
68+ Self :: Uniform
69+ | Self :: OneOverRand32
70+ | Self :: SimpleUniform32
71+ | Self :: SimpleInt32
72+ | Self :: SimpleInt64 => lexical:: to_string ( match self {
73+ Self :: Uniform => rng. f64 ( ) ,
74+ Self :: OneOverRand32 => 1. / rng. u32 ( 1 ..) as f64 ,
75+ Self :: SimpleUniform32 => rng. u32 ( ..) as f64 / u32:: MAX as f64 ,
76+ Self :: SimpleInt32 => rng. u32 ( ..) as f64 ,
77+ Self :: SimpleInt64 => rng. u64 ( ..) as f64 ,
78+ _ => unreachable ! ( ) ,
79+ } ) ,
80+ Self :: IntEInt => format ! ( "{}e{}" , rng. u32 ( ..) , rng. u32 ( ..99 ) ) ,
81+ Self :: BigInts => format ! ( "{}{}{}" , rng. u64 ( ..) , rng. u64 ( ..) , rng. u64 ( ..) ) ,
82+ Self :: BigIntDotInt => format ! ( "{}.{}" , rng. u32 ( ..) , rng. u32 ( ..) ) ,
83+ }
84+ }
4885}
0 commit comments