File tree Expand file tree Collapse file tree 2 files changed +8
-8
lines changed Expand file tree Collapse file tree 2 files changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -95,10 +95,10 @@ def randBound (α : Type u)
95
95
(BoundedRandom.randomR lo hi h : RandGT g _ _)
96
96
97
97
/-- Generate a random `Fin`. -/
98
- def randFin {n : Nat} [RandomGen g] : RandGT g m (Fin n.succ ) :=
99
- fun ⟨g⟩ ↦ pure <| randNat g 0 n |>.map (Fin.ofNat' _ ) ULift.up
98
+ def randFin {n : Nat} [NeZero n] [ RandomGen g] : RandGT g m (Fin n) :=
99
+ fun ⟨g⟩ ↦ pure <| randNat g 0 (n - 1 ) |>.map (Fin.ofNat' n ) ULift.up
100
100
101
- instance {n : Nat} : Random m (Fin n.succ ) where
101
+ instance {n : Nat} [NeZero n] : Random m (Fin n) where
102
102
random := randFin
103
103
104
104
/-- Generate a random `Bool`. -/
@@ -113,10 +113,10 @@ instance {α : Type u} [ULiftable m m'] [Random m α] : Random m' (ULift.{v} α)
113
113
114
114
instance : BoundedRandom m Nat where
115
115
randomR lo hi h _ := do
116
- let z ← rand (Fin (hi - lo).succ )
116
+ let z ← rand (Fin (hi - lo + 1 ) )
117
117
pure ⟨
118
118
lo + z.val, Nat.le_add_right _ _,
119
- Nat.add_le_of_le_sub' h (Nat.le_of_succ_le_succ z.isLt)
119
+ Nat.add_le_of_le_sub' h (Nat.le_of_lt_add_one z.isLt)
120
120
⟩
121
121
122
122
instance : BoundedRandom m Int where
Original file line number Diff line number Diff line change @@ -89,7 +89,7 @@ info: GOOD
89
89
IO.runRandWith 257 <| do
90
90
let mut count := 0
91
91
for _ in [:10000 ] do
92
- if (← randFin (n := 2 ) ) == 1 then count := count + 1
92
+ if (← randFin : Fin 3 ) == 1 then count := count + 1
93
93
if Float.abs (0 .333 - (count / Nat.toFloat 10000 )) < 0 .01 then
94
94
IO.println "GOOD"
95
95
else
@@ -103,7 +103,7 @@ info: GOOD
103
103
IO.runRandWith 257 <| do
104
104
let mut count := 0
105
105
for _ in [:10000 ] do
106
- if (← randFin (n := 1 ) ) == 0 then count := count + 1
106
+ if (← randFin : Fin 2 ) == 0 then count := count + 1
107
107
if Float.abs (0 .5 - (count / Nat.toFloat 10000 )) < 0 .01 then
108
108
IO.println "GOOD"
109
109
else
@@ -117,7 +117,7 @@ info: GOOD
117
117
IO.runRandWith 257 <| do
118
118
let mut count := 0
119
119
for _ in [:10000 ] do
120
- if (← randFin (n := 9 ) ) == 5 then count := count + 1
120
+ if (← randFin : Fin 10 ) == 5 then count := count + 1
121
121
if Float.abs (0 .1 - (count / Nat.toFloat 10000 )) < 0 .01 then
122
122
IO.println "GOOD"
123
123
else
You can’t perform that action at this time.
0 commit comments