Feature #17351
closedDeprecate Random::DEFAULT
Description
From https://bugs.ruby-lang.org/issues/17322#note-11
I think we should deprecate the Random::DEFAULT
constant, it doesn't make sense anymore and it's longer than using Random class methods (Random.rand) or Kernel instance methods (#rand).
Also, people might expect it to be global.
If users want a Random instance they should just use Random.new
, not assume there is a global instance in Random::DEFAULT, which is actually rather misleading now (Random::DEFAULT is no longer an instance of Random).
Also note that JRuby & TruffleRuby use a per-thread instance for Kernel#rand, etc, to avoid contention (otherwise it becomes a huge source of contention when threads run in parallel).
Which means on those implementations using Random::DEFAULT was inefficient (extra synchronization).
So for all these reasons I think it's time to deprecate Random::DEFAULT
and then later remove it (in 3.1?).
I don't think there is any use case for Random::DEFAULT
, but happy to hear if there is and there is no trivial replacement.
Updated by Eregon (Benoit Daloze) over 3 years ago
- Related to Feature #17322: Deprecate `Random::DEFAULT` and introduce `Random.default()` method to provide Ractor-supported default random generator added
Updated by ko1 (Koichi Sasada) over 3 years ago
I don't think there is any use case for Random::DEFAULT, but happy to hear if there is and there is no trivial replacement.
As mentioned here: https://bugs.ruby-lang.org/issues/17322#note-7
there are several users.
For example, I will use Random::DEFAULT
for the default random generator for dice rolling method like:
def roll rnd = Random::DEFAULT
rnd.rand(6)+1
end
for hobby use (non-serious use case).
Updated by ko1 (Koichi Sasada) over 3 years ago
I don't against to remove Random::DEFAULT
, but need to care about current users.
def roll rnd = nil
(rnd ? rnd.rand(6) : rand(6)) + 1
end
is one way, but I think some people can like previous definition.
Updated by Eregon (Benoit Daloze) over 3 years ago
It seems pretty rare to need to supply a custom Random instance.
But in such a case, it would be easy to create an explicit one in e.g. the constructor:
class Dice
def initialize(random = Random.new)
@random = random
end
def roll
@random.rand(6) + 1
end
end
If it needs to scale and run well with parallel threads, the built-in Kernel#rand
is probably the only good option.
Updated by matz (Yukihiro Matsumoto) over 3 years ago
OK, accepted.
Matz.
Updated by Eregon (Benoit Daloze) over 3 years ago
- Status changed from Open to Closed
Applied in changeset git|c183288754fdad17e627c4182de599d965e99405.
Remove references to Random::DEFAULT in the documentation
- Random::DEFAULT is no longer a Random instance, and referencing it is
more confusing than helpful. Related to [Feature #17351]
Updated by Eregon (Benoit Daloze) almost 3 years ago
Just realized, another way to write that roll method (without deprecation) is:
def roll rnd = Random
rnd.rand(6)+1
end
I also noticed deprecated constants are no longer warned by default as they depend on Warning[:deprecated]
.