random names
PARM varname, minlen=4, maxlen=8 # This script returns in the variable specified as "varname" a `random’# name consisting of letters and numbers - cannot start with a number. # At least "minlen" characters long and not more than "maxlen" chars. ## expression for a `random' letter: setvar letter "chr( (hpcpumsecs mod 26) + ord('A') )” ## expression for a `random' number: setvar number "chr((hpcpumsecs mod 10) + ord('0'))" ## first character must be a letter setvar !varname !letter ## now fill in the rest, must have at least "minlen" chars , up to "maxlen" setvar i 1 setvar limit min( (hpcpumsecs mod !maxlen) + !minlen, !maxlen) while setvar(i,i+1) <= limit do if odd(hpcpumsecs) then setvar !varname !varname + !letter else setvar !varname !varname + !number endif endwhile
Notes:
- Script on jazz at: http://jazz.external.hp.com/src/scripts/randname.txt
- This example shows a script returning a value via a passed in variable.
- Shows using HPCPUMSECS to get a sort of pseudo random number.
- Breaking down the line: setvar letter "chr( (hpcpumsecs mod 26) + ord('A') )”
- HPCPUMSECS returns some large number
- mod 26 returns a number in the range of 0..25
- ord(“A”) is 65 and is the decimal number of the letter “A”
- chr(0..25 + 65) is chr(65..90), which is one of the letters A..Z
- The same logic applies to the “number” line above.
- The LIMIT line is evaluated as (using the parameter default values):
- (hpcpumsecs mod 8) is a number in the range of 0..7
- + minlen makes the number in the range 4..11
- min(4..11, 8) returns a pseudo random number in the range of 4..8, which is exactly what is desired.
- The WHILE loop iterates “limit-1” times, filling in the 2nd through “limit” characters in the name. If the HPCPUMSECS value is odd at this moment we append to the name a “random” letter, else a “random” number is appended.
- It would be nice to have a pseudo random number and name generator in the CI core, IMO!