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

random string

 [1/12] from: john_kenyon::mlc::com::au at: 10-Apr-2002 12:22


Hi, Is there a better way of creating a random string of 8 characters than the folowing? salt-string: copy "" loop 8 [ append salt-string to-char add random 78 48 ] Thanks, John

 [2/12] from: carl:cybercraft at: 10-Apr-2002 19:00


On 10-Apr-02, [john_kenyon--mlc--com--au] wrote:
> Hi, > Is there a better way of creating a random string of 8 characters > than the folowing? > salt-string: copy "" > loop 8 [ append salt-string to-char add random 78 48 ]
Hi John, I'm not sure if this is better from a performance point of view, but it's a little bit shorter anyway... salt-string: rejoin array/initial 8 [to-char add random 78 48] -- Carl Read

 [3/12] from: john_kenyon:mlc:au at: 10-Apr-2002 17:31


Carl, Thanks for the suggestion. I had completely forgotten about array. Cheers, John

 [4/12] from: cyphre:seznam:cz at: 10-Apr-2002 10:48


Hi John, look at following example:
>> rejoin array/initial 8 [(to-char random 25) + 65]
== "GHBEYKRI" this example will generate string containing characters from #"A" to #"Z". Because I didnt understand what characters you want in the string you have to change appropriate value 25(scale of characters) and the offset in ascii table (65) to achieve your wanted result or just write another code into the block. regards, Cyphre

 [5/12] from: cyphre:seznam:cz at: 10-Apr-2002 10:53


Oops! ;) this is the right code for random string of 8 chars from #"A" to #"Z":
>> rejoin array/initial 8 [to-char (random 25) + 64]
== "LBAUXVRH" Anyway, the technique remains the same like in my previous post... regards, Cyphre

 [6/12] from: al:bri:xtra at: 10-Apr-2002 22:45


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 Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [7/12] from: anton:lexicon at: 10-Apr-2002 21:20


Ok, you want to talk about performance? Are you going to use it many millions of times in a loop? Then better do this: salt-string: clear "" loop 8 [ insert tail salt-string to-char add random 78 48 ] Two optimizations: 1) clear, instead of copy, reuses the memory instead of allocating new memory each time. 2) insert tail, instead of append, avoids an extra mezzanine function call. (Just look at the source of append). Anton.

 [8/12] from: joel:neely:fedex at: 10-Apr-2002 6:51


Hi, John, A few suggestions... [john_kenyon--mlc--com--au] wrote:
> Is there a better way of creating a random string of 8 > characters than the folowing? > > salt-string: copy "" > loop 8 [ append salt-string to-char add random 78 48 ] >
I'm assuming that the 8 is more or less accidental; there might be a future need to create random strings of other lengths... If you pre-allocate the string to the desired length, the interpreter won't waste time re-allocating and copying as the string grows. Since APPEND is a mezzanine, you can save time (in *very* small increments, but they add up in deeply nested loops) by directly saying INSERT TAIL instead. RANDOM returns a result that is the same type as its argument, including the CHAR! type. You can add (suitably small ;-) integers to CHAR! values; the result will still be CHAR! typed. It wasn't clear to me why you wanted strings to contain only characters between #"1" and #"~". Is there some application-specific reason? (I assume you're aware that REBOL is 1-origin in philosophy, rather than 0-origin, which means that RANDOM 78 will return a value between 1 and 78 inclusive.) In the example below, I assumed that any printable ASCII (7-bit) characters would do as well. YMMV, especially if you want the Extended Roman characters. Here's a function that combines all of the above... randstr: func [w [integer!] /local r] [ r: make string! w loop w [ insert tail r (random #"^^") + 32 ] r ] which behaves as follows:
>> randstr 8 == "O_YgI,WW" >> randstr 8 == "2h6`kj}$"
<<quoted lines omitted: 5>>
>> randstr 8 == "6m2BqVI3" >> randstr 8 == "uBz<[ZV9"
Hope this helps! -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 ]

 [9/12] from: rebolinth:nodep:dds:nl at: 10-Apr-2002 21:39


Citeren Cyphre <[cyphre--seznam--cz]>: Very nice !!! And the nicest part is "And i have to quite Gregg on this" the Mailing list is always a good checklist ;-) I myself also was working on a random string for passwords but is did not even got close to this small !!! (R)egards, Norman.

 [10/12] from: john_kenyon:mlc:au at: 11-Apr-2002 11:02


Joel, Cyphre and Andrew, Thanks for your replies. I am using this random string as a salt for an sha password created using checksum/secure. This is the password format used by the Netscape LDAP server (and possibly others) hence the 8 characters. I am not sure on the limits of the ascii characters allowed by the LDAP server so I choose a fairly conservative collection of chars. Rebol makes a great tool for creating and parsing LDIF files. It will be nice when command supports LDAP too. Cheers, John

 [11/12] 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 ]

 [12/12] from: al:bri:xtra at: 12-Apr-2002 9:34


> That produces a random permutation from "ABCDEF123", *not* a string of
characters each drawn from "ABCDEF123". Yes. I realised that soon after I posted it. Still it's an alternative, that might be suitable. Andrew Martin ICQ: 26227169 http://valley.150m.com/

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted