Skip to content

CHANGE: rename Rng methods random, gen_* #1503

Closed
@dhardy

Description

@dhardy

#989 discusses the addition of methods like rand::range, which leads back to Rng::gen_range.

#1438 and #1500 already renamed genrandom and gen_iterrandom_iter.

Since we no longer have Rng::gen, should we rename gen_range, gen_bool and gen_ratio?

Aim: make method/function names consistent, memorable and easily comprehensible.

Proposals

Old (v0.8)

Before gen was deprecated, we had:

rng.gen()
rng.gen_range(0..len)
rng.gen_bool(0.2)
rng.gen_ratio(2, 3)
rng.sample(Open01)
rng.sample_iter(Open01)
rng.fill(&mut buf)
rng.try_fill(&mut buf)

Status quo

If we make no further changes, we have the following Rng methods:

rng.random()
rng.random_iter()
rng.sample(Open01)
rng.sample_iter(Open01)
rng.fill(&mut buf)
rng.gen_range(..len)
rng.gen_bool(0.2)
rng.gen_ratio(2, 3)
#[deprecated] rng.gen()

gen_bool → p; drop gen prefix

We could just:

rng.range(..len)
rng.bool(0.2)
rng.ratio(2, 3)

And yes, it appears that we can use bool as a method name (it's a primitive type, not a keyword).

gen_bool → p; drop gen prefix

This is the most concise change:

rng.range(..len)
rng.p(0.2)
rng.ratio(2, 3)

Is it clear enough that rng.p(p) is generating a random variate where P(X = true) = p?

Some weird variations on this:

rng.bool_p(0.2)
rng.bool_ratio(2, 3)

rng.true_p(0.2)
rng.true_ratio(2, 3)

gen_ → random_

A straightforward replacement gives us:

rng.random()
rng.random_iter()
rng.random_range(..len)
rng.random_p(0.2)
rng.random_ratio(2, 3)

This seems fine, if a bit long.

random → next

We already have RngCore::next_u32 etc., so we could rename random (what was gen) to next:

rng.next()
rng.next_iter()
rng.next_range(..len)
rng.next_p(0.2)
rng.next_ratio(2, 3)

Caveat 1: this no longer matches rand::random (which we probably don't want to rename to rand::next).

Caveat 2: rng.next_iter() is a weird name; we can't really use rng.iter() however since it generates an iterator over Standard samples, not an abstract iterator over the RNG.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions