-
-
Notifications
You must be signed in to change notification settings - Fork 445
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
Closed01
: distribution that generates floats in the range [0, 1]
#1361
Comments
One thing to consider here is that a "fully accurate" implementation of sampling on [0,1] essentially will never select For example, if we select a real number In my opinion, there should probably only be two distributions for selecting floats between 0 and 1, based on the following two (idealized) processes:
|
Hmm, the documentation for Unity's
It sounds like they're making a significant sacrifice in precision in order to guarantee full coverage of the range. That may make sense for an implementation specialized for games but I can appreciate why it wouldn't necessarily be useful for a general-purpose library. |
Unity's documentation is definitely wrong here, as:
EDIT: My guess is that they are generating a random float in the range |
Okay, I'm actually starting to grok the problem now. For one, I for some reason thought that Anyway, I think any non-expert would see "uniformly distributed between 0 and 1 (inclusive)" and interpret that as "just as likely to be in the range The fact that the actual set of numbers is not evenly spaced is an idiosyncrasy of floating-point representation that I think most people won't really care or want to think about. IMO, a specialized version of FWIW, we ended up deciding not to implement this exact behavior at the Rust level anyway. |
Closing. FYI, just using |
Background
What is your motivation?
My current work project involves a networking and utility library that is primarily targeted at Unity developers, with a core written in Rust.
Without going into details currently under NDA, we're trying to provide a higher quality/more featureful substitute for Unity's
Random
class, which has the idiosyncrasy that it generates floats in the closed range[0, 1]
: https://docs.unity3d.com/ScriptReference/Random-value.htmlOf course, it's relatively simple to implement this with
Uniform::new_inclusive(0f64, 1f64)
, but its constructor does non-trivial work which means we either need to keep an instance cached somewhere (which is what we're currently doing) or pay the cost every time we want to generate a float.What type of application is this? (E.g. cryptography, game, numerical simulation)
Games.
Feature request
Provide a new distribution,
Closed01
, similar toOpen01
andOpenClosed01
, that generatesf32
s andf64
s in the range[0, 1]
.This would complete the set of distributions covering this range:
Standard
: sampling in[0, 1)
Open01
: sampling in(0, 1)
OpenClosed01
: sampling in(0, 1]
Closed01
, sampling in[0, 1]
The text was updated successfully, but these errors were encountered: