Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pseudorandom generator for move tests #7554

Merged
merged 17 commits into from
Jan 24, 2023
Merged
Prev Previous commit
Next Next commit
Update doc II
  • Loading branch information
jonas-lj committed Jan 20, 2023
commit 34935dd58dcb926a2ce1066f4f8eb7f80c4da315
18 changes: 9 additions & 9 deletions crates/sui-framework/sources/test/random.move
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ module sui::random {
output
}

/// Use the given pseudorandom generator to get a random u64 integer.
/// Use the given pseudorandom generator to get a random `u64` integer.
public fun get_next_u64(random: &mut Random): u64 {
let bytes = get_next_digest(random);
bcs::peel_u64(&mut bcs::new(bytes))
}

/// Use the given pseudorandom generator to get an u64 integer in the range [0, ...,
/// Use the given pseudorandom generator to get an `u64` integer in the range [0, ...,
/// 2^bit_length - 1].
fun get_next_u64_with_bit_length(random: &mut Random, bit_length: u8): u64 {
assert!(bit_length > 0 && bit_length < 64, 0);
Expand All @@ -80,11 +80,11 @@ module sui::random {
let length = 0;

while (mid > 0) {
let half = n >> mid;
if (half > 0) {
let n_mod_mid = n >> mid;
if (n_mod_mid > 0) {
// The bit length of n is strictly larger than mid
length = length + mid;
n = half;
n = n_mod_mid;
};
mid = mid >> 1;
};
Expand All @@ -96,8 +96,8 @@ module sui::random {
length
}

/// Use the given pseudo-random generator to get a random integer in the range [0, ...,
/// upper_bound - 1].
/// Use the given pseudo-random generator to get a random `u64` integer in the range
/// [0, ..., upper_bound - 1].
public fun get_next_u64_in_range(random: &mut Random, upper_bound: u64): u64 {
assert!(upper_bound > 0, 0);
let bit_length = bit_length(upper_bound);
Expand All @@ -109,12 +109,12 @@ module sui::random {
}


/// Use the given pseudorandom generator to get a random byte.
/// Use the given pseudorandom generator to get a random `u8`.
public fun get_next_u8(random: &mut Random): u8 {
*vector::borrow(&get_next_digest(random), 0)
}

/// Use the given pseudorandom generator to get a random boolean.
/// Use the given pseudorandom generator to get a random `bool`.
public fun get_next_bool(random: &mut Random): bool {
get_next_u8(random) % 2 == 1
}
Expand Down