8000 Fix int64 random mask by bobzhang · Pull Request #2248 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix int64 random mask #2248

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

Merged
merged 1 commit into from
Jun 11, 2025
Merged

Fix int64 random mask #2248

merged 1 commit into from
Jun 11, 2025

Conversation

bobzhang
Copy link
Contributor

Summary

  • update mask for Rand.int64
  • adjust expected int64 output in README
  • update random_test.mbt accordingly

Testing

  • moon clean
  • moon test

https://chatgpt.com/codex/tasks/task_e_6849493e451c832094e278729b3ed52d

@bobzhang bobzhang requested a review from Copilot June 11, 2025 09:26
Copy link
peter-jerry-ye-code-review bot commented Jun 11, 2025
The original mask implementation was incorrect for generating 63-bit integers

Category
Correctness
Code Snippet
let mask : UInt64 = 1UL << 63 // old code
let mask : UInt64 = (1UL << 63) - 1UL // new code
Recommendation
The new implementation is correct. Consider adding a comment explaining that this creates a mask with 63 ones (0x7FFFFFFFFFFFFFFF)
Reasoning
The old code created a mask with only the highest bit set (0x8000000000000000), which would result in only generating the minimum 64-bit signed integer. The new code creates a mask for the lower 63 bits, which is correct for generating positive integers in the full range.

Consider adding range specifications in comments

Category
Maintainability
Code Snippet
// range [0, 2^63)
Recommendation
Add more specific range information: // range [0, 9223372036854775807] (inclusive), equivalent to [0, 2^63-1]
Reasoning
Explicit range values help developers understand the exact bounds of generated numbers, especially when dealing with large integers where bit manipulation is involved.

Test case update could include range verification

Category
Maintainability
Code Snippet
test "prng" {
assert_eq(r.int64(), 2043189202271773519)
Recommendation
Add additional test cases to verify the bounds:

test "int64_bounds" {
  let r = Rand.from_seed(42)
  let val = r.int64()
  assert(val >= 0)
  assert(val < (1L << 63))
}
**Reasoning**
Single-value testing doesn't verify the range constraints. Adding boundary tests would ensure the implementation maintains its contract across changes.

</details>

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the behavior of the int64 random value generation by revising the bit mask computation and adjusting associated tests and documentation.

  • Updates the int64 mask calculation in random.mbt to compute (1UL << 63) - 1UL
  • Revises the expected int64 output in both random_test.mbt and README.mbt.md
  • Aligns test expectations to the new mask behavior

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
random/random_test.mbt Updated expected int64 output in tests
random/random.mbt Revised bit mask calculation for int64 method
random/README.mbt.md Updated int64 expected output in documentation
Comments suppressed due to low confidence (2)

random/random.mbt:106

  • Update the comment to clearly indicate that this mask extracts the lower 63 bits, ensuring that it accurately reflects the new implementation and intended behavior.
let mask : UInt64 = (1UL << 63) - 1UL

random/random_test.mbt:25

  • Verify that the updated expected int64 value aligns precisely with the deterministic output of the new mask computation in Rand.int64 to avoid future test inconsistencies.
assert_eq(r.int64(), 2043189202271773519)

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 7212

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.087%

Totals Coverage Status
Change from base Build 7210: 0.0%
Covered Lines: 8551
Relevant Lines: 9186

💛 - Coveralls

@bobzhang bobzhang force-pushed the codex/update-int64-mask-and-tests branch from bcad894 to 6adff7b Compare June 11, 2025 10:17
@bobzhang bobzhang enabled auto-merge (rebase) June 11, 2025 10:17
@bobzhang bobzhang merged commit 998dd5e into main Jun 11, 2025
8 of 12 checks passed
@bobzhang bobzhang deleted the codex/update-int64-mask-and-tests branch June 11, 2025 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0