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 Gen::gen_uniform #283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ impl Gen {
{
self.rng.gen_range(range)
}

/// Create a random number uniformly distributed on the unit interval [0, 1)
pub fn gen_uniform(&mut self) -> f32 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should probably be gen_uniform_f32 or somesuch, since there could as well be versions for f64, and all the integer types.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess one could make it polymorphic using num-traits..

self.gen()
}
}

/// Creates a shrinker with zero elements.
Expand Down Expand Up @@ -1583,4 +1588,14 @@ mod test {
],
);
}

#[test]
fn gen_uniform() {
let mut g = Gen::new(10);
for _ in 0..100 {
let u = g.gen_uniform();
assert!(u >= 0.);
assert!(u < 1.);
}
}
}