You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
math/rand and math/rand/V2 have functions that return various int and float types, but no function that returns a bool. While this would be very easy to implement on our own, I think it's an oversight for the packages.
rand.Bool() could represent a coin flip, which is a very common need in random value generation and modeling.
M
unread,
May 5, 2026, 11:06:43 PM (7 days ago) May 5
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-nuts
func (r *Rand) IntN(n int) intcomes awfully close, with n=2.
Randall O'Reilly
unread,
May 6, 2026, 6:13:50 AM (7 days ago) May 6
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to M, golang-nuts
Because Go does not automatically convert int to bool, having the bool return type is critical for simple conditional expressions. This is the version I use, giving a boolean with given probability:
func BoolP(p float64) bool {
return rand.Float64() < p
}