StandardUniform<usize> Breakage In 0.9
Introduction
The recent upgrade to the rand
crate version 0.9 has introduced a breaking change that affects the StandardUniform<usize>
implementation. This change was motivated by the need to ensure consistency in the generation of random numbers across different architectures. However, this decision has impacted users who rely on the rand
crate for generating random test input values. In this article, we will explore the valid use case for StandardUniform<usize>
and why its removal is causing issues for users.
Background
The rand
crate is a popular library for generating random numbers in Rust. It provides a range of distributions, including the StandardUniform
distribution, which generates uniformly distributed random numbers. The StandardUniform
distribution is implemented for various integer types, including usize
. However, in version 0.9, the implementation of StandardUniform<usize>
was removed.
The Original Motivation
The original motivation for removing the implementation of StandardUniform<usize>
was to address a concern about the consistency of random number generation across different architectures. As noted by @dhardy, the random()
method of the rng
object would generate different results on 32-bit and 64-bit architectures. This inconsistency was seen as a problem, and the implementation of StandardUniform<usize>
was removed to address it.
A Valid Use Case
However, the removal of StandardUniform<usize>
has introduced a breaking change that affects users who rely on the rand
crate for generating random test input values. One such user is the author of the bit vector crate
, who has implemented a fuzzing approach using the rand
crate to generate random test vectors. The author has provided an example of a function that uses the rand
crate to generate random test input values for a bit vector.
fn get_int_inner<B, I>(max_capacity: usize)
where
rand::distributions::Standard: rand::distributions::Distribution<I>,
B: BitVector + TryFrom<I, Error: std::fmt::Debug>,
<B as IArray>::I: StaticCast<I>,
I: Integer,
{
let mut rng = thread_rng();
let ints = (0..max_capacity / I::BITS)
.map(|_| rng.gen::<I>())
.collect::<Vec<_>>();
let bv = ints.iter().map(|i| B::try_from(*i).unwrap()).fold(
B::with_capacity(max_capacity),
|mut acc, bv| {
acc.append(&bv);
return acc;
},
);
for (idx, int) in ints.iter().enumerate() {
assert_eq!(Some(*int), bv.get_int::<I>(idx));
}
}
This function uses the rand
crate to generate random test input values for a bit vector. The author has used generics and macros to make the function reusable for different types. However, the removal of StandardUniform<usize>
has broken this approach.
Conclusion
The removal of StandardUniform<usize>
in version 0.9 of the rand
crate has introduced a breaking change that affects users who rely on the rand
crate for generating random test input values. While the original motivation for removing the implementation was to address a concern about consistency across different architectures, the removal has impacted users who have valid use cases for StandardUniform<usize>
. We hope that the maintainers of the rand
crate will reconsider this decision and provide a solution that allows users to continue using the rand
crate for generating random test input values.
Recommendations
Based on the discussion above, we recommend the following:
- The maintainers of the
rand
crate should reconsider the decision to remove the implementation ofStandardUniform<usize>
. - A solution should be provided to allow users to continue using the
rand
crate for generating random test input values. - The
rand
crate should provide a way for users to opt-in to the old behavior, allowing them to continue using theStandardUniform<usize>
implementation.
Introduction
The recent upgrade to the rand
crate version 0.9 has introduced a breaking change that affects the StandardUniform<usize>
implementation. This change was motivated by the need to ensure consistency in the generation of random numbers across different architectures. However, this decision has impacted users who rely on the rand
crate for generating random test input values. In this Q&A article, we will address some of the frequently asked questions about the StandardUniform<usize>
breakage in 0.9.
Q: What is the reason behind the removal of StandardUniform in version 0.9?
A: The original motivation for removing the implementation of StandardUniform<usize>
was to address a concern about the consistency of random number generation across different architectures. As noted by @dhardy, the random()
method of the rng
object would generate different results on 32-bit and 64-bit architectures. This inconsistency was seen as a problem, and the implementation of StandardUniform<usize>
was removed to address it.
Q: Why is the removal of StandardUniform causing issues for users?
A: The removal of StandardUniform<usize>
has introduced a breaking change that affects users who rely on the rand
crate for generating random test input values. One such user is the author of the bit vector crate
, who has implemented a fuzzing approach using the rand
crate to generate random test vectors. The author has provided an example of a function that uses the rand
crate to generate random test input values for a bit vector.
fn get_int_inner<B, I>(max_capacity: usize)
where
rand::distributions::Standard: rand::distributions::Distribution<I>,
B: BitVector + TryFrom<I, Error: std::fmt::Debug>,
<B as IArray>::I: StaticCast<I>,
I: Integer,
{
let mut rng = thread_rng();
let ints = (0..max_capacity / I::BITS)
.map(|_| rng.gen::<I>())
.collect::<Vec<_>>();
let bv = ints.iter().map(|i| B::try_from(*i).unwrap()).fold(
B::with_capacity(max_capacity),
|mut acc, bv| {
acc.append(&bv);
return acc;
},
);
for (idx, int) in ints.iter().enumerate() {
assert_eq!(Some(*int), bv.get_int::<I>(idx));
}
}
This function uses the rand
crate to generate random test input values for a bit vector. The author has used generics and macros to make the function reusable for different types. However, the removal of StandardUniform<usize>
has broken this approach.
Q: Can I still use the rand crate for generating random test input values?
A: Yes, you can still use the rand
crate for generating random test input values. However, you will need to use a different distribution, such as rng.random_range
, to generate random numbers. This will require changes to your code, but it is still possible to use the rand
crate for generating random test input values.
Q: What is the recommended solution for users who rely on the rand crate for generating random test input values?
A: The recommended solution is to use a different distribution, such as rng.random_range
, to generate random numbers. This will require changes to your code, but it is still possible to use the rand
crate for generating random test input values. Additionally, the maintainers of the rand
crate should reconsider the decision to remove the implementation of StandardUniform<usize>
and provide a solution that allows users to continue using the rand
crate for generating random test input values.
Q: How can I opt-in to the old behavior and continue using the StandardUniform implementation?
A: Unfortunately, it is not possible to opt-in to the old behavior and continue using the StandardUniform<usize>
implementation. The implementation was removed in version 0.9, and there is no way to use the old implementation. However, you can use a different distribution, such as rng.random_range
, to generate random numbers.
Conclusion
The removal of StandardUniform<usize>
in version 0.9 of the rand
crate has introduced a breaking change that affects users who rely on the rand
crate for generating random test input values. While the original motivation for removing the implementation was to address a concern about consistency across different architectures, the removal has impacted users who have valid use cases for StandardUniform<usize>
. We hope that the maintainers of the rand
crate will reconsider this decision and provide a solution that allows users to continue using the rand
crate for generating random test input values.