-
Notifications
You must be signed in to change notification settings - Fork 75
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
Best way to generate float values in range #77
Comments
I imagine the most straightforward way would probably be to generate unsigned integers and use one of the distributions to make them into a float (maybe taken from the rand crate?) |
I think it would be nice to have But I agree that this functionality is desirable! |
In the meantime, you can do something like this let x: f64 = u.arbitrary()?;
// clamp `x` to `MY_MIN..=MY_MAX`
let x = min(x, MY_MAX);
let x = max(x, MY_MIN); and ignore bias for now, instead hoping that libfuzzer will figure things out alright via its coverage feedback. |
Thanks for the replies, Nick! I might get back to this -- right now though I've implemented it by getting a random int and scaling it. This surely has a bias but it was done in 10min and suffices for my usage for now :) |
What does 'unbiased' mean in this case? All floating point bit patterns between the min and max are equally likely? Or something like @killercup/rand's where the distance between each of the possible values is the same but some bit patterns are not covered. |
To generate all the float bit patterns you could just |
This is a good question, and I'm not sure which we should aim for! |
Hi folks, in technocreatives/dbc-codegen#12 we're generating implementations of
Arbitrary
for structs where we know some values have to be in a specific range. For ints, this is not a problem as we just useUnstructured::int_in_range
, but I'm unsure how to proceed for floats. (We currently just go with always setting the float field to the lower bound.)My initial idea was to keep generating floats until we get one in ourr range, but that seems awfully slow. Is there a good strategy for generating floats that are in a specific range? Anything I should look up? All I can find assumes an existing random number library is in place… which I'm hesitant to add just for this.
The text was updated successfully, but these errors were encountered: