Zulip Chat Archive
Stream: mathlib4
Topic: Bug in randFin
Adam Topaz (Mar 22 2024 at 14:23):
I think there's a bug in docs#Random.randFin (namely, it should say randNat 0 n
as opposed to randNat 0 n.succ
). E.g.
import Mathlib
def foo : RandT IO (Fin 2) := Random.randFin
#eval show IO Unit from do
let mut out := 0
let n := 10000
for _ in [:n] do
if (← IO.runRand foo) == 0 then out := out + 1
println! out
-- 6615
Adam Topaz (Mar 22 2024 at 14:24):
Pinging @Henrik Böving who is the author on that file.
Adam Topaz (Mar 22 2024 at 14:25):
In general with the current implementation I think will come up more often than it should.
Adam Topaz (Mar 22 2024 at 14:25):
assuming one expects the uniform distribution, of course.
Henrik Böving (Mar 22 2024 at 14:33):
I agree yes, that's an off by one.
Adam Topaz (Mar 22 2024 at 14:33):
I guess the real question is whether we want randNat
to be inclusive in the upper bound. What does haskell do?
Henrik Böving (Mar 22 2024 at 14:38):
https://hackage.haskell.org/package/random-1.2.1.2/docs/System-Random.html#g:2 bsaed on the usage here it appears that if you ask haskell for a uniform distribution 1, 6 you get upper and lower bound inclusive
Adam Topaz (Mar 22 2024 at 14:38):
ok, I can open a quick pr with a fix to randFin
then.
Adam Topaz (Mar 22 2024 at 14:42):
Adam Topaz (Mar 22 2024 at 16:09):
@Matthew Ballard has already deligated the PR, but I did add some additional nontrivial tests. @Henrik Böving and Matt -- are you okay with these new tests?
Henrik Böving (Mar 22 2024 at 16:16):
I guess those are fine. They might flipper when we are really unlucky but let's just hope that doesn't happen^^
Adam Topaz (Mar 22 2024 at 16:16):
But an explicit seed is provided, so these tests should be deterministic
Henrik Böving (Mar 22 2024 at 16:17):
Ah I didn't see that while quickly reading the file, then everything is fine :thumbs_up:
Adam Topaz (Mar 22 2024 at 16:20):
great. I'll merge it once it passes CI
Adam Topaz (Mar 22 2024 at 16:20):
... it just failed CI :-/
Adam Topaz (Mar 22 2024 at 16:21):
oh... just because I didn't update the slim check test file!
Last updated: May 02 2025 at 03:31 UTC