     dup miller-rabin
     [ nip ] [ drop next-safe-prime ] if ;
 
+: random-bits* ( numbits -- n )
+    [ random-bits ] keep set-bit ;
+
 : random-safe-prime ( numbits -- p )
-    random-bits next-safe-prime ;
+    1- random-bits* next-safe-prime ;

! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators kernel locals math math.functions math.ranges
random sequences sets combinators.short-circuit ;
IN: math.miller-rabin

<PRIVATE

    1 + 6 divisor? ;

: next-safe-prime-candidate ( n -- candidate )
    next-prime dup safe-prime-candidate?
    [ next-safe-prime-candidate ] unless ;

