Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks for exact values in core/random/bytes #1051

Closed
herwinw opened this issue Aug 9, 2023 · 1 comment
Closed

Checks for exact values in core/random/bytes #1051

herwinw opened this issue Aug 9, 2023 · 1 comment

Comments

@herwinw
Copy link
Member

herwinw commented Aug 9, 2023

The spec core/random/bytes has a few checks for the exact results:

  # Should double check this is official spec
  it "returns the same numeric output for a given seed across all implementations and platforms" do
    rnd = Random.new(33)
    rnd.bytes(2).should == "\x14\\"
    rnd.bytes(1000) # skip some
    rnd.bytes(2).should == "\xA1p"
  end

The documentation of Random contains the following line:

PRNGs are currently implemented as a modified Mersenne Twister with a period of 2**19937-1.

Although it is described as being modified, I can't find any documentation on how it is modified without digging into the Ruby source code. In Natalie, we tried this using std::mt19937. This results in different output and fails the test (see natalie-lang/natalie#919)

Given that there already is a comment about whether this is the official spec, and there is no official documentation on how the MRI implementation actually works except for the code itself, I'm not sure this check should be in the spec.

@eregon
Copy link
Member

eregon commented Aug 10, 2023

It is intentional to test the exact values for Random, e.g. also see

it "returns a float within a given float range" do
Random.new(42).rand(0.0...100.0).should == 37.454011884736246
Random.new(42).rand(-100.0...0.0).should == -62.545988115263754
end

Because it is very valuable when running some Ruby app on another Ruby to actually have the same Random values when given the same seed (e.g. via srand).
I'll just remove that comment.

@eregon eregon closed this as not planned Won't fix, can't repro, duplicate, stale Aug 10, 2023
eregon added a commit that referenced this issue Aug 10, 2023
seven1m pushed a commit to seven1m/ruby_spec that referenced this issue Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants