Skip to content

Array#sample does not honor random byte gen #7607

@headius

Description

@headius

This case is from CRuby's test_rand.rb, test_random_ulong_limited_no_rand:

  def test_random_ulong_limited_no_rand
    c = Class.new do
      undef rand
      def bytes(n)
        "\0"*n
      end
    end
    gen = c.new.extend(Random::Formatter)
    assert_equal(1, [1, 2].sample(random: gen))
  end

The problem here is that the random: passed in still gets used by calling rand which produces a random number, rather than using the bytes method to generate a random number {which should be the same always due to the constant return value).

This broke along with other changes to Random to support the SecureRandom gem and user-defined random byte generation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions