Project

General

Profile

Actions

Feature #17351

closed

Deprecate Random::DEFAULT

Added by Eregon (Benoit Daloze) about 4 years ago. Updated about 3 years ago.

Status:
Closed
Target version:
-
[ruby-core:101124]

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.


Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #17322: Deprecate `Random::DEFAULT` and introduce `Random.default()` method to provide Ractor-supported default random generatorClosedActions
Actions #1

Updated by Eregon (Benoit Daloze) about 4 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) about 4 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) about 4 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) about 4 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) about 4 years ago

OK, accepted.

Matz.

Actions #6

Updated by Eregon (Benoit Daloze) about 4 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) about 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].

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0