1 Random Distributions
This module provides utilities for generating random values.
1.1 Booleans
Produces
#t with probability
p, and
f with
probability
(- 1 p).
Produces #t or #f with equal probability.
Produces
#t with probability
p, and
f with
probability
(- 1 p).
1.2 Numbers
These procedures represent distributions over various subsets of the Scheme
numbers.
Examples: |
> (random-seed 1) | > (build-list 20 (lambda (i) (random-natural))) | '(679 165 2256 7 60 123 841 886 457 688 1836 37 424 385 1499 2716 748 429 4017 499) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-integer))) | '(-165 7 -123 886 -688 37 -385 2716 429 499 311 580 15 1488 1883 -2622 586 395 354 -3998) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-rational))) | '(-282 -841/887 -918/19 -1499/2717 -4017 311/1750 15/2854 1883 994/587 -2140 -3998/1095 -484/791 668/127 76/1265 1469 117/1783 120 -311 1384 425/2489) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-exact))) | '(-282 -841/887-688/1837i -77/300+374/215i -1302 116/219-2853/1489i 130 586 -2140 -3998/1095-560/969i -908 -37/81 809/801+87/325i 159/1106-1171/125i 1834/1385-1684/213i 438 1893 -983/458 1259/666 161/90-53/1344i -118) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-positive-real))) | '(0.5978373153418373 0.10544079461477066 1.065426214170885 1.0458950820656803 1.2602262735583296 0.16518257759654129 0.9620560311285986 3.379590610653502 0.7268962380113153 0.029612850523509173 0.3713505520196463 0.31048000016794686 0.33976813464024447 0.25512483026381727 1.4252039491595079 12.099866935688016 0.6654006959406588 0.46671715760029037 0.16743681623180834 35.09364653846772) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-real))) | '(-8.102437753519679 -1.065426214170885 0.6509199604883179 -0.16518257759654129 -3.048733385695219 0.7268962380113153 2.2319350685211328 -0.31048000016794686 17.11158384560407 1.4252039491595079 -0.19605918316666343 -0.46671715760029037 1.0916879815729899 0.6418003318571719 -1.8476783856271501 -0.7860292236838364 -1.4853973411518142 -0.05042955540346871 0.29858213102621484 5.290089668540595) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-inexact))) | '(-0.10544079461477066-2.0510232372278656i -3.155542799752904 -3.048733385695219+0.7268962380113153i -0.3713505520196463 -0.33976813464024447 1.4252039491595079 0.6654006959406588+5.73356799529654i -0.05471776721450147-1.5036082722103665i -0.7860292236838364 -0.6062879125014674+0.9909767946414353i -0.4245243297674075 0.004466544667114087 -1.8803943633190778 16.343777171383724 0.38189045062571664+0.3708481044313414i -0.3492758929334177+1.1122446171095848i -1.290107161461921-0.699963778818598i 0.8022352854604103 -1.3022964694716193 4.1980008775469075-0.9230057775064026i) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-number))) | '(-282-123/842i -688/1837-212/193i 374/215 -1302 1.6719289307759317 1.0418763249943892+12.099866935688016i -1157/396 -442 651 1581/260+668/127i 35.45887271469893 1469/262+117/1783i 40/781-311/943i 576 -2488 -450/577 -108 857 19/2 629/463-2687/2729i) |
|
Produces an integer chosen uniformly in the range [lo,hi],
inclusive.
Note: out-of-order arguments cause an exception!
> (build-list 10 (lambda (i) (random-integer/uniform 1 -1))) |
random-integer/uniform: contract violation, expected |
<(and/c exact-integer? (>=/c 1))>, given -1, which isn't |
(>=/c 1) |
contract from |
/Users/cce/git/planet/fasttest/random.rkt |
blaming program |
contract: |
(->d ((x ...) (y ...)) () (_ ...)) |
at: /Users/cce/git/planet/fasttest/random.rkt:81.2 |
Produces a natural number from a geometric distribution with minimum value
base and mean
(+ base (/ (- 1 p) p)). The probability of each
possible result
n+base is
(* p (expt (- 1 p) n)).
Produces a natural number with a Poisson distribution, meaning the number of
events occurring in a unit of time if the events occur independently and with an
average rate of r per unit of time.
Produces the difference between two Poisson random variables, chosen with rates
r1 and r2 respectively.
Produces an inexact real number from a uniform distribution in the range
[lo,hi], inclusive.
Examples: |
> (random-seed 1) | > (build-list 20 (lambda (i) (random-real/uniform -1/2 1/2))) | '(0.006730890460318273 0.34760666063571943 -0.3953886859214042 0.4921332104978403 0.4416750166258784 0.3838481765334544 -0.06906964265892412 -0.08797936358016623 0.1329820276378333 0.0022764886900572545 -0.34082722987329206 0.4636171831359096 0.15415791540053825 0.179958229286436 -0.27697025509779644 -0.4340068752117059 -0.027190605563960468 0.1504496373919595 -0.4820350062249418 0.10666208951401401) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-real/uniform 10.0 20.0))) | '(15.067308904603182 18.476066606357193 11.046113140785959 19.921332104978404 19.416750166258783 18.838481765334542 14.309303573410759 14.120206364198339 16.329820276378335 15.022764886900573 11.591727701267079 19.636171831359096 16.541579154005383 16.79958229286436 12.230297449022036 10.659931247882941 14.728093944360396 16.504496373919594 10.179649937750582 16.06662089514014) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-real/uniform 0.05 0.05))) | '(0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05) |
|
Note: out-of-order arguments cause an exception!
> (build-list 10 (lambda (i) (random-real/uniform 0.5 -0.5))) |
random-real/uniform: contract violation, expected <(and/c |
real? (>=/c 0.5))>, given -0.5, which isn't (>=/c 0.5) |
contract from |
/Users/cce/git/planet/fasttest/random.rkt |
blaming program |
contract: |
(->d ((x ...) (y ...)) () (_ ...)) |
at: /Users/cce/git/planet/fasttest/random.rkt:137.2 |
1.3 Textual Data
Produces a random lower-case letter.
Produces a string of length len with characters generated by
make-char.
Examples: |
> (random-seed 1) | > (build-list 20 (lambda (i) (random-string))) | '("kqnezr" "mq" "" "izbf" "joi" "inoj" "uallfu" "xcut" "oa" "qc" "o" "hnyk" "bsxb" "mcyw" "gofkgsd" "fsnuchxj" "ysp" "uhlm" "fo" "rkgjjej") | > (random-seed 1) | | '("BBAB" "BBAA" "BBAB" "BBAA" "ABAB" "ABAB" "ABAA" "AABA" "ABAB" "ABBA" "ABBA" "ABAB" "BBAB" "AAAA" "BABA" "AABA" "BBAA" "ABAB" "AABA" "AABB") |
|
Produces an interned symbol with underlying string string.
Produces an interned keyword with underlying string string.
1.4 Lists and S-expressions
Produces a list of length len of elements generated by thunk.
Examples: |
> (random-seed 1) | > (build-list 20 (lambda (i) (random-list random-string))) | '(("rfbmq" "" "izbf" "joi" "inoj" "uallfu") ("goaqc" "bd" "ihny" "bsxbw") ("vrmyk" "kgs" "zmpnf") ("ysptf" "mcpaf" "hrkgjje" "bxgjyti") () ("gxk" "cttga" "bfjk") ("yxg" "jzw" "jkenzbk" "xyqy" "abwu" "rhk" "ae") ("jfm" "rgg" "w" "mtequxfi" "ncnwz" "pjof") ("sw" "emfy" "qgmk" "mrqgcf" "vr") ("mbm") ("reuwlxtw" "nuk") ("xbtc" "zfp" "yyz" "bvpb" "u" "qoqkgcy") ("si" "llkvg" "glqk" "cry" "atbbedjyy" "q") ("qum" "fhzpvertckml" "bsncig" "ppkh" "jfheftpq" "pz") ("qdpnxht" "i" "ro" "ng" "fwrtrf") ("tao" "llu" "nv" "ek" "xx") ("jotn" "vvblrt" "momlr" "unr" "zrmtnpt") ("t" "zf" "ydnfumc" "zfanoin") ("o" "oymsk" "ro" "yb" "bwqoge") ("" "mbnq" "ji" "csl" "tf" "vumf")) | > (random-seed 1) | > (build-list 20 (lambda (i) (random-list random-string #:len 4))) | '(("kqnezr" "mq" "" "izbf") ("joi" "inoj" "uallfu" "xcut") ("oa" "qc" "o" "hnyk") ("bsxb" "mcyw" "gofkgsd" "fsnuchxj") ("ysp" "uhlm" "fo" "rkgjjej") ("xgjytia" "qnj" "zmg" "ttg") ("rn" "f" "vyyv" "yxgb") ("wqfjjkenz" "pn" "notgab" "rhkpf") ("e" "flgijf" "rggb" "ynzdmt") ("iwur" "cnw" "pjoft" "ppas") ("gemf" "qgmkg" "rqgcfh" "ra") ("gb" "bj" "mreuwlxt" "nukm") ("bzozj" "jcr" "vfay" "bvpbbc") ("qoqkgcyw" "lhcsi" "llkvg" "glqk") ("cry" "atbbedjyy" "q" "snhcqu") ("tcfhzpvertc" "rkebsn" "rn" "pp") ("hvzbjf" "tp" "pzx" "nqlq") ("pnxht" "i" "ro" "ng") ("fwrtrf" "pwbet" "" "lluj") ("n" "ekh" "tiqy" "jotnp")) |
|
Produces a random boolean, symbol, character, number, or string.
Produces a random
cons-tree (if
improper?) or nested
list (if not) containing
size atoms generated by
make-atom.
Examples: |
> (random-seed 1) | > (build-list 10 (lambda (i) (random-sexp))) | '(((#\r) ((#\a)) #\h) (((#f)) (0.6418003318571719)) (-1897/405+1469/262i xcu (qcg)) ((())) (bsxb (#t)) ("gofkgsd" #\o "snuchx") (-0.33383828860756826 cpaf #\u -463/146+410/137i) ((#f #\u)) (24/17 (#\q)) (cttga -97/404 ())) | > (random-seed 1) | | '((((zrrfbmq . ||) . izbf) jo . qai) (((uallfu . xcut) . oa) cb . kli) ((bsxbwh . ywyj) (ofkg . jzmpn) . hxju) ((|| . iuhlmc) (fhrkgjj . souk) . jy) ((|| jcgx . hcttg) bfjk . ktuayxg) (wqfjjkenz hxyq (bwuo . hkp) . a) (flgijf (gbdw . mtequxfi) ncnw . pjoft) ((pas . gemf) (mkgsx . qgcfh) . ra) (mbm vvxr (kcenukmi . bzoz) . cjc) (((ayyz . bvpb) . u) qoqkgc . lhcsivi)) |
|
1.5 Choices
Produces one of its arguments, chosen fairly.
Produces one of the values (
cdr) of
alist, weighted by their
keys (
car).
(random-case clause ...+) |
|
clause | | = | | expr | | | | | | expr #:weight weight |
|
Chooses one of the exprs and evaluates it in tail position. Each
expression is chosen with the associated weight, default 1.
Examples: |
> (random-seed 1) | | Goodbye. | Goodbye. | Hello! | Goodbye. | Goodbye. | Goodbye. | Hello! | Hello! | Goodbye. | Goodbye. | Hello! | Goodbye. | Goodbye. | Goodbye. | Hello! | Hello! | Hello! | Goodbye. | Hello! | Goodbye. |
| (define (random-tree) | (random-case 'leaf #:weight 2 | (list 'node (random-tree) (random-tree)) #:weight 1)) | |
| > (random-seed 1) | > (build-list 20 (lambda (i) (random-tree))) | '(leaf (node leaf (node (node (node leaf leaf) leaf) leaf)) leaf (node leaf (node leaf leaf)) leaf leaf leaf leaf leaf (node leaf leaf) leaf (node leaf leaf) leaf leaf (node leaf leaf) leaf leaf (node leaf (node leaf leaf)) leaf leaf) |
|
1.6 Probabilities
(prob/c) → flat-contract? |
(prob/c inc) → flat-contract? |
inc : (one-of/c 0 1) |
(prob/c zero one) → flat-contract? |
zero : 0 |
one : 1 |
This contract is intended to accept probability values. It accepts real numbers
in the range [0,1], inclusive of zero and/or one if passed as an argument, and
exclusive otherwise.