rust-random / rand

A Rust library for random number generation.
https://crates.io/crates/rand
Other
1.67k stars 431 forks source link

Higher quality (0, 1] floats #1346

Closed vks closed 1 month ago

vks commented 1 year ago

Background

Motivation: It's possible to get higher-quality floats without having to add a loop.

Application: I don't have a concrete application, but this approach is able to generate floats < 2^-53, and does not generate 0, which should have a probability of 2^-1075. It can also generate more distinct floats than our current approach.

Feature request

Implement another (0, 1] distribution.

dhardy commented 1 year ago

So it uses a maximum of two steps, a bit like Canon's method. Might be generally preferable to #531, but probably still has a significant cost overhead?

At any rate, it may be worth investigating (implementing and benchmarking at least), but not something I'm going to put on my to-do list.

josephlr commented 1 year ago

Initial benchmarks for f64 on the OpenClosed01 distrubution (test distr_openclosed01_f64):

EDIT: testing done on a Zen3 x86_64 processor, but I didn't pass -C target-cpu=native, so rep bsf was being used instead of tzcnt. Rerunning with -C target-cpu=native seemed to make all the microbenchmarks slower, even the existing implementation, which is odd.

dhardy commented 1 year ago

Thanks. Overhead there is not negligible but is small enough that it could be offered as an alternative to the current implementation under a feature flag, if there is genuine interest in using it.