Home > Archive > AWK > March 2004 > random word generator
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
random word generator
|
|
|
| Hello,
I would like to generate random words consisting of
alphabets and numbers . the generated string is always of length 7
examples :
qwe3rff
kjadsgr
8jndlks
etc
a method i was thinking of was to create an array like this
arr[0]="a"
arr[1]="b"
| |
| Ted Davis 2004-03-19, 8:24 pm |
| On 12 Mar 2004 15:59:29 -0800, usenetdada@yahoo.com (dada) wrote:
>Hello,
>
>I would like to generate random words consisting of
>alphabets and numbers . the generated string is always of length 7
>
>examples :
>
>qwe3rff
>kjadsgr
>8jndlks
>
>etc
>
>a method i was thinking of was to create an array like this
>
>arr[0]="a"
>arr[1]="b"
> .
> .
> .
>arr[25]="z"
>arr[26]="0"
>arr[27]="1"
> .
> .
>arr[33]="9"
>
>and then concatenate the string(which is the random word and initially "") with
>arr[rand()] where rand() picks a value betwwen 0 and 33
>the same thing would be done over 7 times
>but this seems to be a highly inefficeint way and also
>how to sprecify rand() like this
>generate random integers between 0 and 33 ?
>
>Can anybody suggest some method for this.
>thanks
I have found it worth while to write these sorts of things in general
ways. This function is used to generate strings used for various
security purposes, including passwords. It takes the length of the
string as its only real argument. You could fiddle with the character
range code to limit it to lower case, or just use it as is and run the
return through tolower(). The string is really only psuedorandom, of
course.
function RandomString( StringLength, x, c, String ) {
# Returns a mixed case alphanumeric string containing StringLength
# number of characters.
srand()
# Generate an integer between 0 and 200, then discard any that are
# not in one of the desired ranges (48-57, 65-90, and 97-122).
for( x = 1; x <= StringLength; x++ ) {
while( 1 ) {
c = int( 200 * rand() )
if( c < 48 ) continue
if( ( c > 57 ) && ( c < 65 ) ) continue
if( ( c > 90 ) && ( c < 97 ) ) continue
if( c > 122 ) continue
break
}
String = String sprintf( "%c", c )
}
return( String )
}
T.E.D. (tdavis@gearbox.maem.umr.edu - e-mail must contain "T.E.D." or my .sig in the body)
| |
|
| I know it's an awk list, but... I had the same problem long ago, when I
had to generate several passwords. First i thought of using only awk,
but then I found an easier solution. Instead of trying to force awk to use
it's random and convert to chars, rather read bytes (or lines) from
/dev/urandom. It will contain everything from char 0 to char 255; just
drop what you don't need, count how many characters left and exit when you
have 7.
With awk, it's only one process; if you don't have awk for some reason,
with dd and sed (and/or tr) you can do the same: dd can count for you, sed
(and/or tr) can select what characters to keep. Backdraw of this method is
that you have to run more processes.
bye
Igor2
On Sat, 12 Mar 2004, dada wrote:
> Hello,
>
> I would like to generate random words consisting of
> alphabets and numbers . the generated string is always of length 7
>
> examples :
>
> qwe3rff
> kjadsgr
> 8jndlks
>
> etc
>
> a method i was thinking of was to create an array like this
>
> arr[0]="a"
> arr[1]="b"
> .
> .
> .
> arr[25]="z"
> arr[26]="0"
> arr[27]="1"
> .
> .
> arr[33]="9"
>
> and then concatenate the string(which is the random word and initially "") with
> arr[rand()] where rand() picks a value betwwen 0 and 33
> the same thing would be done over 7 times
> but this seems to be a highly inefficeint way and also
> how to sprecify rand() like this
> generate random integers between 0 and 33 ?
>
> Can anybody suggest some method for this.
> thanks
>
|
|
|
|
|