smol-rs / fastrand

A simple and fast random number generator
Apache License 2.0
386 stars 33 forks source link

Uniform and normal distributions #66

Closed pnevyk closed 1 year ago

pnevyk commented 1 year ago

Hi,

What do you think about adding APIs for generating values from uniform and normal distributions? I personally think that these two are so common and generally useful that it might be worthy to add them into the crate. Uniform distribution is essentially the same as hypothetical {f32,f64}_range functions mentioned in #18.

If this feature is acceptable, I would gladly submit a PR.

notgull commented 1 year ago

It depends. The main goal of fastrand is to be a small, efficient RNG library. I don't want it to get too bloated.

I'm wondering if it would be better to create a fastrand-contrib crate with all of these extra functions, which can probably be implemented in terms of fastrand's current API.

pnevyk commented 1 year ago

The main goal of fastrand is to be a small, efficient RNG library. I don't want it to get too bloated.

Yeah, I thought so and I am in favor of that. Although I was thinking only about uniform and normal distributions, if the crate introduced those, anyone could then ask for more, so probably better to not give a precedent.

A separate crate makes sense to me. In the end, it is what rand crate also has in form of rand_distr. I am fine with making the implementations, but I assume you (smol-rs) would like to be the owner of such a crate?

notgull commented 1 year ago

I've created the new fastrand-contrib repository, not yet finished or published yet. Over the next month or so I'll spruce it up and publish it to crates.io.

pnevyk commented 1 year ago

Great, thank you! Should I provide the implementations for f{32,64}_range and f{32,64}_normal (or any other name), perhaps as a PR to the new repo, or do you want to do it yourself? Naive f{32,64}_range is trivial, but there are techniques that work even if max - min overflows the float's max value. For normal distribution sampling I use Box-Muller transform.

notgull commented 1 year ago

Go ahead and make a PR, I'll review and accept it.

notgull commented 1 year ago

This is now exposed in fastrand-contrib v0.1.0.