D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10434 - Don't use Random as template parameter name in std.random (or anywhere else)
Summary: Don't use Random as template parameter name in std.random (or anywhere else)
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-06-20 16:18 UTC by Joseph Rushton Wakeling
Modified: 2017-01-23 07:19 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Joseph Rushton Wakeling 2013-06-20 16:18:36 UTC
Quite a number of functions and objects in std.random use Random as the template parameter name for a random number generator -- e.g. in RandomCover:

    struct RandomCover(Range, Random)
        if(isRandomAccessRange!Range && isUniformRNG!Random)

This is obviously dangerous as it risks accidental confusion with the alias Random used for the default RNG type.

Elsewhere, the very long UniformRandomNumberGenerator is used as a typical parameter name -- descriptive but a pain to retype!

So, I suggest replacing these parameter name with something consistent and more appropriate -- either Rng (which is used in some other places in std.random) or RNG (which I prefer to Rng, but which maybe isn't in line with D style:-)

If there's a desire to stress that a parameter is a _uniform_ random number generator, we could also use Urng or URNG or even UniformRNG, but I think that's overkill as those features of the parameter should be indicated by template constraints, not naming conventions that are bordering on the Hungarian.

We could also take this opportunity to standardize the typical variable name for a RNG -- which in some places is called rnd, in some rng, in some urng, and in some gen.

So, I propose standardizing on RNG or Rng for template parameters (someone tell me which:-) and rng for variable names.
Comment 1 Andrej Mitrovic 2013-06-20 16:23:40 UTC
RandGen could also work.
Comment 2 hsteoh 2013-07-15 16:16:22 UTC
+1 for RandGen, self-documenting and doesn't clash with existing symbol. Rng is a bit obscure for those not in the know.
Comment 3 github-bugzilla 2013-08-29 00:34:59 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8d9233cf8b9e4d27bd70dd0fcd171d2f6dc2f2c0
Partial fix for Issues 7067 and 10434 - std.random.RandomCover

The existing RandomCover design is fatally flawed because it
requires a RNG as input, which it then copies internally by
value.  So, unless the user is smart enough to pass something
like e.g. SomeRNG(unpredictableSeed), there will be unintended
correlations in random behaviour.

This partial fix follows the design of RandomSample in allowing
RandomCover to use the thread-global default RNG rndGen.  It
also improves the choice of template parameter and variable
names in line with Issue 10434.

https://github.com/D-Programming-Language/phobos/commit/2c9ecb8bd146a77c5ca450a69771b0d9dfcdf732
Partial fix for Issue 10434 - std.random.RandomSample

In line with changes to RandomCover, this patch tweaks
the choice of template parameter and variable names in
order to bring clarity and uniformity to the module.
Comment 4 Joseph Rushton Wakeling 2013-08-30 03:02:56 UTC
(In reply to comment #2)
> +1 for RandGen, self-documenting and doesn't clash with existing symbol. Rng is
> a bit obscure for those not in the know.

If you look at the pull which has just landed, I settled on UniformRNG in order to match the isUniformRNG template, with RandomGen as a secondary choice.  I decided this was preferable since the uniformity of the generator _does_ matter.