2005-01-29 14:18:28 -05:00
|
|
|
! Copyright (C) 2003, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-05-02 00:56:09 -04:00
|
|
|
IN: math USING: kernel ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-25 21:51:47 -05:00
|
|
|
: (random-int-0) ( n bits val -- n )
|
2004-12-29 03:35:46 -05:00
|
|
|
3dup - + 1 < [
|
2004-11-25 21:51:47 -05:00
|
|
|
2drop (random-int) 2dup swap mod (random-int-0)
|
|
|
|
] [
|
2005-01-19 21:01:47 -05:00
|
|
|
2nip
|
2005-08-12 18:02:03 -04:00
|
|
|
] ifte ; inline
|
2004-11-25 21:51:47 -05:00
|
|
|
|
|
|
|
: random-int-0 ( max -- n )
|
2005-09-16 22:47:28 -04:00
|
|
|
1+ dup power-of-2? [
|
2004-11-25 21:51:47 -05:00
|
|
|
(random-int) * -31 shift
|
|
|
|
] [
|
|
|
|
(random-int) 2dup swap mod (random-int-0)
|
2005-08-12 18:02:03 -04:00
|
|
|
] ifte ; inline
|
2004-11-25 21:51:47 -05:00
|
|
|
|
2005-08-19 22:22:15 -04:00
|
|
|
: random-int ( min max -- n )
|
|
|
|
dupd swap - random-int-0 + ; flushable
|