]> gitweb.factorcode.org Git - factor.git/commitdiff
random-string: make letters and numbers equiprobable
authorJon Harper <jon.harper87@gmail.com>
Sat, 28 Jul 2012 00:43:43 +0000 (02:43 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 5 Aug 2012 00:32:33 +0000 (17:32 -0700)
basis/random/data/data.factor

index f15306552704091026608fcbd8c000365203f64c..81ed1d20cc3821ddd6e9d30a72f62fd3739f72c0 100644 (file)
@@ -1,20 +1,27 @@
 ! Copyright (C) 2010 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: combinators effects.parser kernel math random
-combinators.random sequences ;
+USING: combinators combinators.random effects.parser kernel
+literals math random sequences ;
 IN: random.data
 
+CONSTANT: digits-count 10
+CONSTANT: letters-count 26
+
 : random-digit ( -- ch )
-    10 random CHAR: 0 + ;
+    digits-count random CHAR: 0 + ;
 
-: random-LETTER ( -- ch ) 26 random CHAR: A + ;
+: random-LETTER ( -- ch ) letters-count random CHAR: A + ;
 
-: random-letter ( -- ch ) 26 random CHAR: a + ;
+: random-letter ( -- ch ) letters-count random CHAR: a + ;
 
 : random-Letter ( -- ch )
     { random-LETTER  random-letter } execute-random ;
 
+CONSTANT: digit-probability $[ letters-count 2 * digits-count / 1 + recip ]
 : random-ch ( -- ch )
-    { random-digit random-Letter } execute-random ;
+    {
+      { $ digit-probability [ random-digit ] }
+      [ random-Letter ]
+    } casep ;
 
 : random-string ( n -- string ) [ random-ch ] "" replicate-as ;