File tree 1 file changed +2
-9
lines changed
math-lib/math/private/base
1 file changed +2
-9
lines changed Original file line number Diff line number Diff line change 24
24
[else r]))]))
25
25
26
26
(define random-max 4294967087 )
27
- (define bias-bits (* 2 block-bits))
28
27
29
28
(: random-natural (Integer -> Natural))
30
29
;; Returns a random integer in the interval [0..n)
33
32
[(n . <= . 0 ) (raise-argument-error 'random-natural "Positive-Integer " n)]
34
33
[(n . <= . random-max) (random n)]
35
34
[else
36
- ;; Rejection sampling has rejection probability approaching 1/2 in the worst cases; that is,
37
- ;; when n = 1+2^i for some large-ish integer i
38
- ;; Adding extra bits shrinks the rejection probability to near zero (it approaches
39
- ;; (* 1/2 (expt 2 (- bias-bits)))), at the cost of some bias
40
- ;; The bias starts become detectable after taking (expt 2 bias-bits) samples, which is plenty
41
- (define bits (+ bias-bits (integer-length (- n 1 ))))
42
- (define m (arithmetic-shift 1 bits))
35
+ (define bits (integer-length (- n 1 )))
43
36
(let loop ()
44
- (define r (quotient (* (+ ( random-bits bits) 1 ) n) m ))
37
+ (define r (random-bits bits))
45
38
(if (r . >= . n) (loop) r))]))
46
39
47
40
(: random-integer (Integer Integer -> Integer))
You can’t perform that action at this time.
0 commit comments