Skip to content

Endianness is inconsistent between rand 0.4.0 and rand 0.7.0 #876

@dingxiangfei2009

Description

@dingxiangfei2009

Related to #875

During the tests, we discovered inconsistency in endianness when comparing the outputs from rand 0.4.0 and rand 0.7.0.
As an extension to #875, tests are run to check if next_u64 is consistent across versions. These tests shows that the byte order of the output has switched.

For instance, on rand 0.4.0, running the following code

let mut rng = rand::ChaChaRng::new_unseeded();
let lower = 88293;
let higher = 9300932;
rng.set_counter(lower, higher);
assert_eq_bin!(4060232610, rng.next_u32());
assert_eq_bin!(2786236710, rng.next_u32());
assert_eq_bin!(3748877652, rng.next_u32());
assert_eq_bin!(3324792667, rng.next_u32());
println!("{:b}", rng.next_u64());

gives

10100111101100110100101110001011
00000000011011000000000000111011

With rand 0.7.0, running the following

let mut rng = ChaChaRng::from_seed([0u8; 32]);
let lower = 88293;
let higher = 9300932;
rng.set_word_pos(lower << 4);
rng.set_stream(higher);
assert_eq!(4060232610, rng.next_u32());
assert_eq!(2786236710, rng.next_u32());
assert_eq!(3748877652, rng.next_u32());
assert_eq!(3324792667, rng.next_u32());
println!("{:b}", rng.next_u64());

gives us

00000000011011000000000000111011
10100111101100110100101110001011

So byte orders are reversed.

I also reviewed the wordings of RngCore trait, and the required methods have no guarantees on the byte order of next_* methods.

Maybe we should enforce some form of endianness requirement?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions