Fix conflict

db4
Slava Pestov 2008-03-28 23:37:42 -05:00
commit daa6855dae
9 changed files with 72 additions and 45 deletions

View File

@ -1,6 +1,6 @@
USING: vocabs.loader sequences system USING: vocabs.loader sequences system
random random.mersenne-twister combinators init random random.mersenne-twister combinators init
namespaces ; namespaces random ;
"random.mersenne-twister" require "random.mersenne-twister" require
@ -9,5 +9,6 @@ namespaces ;
{ [ unix? ] [ "random.unix" require ] } { [ unix? ] [ "random.unix" require ] }
} cond } cond
! [ [ 32 random-bits ] with-secure-random <mersenne-twister> random-generator set-global ]
[ millis <mersenne-twister> random-generator set-global ] [ millis <mersenne-twister> random-generator set-global ]
"generator.random" add-init-hook "generator.random" add-init-hook

View File

@ -1,24 +0,0 @@
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types kernel math namespaces sequences
io.backend io.binary combinators system vocabs.loader ;
IN: random.backend
SYMBOL: insecure-random-generator
SYMBOL: secure-random-generator
SYMBOL: random-generator
GENERIC: seed-random ( tuple seed -- )
GENERIC: random-32* ( tuple -- r )
GENERIC: random-bytes* ( n tuple -- bytes )
M: object random-bytes* ( n tuple -- byte-array )
swap [ drop random-32* ] with map >c-uint-array ;
M: object random-32* ( tuple -- n ) 4 random-bytes* le> ;
ERROR: no-random-number-generator ;
M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
M: f random-32* ( obj -- * ) no-random-number-generator ;

View File

@ -1,4 +1,4 @@
USING: kernel random math accessors random.backend ; USING: kernel random math accessors random ;
IN: random.dummy IN: random.dummy
TUPLE: random-dummy i ; TUPLE: random-dummy i ;

View File

@ -1,5 +1,5 @@
USING: kernel math random namespaces random.mersenne-twister USING: kernel math random namespaces random.mersenne-twister
sequences tools.test random.backend ; sequences tools.test ;
IN: random.mersenne-twister.tests IN: random.mersenne-twister.tests
: check-random ( max -- ? ) : check-random ( max -- ? )

View File

@ -4,7 +4,7 @@
! http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c ! http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
USING: arrays kernel math namespaces sequences system init USING: arrays kernel math namespaces sequences system init
accessors math.ranges new-effects random.backend ; accessors math.ranges new-effects random ;
IN: random.mersenne-twister IN: random.mersenne-twister
<PRIVATE <PRIVATE

View File

@ -0,0 +1,44 @@
USING: help.markup help.syntax math random.backend ;
IN: random
ARTICLE: "random-numbers" "Generating random integers"
"The " { $vocab-link "random" } " vocabulary implements the ``Mersenne Twister'' pseudo-random number generator algorithm."
{ $subsection random } ;
ABOUT: "random-numbers"
HELP: seed-random
{ $values { "tuple" "a random number generator" } { "seed" "an integer between 0 and 2^32-1" } }
{ $description "Seed the random number generator." }
{ $notes "Not supported on all random number generators." } ;
HELP: random-32*
{ $values { "tuple" "a random number generator" } { "r" "an integer between 0 and 2^32-1" } }
{ $description "Generates a random 32-bit unsigned integer." } ;
HELP: random-bytes*
{ $values { "n" "an integer" } { "tuple" "a random number generator" } { "bytes" "a sequence of random bytes" } }
{ $description "Generates a byte-array of random bytes." } ;
HELP: random
{ $values { "seq" "a sequence" } { "elt" "a random element" } }
{ $description "Outputs a random element of the sequence. If the sequence is empty, always outputs " { $link f } "." }
{ $notes "Since integers are sequences, passing an integer " { $snippet "n" } " yields a random integer in the interval " { $snippet "[0,n)" } "." } ;
HELP: random-bytes
{ $values { "n" "an integer" } { "bytes" "a random integer" } }
{ $description "Outputs an integer with n bytes worth of bits." } ;
HELP: random-bits
{ $values { "n" "an integer" } { "r" "a random integer" } }
{ $description "Outputs an random integer n bits in length." } ;
HELP: with-random
{ $values { "tuple" "a random generator" } { "quot" "a quotation" } }
{ $description "Calls the quotation with the random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
HELP: with-secure-random
{ $values { "quot" "a quotation" } }
{ $description "Calls the quotation with the secure random generator in a dynamic variable. All random numbers will be generated using this random generator." } ;
{ with-random with-secure-random } related-words

View File

@ -1,12 +1,29 @@
! Copyright (C) 2008 Doug Coleman. ! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: alien.c-types kernel math namespaces sequences USING: alien.c-types kernel math namespaces sequences
io.backend io.binary combinators system vocabs.loader io.backend io.binary combinators system vocabs.loader ;
random.backend random.mersenne-twister init ;
USE: prettyprint
IN: random IN: random
: random-bytes ( n -- r ) SYMBOL: insecure-random-generator
SYMBOL: secure-random-generator
SYMBOL: random-generator
GENERIC: seed-random ( tuple seed -- )
GENERIC: random-32* ( tuple -- r )
GENERIC: random-bytes* ( n tuple -- byte-array )
M: object random-bytes* ( n tuple -- byte-array )
swap [ drop random-32* ] with map >c-uint-array ;
M: object random-32* ( tuple -- r ) 4 random-bytes* le> ;
ERROR: no-random-number-generator ;
M: f random-bytes* ( n obj -- * ) no-random-number-generator ;
M: f random-32* ( obj -- * ) no-random-number-generator ;
: random-bytes ( n -- byte-array )
[ [
dup 4 rem zero? [ 1+ ] unless dup 4 rem zero? [ 1+ ] unless
random-generator get random-bytes* random-generator get random-bytes*
@ -29,13 +46,3 @@ IN: random
: with-secure-random ( quot -- ) : with-secure-random ( quot -- )
>r secure-random-generator get r> with-random ; inline >r secure-random-generator get r> with-random ; inline
{
{ [ windows? ] [ "random.windows" require ] }
{ [ unix? ] [ "random.unix" require ] }
} cond
[
[ 32 random-bits ] with-secure-random
<mersenne-twister> random-generator set-global
] "random" add-init-hook

View File

@ -1,5 +1,5 @@
USING: alien.c-types io io.files io.nonblocking kernel USING: alien.c-types io io.files io.nonblocking kernel
namespaces random.backend io.encodings.binary singleton init namespaces random io.encodings.binary singleton init
accessors ; accessors ;
IN: random.unix IN: random.unix

View File

@ -1,6 +1,5 @@
USING: accessors alien.c-types byte-arrays continuations USING: accessors alien.c-types byte-arrays continuations
kernel windows windows.advapi32 init namespaces kernel windows windows.advapi32 init namespaces random ;
random.backend ;
IN: random.windows IN: random.windows
TUPLE: windows-crypto-context handle ; TUPLE: windows-crypto-context handle ;