utils: Reimplement and type in-random.

This commit is contained in:
Sergiu Ivanov 2022-02-15 00:14:52 +01:00
parent b36d8adaa4
commit 87c80bb6ef
3 changed files with 62 additions and 50 deletions

View File

@ -6,7 +6,8 @@
"rackunit-typed"
"typed-graph"
"typed-racket-lib"
"typed-compose"))
"typed-compose"
"typed-racket-stream"))
(define build-deps '("racket-doc"
"typed-racket-doc"
"sandbox-lib"

View File

@ -400,6 +400,33 @@ a couple conversions.
(lists-transpose '((a b) (1 2 3) (#t)))
]}
@defproc*[([(in-random) (Sequenceof Flonum)]
[(in-random [k Integer]) (Sequenceof Nonnegative-Fixnum)]
[(in-random [min Integer] [max Integer]) (Sequenceof Nonnegative-Fixnum)])]{
Generates a stream of (inexact) random numbers. The meaning of the arguments
is the same as for the function @racket[random]:
@itemlist[
@item{@racket[(in-random)] — a stream of random inexact numbers between
0 and 1,}
@item{@racket[(in-random k)] — a stream of random exact integers in the range
@racket[0] to @racket[k]-1.}
@item{@racket[(in-random min max)] — a stream of random exact integers the
range @racket[min] to @racket[max]-1.}
]
@examples[#:eval utils-evaluator
(require typed/racket/stream)
(stream->list (stream-take (in-random) 5))
(stream->list (stream-take (in-random 10) 5))
(stream->list (stream-take (in-random 5 10) 5))
]}
@section{Functions and procedures}
@section{Randomness}

View File

@ -11,7 +11,7 @@
;;; Typed section.
(module typed typed/racket
(require typed/graph typed/rackunit typed-compose
(require typed/graph typed/rackunit typed-compose typed/racket/stream
(for-syntax syntax/parse racket/list))
(provide
@ -24,7 +24,7 @@
list-sets->list-strings pretty-print-set pretty-print-set-sets
update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets ht-values/list->set hash->list/ordered
multi-split-at lists-transpose)
multi-split-at lists-transpose in-random)
(define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -454,6 +454,35 @@
(module+ test
(test-case "lists-transpose"
(check-equal? (lists-transpose '((1 2) (a b))) '((1 a) (2 b)))))
(: in-random (case->
(-> (Sequenceof Flonum))
(-> Integer (Sequenceof Nonnegative-Fixnum))
(-> Integer Integer (Sequenceof Nonnegative-Fixnum))))
(define in-random
(case-lambda
[() (stream-cons (random) (in-random))]
[(k) (stream-cons (random k) (in-random k))]
[(min max) (stream-cons (random min max) (in-random min max))]))
(module+ test
(test-case "in-random"
(random-seed 1)
(check-equal? (stream->list (stream-take (in-random 100) 10))
'(50 84 10 99 94 88 43 41 63 50))
(check-equal? (stream->list (stream-take (in-random 50 100) 10))
'(57 98 82 83 61 53 73 82 50 80))
(check-equal? (stream->list (stream-take (in-random) 10))
'(0.2718099186980313
0.7319496826374751
0.17365244033739616
0.5593031443038616
0.3345256691289459
0.9845704615094365
0.05753824253751768
0.22552976312818723
0.21646500425988832
0.15188352823997242))))
)
(require 'typed)
@ -464,17 +493,13 @@
list-sets->list-strings pretty-print-set pretty-print-set-sets
update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets ht-values/list->set hash->list/ordered
multi-split-at lists-transpose)
multi-split-at lists-transpose in-random)
;;; Untyped section.
(provide
;; Functions
(contract-out [in-random (case-> (-> (stream/c (and/c real? inexact? (>/c 0) (</c 1))))
(-> (integer-in 1 4294967087) (stream/c exact-nonnegative-integer?))
(-> exact-integer? (integer-in 1 4294967087)
(stream/c exact-nonnegative-integer?)))]
[cartesian-product/stream (->* () #:rest (listof stream?) stream?)]
(contract-out [cartesian-product/stream (->* () #:rest (listof stream?) stream?)]
[boolean-power (-> number? (listof (listof boolean?)))]
[boolean-power/stream (-> number? (stream/c (listof boolean?)))]
[any->01 (-> any/c (or/c 0 1))]
@ -499,47 +524,6 @@
(cons/c key-contract val-contract)))
;;; ==========
;;; Randomness
;;; ==========
;;; Generates a stream of inexact random numbers. The meaning of the
;;; arguments is the same as for the function random:
;;;
;;; (in-randoms k) — a sequence of random exact integers in the range
;;; 0 to k-1.
;;;
;;; (in-randoms min max) — a sequence of random exact integers the
;;; range min to max-1.
;;;
;;; (in-randoms) — a sequence of random inexact numbers between
;;; 0 and 1.
(define in-random
(case-lambda
[() (for/stream ([i (in-naturals)]) (random))]
[(k) (for/stream ([i (in-naturals)]) (random k))]
[(min max) (for/stream ([i (in-naturals)]) (random min max))]))
(module+ test
(test-case "in-random"
(random-seed 0)
(check-equal? (stream->list (stream-take (in-random 100) 10))
'(85 65 20 40 89 45 54 38 26 62))
(check-equal? (stream->list (stream-take (in-random 50 100) 10))
'(75 59 82 85 61 85 59 64 75 53))
(check-equal? (stream->list (stream-take (in-random) 10))
'(0.1656109603231493
0.9680391127132195
0.051518813640790355
0.755901955353936
0.5923534604277275
0.5513340634474264
0.7022057040731392
0.48375400938578744
0.7538961707172924
0.01828428516237329))))
;;; ===========================
;;; Additional stream utilities
;;; ===========================