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

proposal: math/rand/v2: Add IntInRange function #63880

Open
knbr13 opened this issue Nov 1, 2023 · 2 comments
Open

proposal: math/rand/v2: Add IntInRange function #63880

knbr13 opened this issue Nov 1, 2023 · 2 comments
Labels
Milestone

Comments

@knbr13
Copy link

knbr13 commented Nov 1, 2023

Proposal

This proposal suggests adding a new function, IntInRange(low, high int) int, to the math/rand package.
The function generates random integers within a specified range [low, high).

Function Implementation

func IntInRange(low, high int) int {
    return rand.Intn(high-low) + low
}

Rationale

  • It simplifies random integer generation within a specific range, making it easier especially for beginners.
  • Provides a clean and concise code for this common task.
@knbr13 knbr13 added the Proposal label Nov 1, 2023
@gopherbot gopherbot added this to the Proposal milestone Nov 1, 2023
@mateusz834
Copy link
Member

mateusz834 commented Nov 1, 2023

Probably at this point this should be a proposal for math/rand/v2 (#61716).

@leb-kuchen
Copy link

leb-kuchen commented Dec 24, 2023

Generics make more sense for me, with and N and RangeN.

func RangeN[I integer](low, high I) I {
	return I(Uint64n(uint64(high-low)) + uint64(low))
}

@seankhliao seankhliao changed the title proposal: math/rand: Add IntInRange function proposal: math/rand/v2: Add IntInRange function Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants