Skip to content

math/rand,math/rand/v2: ExpFloat64 can sometimes return exactly 0 #65432

@argusdusty

Description

@argusdusty

Go version

go version go1.21.6 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Argusdusty\AppData\Local\go-build
set GOENV=C:\Users\Argusdusty\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=O:\Code\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=O:\Code
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.6
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=O:\Code\src\go.work
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\ARGUSD~1\AppData\Local\Temp\go-build672535778=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://go.dev/play/p/-5HbPTMP5X9

Running ExpFloat64() after a few iterations on a brute-forced seed that triggers this case.

What did you see happen?

ExpFloat64() returns 0.0 exactly.

What did you expect to see?

Per the function's comment: "ExpFloat64 returns an exponentially distributed float64 in the range (0, +math.MaxFloat64]", so a value larger than 0 was expected.

This should occur whenever r.Uint32() at https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/math/rand/exp.go;l=32 returns 0 exactly. This appears to still exist in math/rand/v2.

Note: as maybe a separate issue, ExpFloat64 can also theoretically return +Inf, also not in the range specified by the function comment, when r.Float64() returns exactly 0.0 at https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/math/rand/exp.go;l=39 - though this is extremely unlikely (example of a specially crafted "random" source to trigger a +Inf https://go.dev/play/p/sugD7822wW5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions