Fixing typo in contract and adding random-index

This commit is contained in:
Jay McCarthy 2014-11-27 08:35:41 -05:00
parent 3e9f23a2c8
commit bf97e5907e
3 changed files with 37 additions and 1 deletions

View File

@ -672,6 +672,19 @@ An @tech{enumeration} of S-expressions.
This library defines some library @tech{enumerations} built on
@racketmodname[data/enumerate].
@defproc[(random-index [e enum?])
exact-nonnegative-integer?]{
Returns a random index into @racket[e]. This works regardless of the
size of @racket[e], unlike @racket[(random (size e))].
@examples[#:eval the-eval
(random (size nat/e))
(random (size (below/e 5000000000)))
(random-index nat/e)
(random-index (below/e 5000000000))
]}
@defproc[(permutations-of-n/e [n exact-nonnegative-integer?])
enum?]{

View File

@ -106,7 +106,7 @@
(-> enum? (-> any/c enum?)
enum?)]
[dep2/e
(-> nat? enum? (-> any/c enum?)
(-> extended-nat/c enum? (-> any/c enum?)
enum?)]
[fold-enum
(-> (-> list? any/c enum?)

View File

@ -1,9 +1,30 @@
#lang racket/base
(require racket/contract/base
data/enumerate
math/base
math/distributions
math/number-theory
racket/generator)
;; pick-an-index : ([0,1] -> Nat) ∩ (-> Nat)
(define (random-natural-w/o-limit [prob-of-zero 0.01])
(max (random-natural/no-mean prob-of-zero)
(random-natural/no-mean prob-of-zero)
(random-natural/no-mean prob-of-zero)))
;; random-natural/no-mean : [0,1] -> Nat
(define (random-natural/no-mean prob-of-zero)
(define x (sample (geometric-dist prob-of-zero)))
(define m1 (expt 2 (exact-floor x)))
(define m0 (quotient m1 2))
(random-integer m0 m1))
(define (random-index e)
(define k (size e))
(if (infinite? k)
(random-natural-w/o-limit)
(random-natural k)))
(define (BPP-digits N)
(let loop ([8Pi -8])
(define 8i
@ -168,6 +189,8 @@
(provide
(contract-out
[random-index
(-> enum? exact-nonnegative-integer?)]
[infinite-sequence/e
(-> enum? enum?)]
[permutations/e