| 72 | } |
| 73 | |
| 74 | pub fn get_random_value(&mut self, t: Type) -> Result<Value> { |
| 75 | match t { |
| 76 | Type::Scalar(_) | Type::Array(_, _) => { |
| 77 | let bit_size = get_size_in_bits(t)?; |
| 78 | let byte_size = (bit_size + 7) / 8; |
| 79 | // the last random byte should contain bits_to_flush zeros |
| 80 | let bits_to_flush = 8 * byte_size - bit_size; |
| 81 | let mut bytes = self.get_random_bytes(byte_size as usize)?; |
| 82 | // Remove unused random bits |
| 83 | if !bytes.is_empty() { |
| 84 | *bytes.last_mut().unwrap() >>= bits_to_flush; |
| 85 | } |
| 86 | Ok(Value::from_bytes(bytes)) |
| 87 | } |
| 88 | Type::Tuple(_) | Type::Vector(_, _) | Type::NamedTuple(_) => { |
| 89 | let ts = get_types_vector(t)?; |
| 90 | let mut v = vec![]; |
| 91 | for sub_t in ts { |
| 92 | v.push(self.get_random_value((*sub_t).clone())?) |
| 93 | } |
| 94 | Ok(Value::from_vector(v)) |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Generates a random 64-bit integer modulo a given modulus. |
| 100 | // To avoid modulo bias, rejection sampling is performed. |