Skip to content

Commit

Permalink
Add top-level N()
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmutant committed Aug 2, 2023
1 parent d64f7d2 commit c0f8905
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rand_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ package rand

import "math"

type integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// N returns a uniformly distributed pseudo-random number in [0, n).
// The type parameter I can be any integer type. N panics if n <= 0.
//
// When r is nil, N uses non-deterministic goroutine-local pseudo-random data source,
// and is safe for concurrent use from multiple goroutines.
func N[I integer](r *Rand, n I) I {
if n <= 0 {
panic("invalid argument to N")
}
if r == nil {
return I(Uint64n(uint64(n)))
} else {
return I(r.Uint64n(uint64(n)))
}
}

// ShuffleSlice pseudo-randomizes the order of the elements of s.
//
// When r is nil, ShuffleSlices uses non-deterministic goroutine-local
Expand Down

0 comments on commit c0f8905

Please sign in to comment.