From 8a5411cfc1400fba5b972249c821c77f3d4d79fa Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Wed, 27 Feb 2013 14:05:46 -0800 Subject: [PATCH] random: performance improvements to random-integer and random-bits. --- basis/random/random.factor | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/basis/random/random.factor b/basis/random/random.factor index 5571fccd3d..7d2bfc55a8 100644 --- a/basis/random/random.factor +++ b/basis/random/random.factor @@ -46,21 +46,24 @@ TYPED: random-bytes ( n: fixnum -- byte-array: byte-array ) ] [ [ 32 shift obj random-32* + ] [ 32 + ] [ 32 - ] tri* - ] while drop ; + ] while drop [ n * ] [ 2^ /i ] bi* ; inline : (random-integer) ( n obj -- n' ) - [ dup next-power-of-2 log2 ] dip ((random-integer)) - [ * ] [ 2^ /i ] bi* ; + [ dup #bits ] dip (random-bits) ; : random-integer ( n -- n' ) random-generator get (random-integer) ; PRIVATE> -: random-bits ( numbits -- r ) 2^ random-integer ; +: random-bits ( numbits -- r ) + [ 2^ ] keep random-generator get (random-bits) ; : random-bits* ( numbits -- n ) 1 - [ random-bits ] keep set-bit ;