Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: random string

From: joel:neely:fedex at: 11-Apr-2002 6:15

Hi, Andrew, Andrew Martin wrote:
> John wrote: > > Is there a better way of creating a random string of 8 > > characters... > > How about: > > Allowed_Characters: "ABCDEF123" > copy/part random Allowed_Characters 8 >
That produces a random permutation from "ABCDEF123", *not* a string of characters each drawn from "ABCDEF123". IOW a much smaller sample space, since e.g. the result of
>> loop 8 [append "" random/only allowed_characters]
== "CD1AC3DB" could never appear (due to the duplicated appearance of the #"D"). If C is then number of candidate characters and W is the desired string width, then we're dealing with C! / (C-W)! possibilities for "copy/part ..." versus C ** W for "append ... random ..." which, for the sample above, means only
>> 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 == 362880
possible permutations, but
>> 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 == 43046721
possible "fair random with replacement" selections. -jn- -- ; Joel Neely joeldotneelyatfedexdotcom REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] { | e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]