Skip to content

SecureRandom.random_number is always 0 #7586

@duritong

Description

@duritong
require 'securerandom'

arr = []
(1..100).each do |i|
  r = SecureRandom.random_number(10)
  puts "#{i}: #{r}"
  arr << r
end

puts arr.uniq

# there might still be the one case in the
# universe where we would have a 100times the
# same number. But it's probably fine as an
# indication...
exit (arr.uniq.length > 1 ? 0 : 1)

Always returns 0.

According to @headius

It appears to be a bug in some very old code used to generate a random int or long by calling bytes on the self object, but it does not handle the bit math properly to produce the final value. This logic was not in use for SecureRandom until we moved to the securerandom gem, which depends on these callbacks to work properly.

See ruby/setup-ruby#448 & duritong/trocla#75 for more background

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