Skip to content

Fix randInt to handle invalid range and include max#9

Merged
mmalcek merged 1 commit into
mmalcek:mainfrom
spetr:find-errors-in-the-code
Sep 6, 2025
Merged

Fix randInt to handle invalid range and include max#9
mmalcek merged 1 commit into
mmalcek:mainfrom
spetr:find-errors-in-the-code

Conversation

@spetr

@spetr spetr commented Sep 6, 2025

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI review requested due to automatic review settings September 6, 2025 16:47

Copilot AI left a comment

Copy link
Copy Markdown

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 fixes the randInt function to properly handle edge cases and ensure correct range behavior. The function now validates invalid ranges and includes the maximum value in the possible outputs.

  • Added validation for invalid ranges where max ≤ min
  • Fixed the range calculation to include the maximum value by adding 1 to the range
  • Added comprehensive test cases for edge cases including equal min/max and invalid ranges

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
functions.go Updated randInt function with range validation and inclusive max behavior
functions_test.go Added test cases for edge cases and seeded random for deterministic testing

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread functions_test.go
result := randInt(50, 55)
if result < 50 || result > 55 {
t.Errorf("result: %v", result)
rand.Seed(1)

Copilot AI Sep 6, 2025

Copy link

Choose a reason for hiding this comment

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

Using rand.Seed() is deprecated as of Go 1.20. Consider using rand.New(rand.NewSource(1)) to create a local random generator or use the newer rand.NewPCG() for better randomness.

Suggested change
rand.Seed(1)
r := rand.New(rand.NewSource(1))
randInt := func(min, max int) int {
if min >= max {
return min
}
return r.Intn(max-min+1) + min
}

Copilot uses AI. Check for mistakes.

@mmalcek mmalcek left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

ok

@mmalcek mmalcek merged commit 72409a0 into mmalcek:main Sep 6, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants