11-23-2007, 07:50 PM
Member function str.Random. Create it using menu File New New Member Function.
function minlen maxlen [$charset]
;Creates random string.
;minlen, maxlen - minimal and maximal number of characters that must be in the string.
;charset - characters that must be in the string.
;;;By default are included all characters between ASCII 33 and 127. This does not include space characters.
;;;Use hyphen between two characters to include all characters whose ASCII codes are more than of the first character and less than of the second.
;EXAMPLES
;str s
;s.Random(5 5) ;;5 characters ASCII 33-127
;out s
;s.Random(8 16 "a-zA-Z0-9") ;;8 to 16 alphanumeric characters
;s.Random(8 8 "[1]-[255]") ;;8 any characters
this.all(Uniform(minlen maxlen) 2)
int i j c
if(!len(charset))
,for(i 0 this.len) this[i]=Uniform(33 127)
else
,str s(charset) ss
,;replace hyphens
,for i s.len-2 0 -1
,,if(s[i]='-')
,,,c=s[i+1]-s[i-1]-1
,,,if(c<0) continue
,,,ss.all(c 2)
,,,c=s[i-1]
,,,for(j 0 ss.len) c+1; ss[j]=c
,,,s.replace(ss i 1)
,,,i-2
,;find min and max char
,int minchar(255) maxchar(1)
,for(i 0 s.len)
,,if(s[i]<minchar) minchar=s[i]
,,if(s[i]>maxchar) maxchar=s[i]
,;generate random chars between min and max, and reject chars not in charset
,for(i 0 this.len)
,,rep
,,,c=Uniform(minchar maxchar)
,,,if(findc(s c)>=0) this[i]=c; break