Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REXX help

Status
Not open for further replies.

tomwaites

Technical User
Nov 30, 2010
1
EU
REXX -

Can someone help please :)

I just need a simple code to output random numbers and letters, about 10 lines but they need to be unique!

i.e.
1234abcd56789efghij

Thanks in advance
 
Here is an quick example, how to generate 10 lines of 20 random characters+digits.
To ensure, that the lines are unique, I marked every generated unique line in stem and by next line I proove if it's in stem or not.
random_lines.rex
Code:
[COLOR=#0000ff]/************** main program *********************/[/color]
nr_lines [COLOR=#804040][b]=[/b][/color] 10   [COLOR=#0000ff]/* nr of lines to be generated   */[/color]
lines_used. [COLOR=#804040][b]=[/b][/color] 0 [COLOR=#0000ff]/* stem for storing lines  */[/color]

i [COLOR=#804040][b]=[/b][/color] 0
[COLOR=#804040][b]do [/b][/color][COLOR=#804040][b]while[/b][/color][COLOR=#804040][b] [/b][/color][COLOR=#008080]([/color]i [COLOR=#804040][b]<[/b][/color] nr_lines[COLOR=#008080])[/color]
  line [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]random_line()[/color]
  [COLOR=#804040][b]if[/b][/color] lines_used.line [COLOR=#804040][b]=[/b][/color] 1 [COLOR=#804040][b]then[/b][/color] 
    [COLOR=#804040][b]nop[/b][/color] [COLOR=#0000ff]/* this line was generated before */[/color]
  [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]do[/b][/color]
    lines_used.line [COLOR=#804040][b]=[/b][/color] 1 [COLOR=#0000ff]/* mark line in stem */[/color]
    lines_used.0 [COLOR=#804040][b]=[/b][/color] lines_used.0 [COLOR=#804040][b]+[/b][/color] 1
    i [COLOR=#804040][b]=[/b][/color] i [COLOR=#804040][b]+[/b][/color] 1
    [COLOR=#804040][b]say[/b][/color] [COLOR=#008080]right([/color] i[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]'. '[/color][COLOR=#804040][b],[/b][/color] 4[COLOR=#008080])[/color] [COLOR=#804040][b]||[/b][/color] line [COLOR=#0000ff]/* print unique line */[/color]
  [COLOR=#804040][b]end[/b][/color] 
[COLOR=#804040][b]end[/b][/color]

[COLOR=#804040][b]exit[/b][/color] 

[COLOR=#0000ff]/******************* functions *******************/[/color]
[COLOR=#008080]random_line [/color][COLOR=#804040][b]:[/b][/color]
  [COLOR=#0000ff]/* creates random line of 20 characters+digits */[/color]
  line [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color]
  nr_char [COLOR=#804040][b]=[/b][/color] 0
  [COLOR=#804040][b]do [/b][/color][COLOR=#804040][b]while[/b][/color][COLOR=#804040][b] [/b][/color][COLOR=#008080]([/color]nr_char [COLOR=#804040][b]<[/b][/color] 20[COLOR=#008080])[/color]
    r [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]random([/color]255[COLOR=#008080])[/color]
    [COLOR=#804040][b]select[/b][/color]
      [COLOR=#804040][b]when[/b][/color] [COLOR=#008080]([/color]r [COLOR=#804040][b]>=[/b][/color] 0 [COLOR=#804040][b]&[/b][/color] r [COLOR=#804040][b]<=[/b][/color] 9[COLOR=#008080])[/color] [COLOR=#804040][b]then[/b][/color] [COLOR=#804040][b]do[/b][/color]
        [COLOR=#0000ff]/* digit 0 to 9 */[/color]
        char [COLOR=#804040][b]=[/b][/color] r
        nr_char [COLOR=#804040][b]=[/b][/color] nr_char [COLOR=#804040][b]+[/b][/color] 1
        line [COLOR=#804040][b]=[/b][/color] line [COLOR=#804040][b]||[/b][/color] char
      [COLOR=#804040][b]end[/b][/color]
      [COLOR=#804040][b]when[/b][/color] [COLOR=#008080]([/color]r [COLOR=#804040][b]>=[/b][/color] 65 [COLOR=#804040][b]&[/b][/color] r [COLOR=#804040][b]<=[/b][/color] 90[COLOR=#008080])[/color] [COLOR=#804040][b]|[/b][/color] [COLOR=#008080]([/color]r [COLOR=#804040][b]>=[/b][/color] 97 [COLOR=#804040][b]&[/b][/color] r [COLOR=#804040][b]<=[/b][/color] 122[COLOR=#008080])[/color] [COLOR=#804040][b]then[/b][/color] [COLOR=#804040][b]do[/b][/color]
        [COLOR=#0000ff]/* character A..Za..z */[/color]
        char [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]D2C([/color]r[COLOR=#008080])[/color]
        nr_char [COLOR=#804040][b]=[/b][/color] nr_char [COLOR=#804040][b]+[/b][/color] 1
        line [COLOR=#804040][b]=[/b][/color] line [COLOR=#804040][b]||[/b][/color] char
      [COLOR=#804040][b]end[/b][/color]
      [COLOR=#804040][b]otherwise[/b][/color]
        [COLOR=#804040][b]nop[/b][/color] 
    [COLOR=#804040][b]end[/b][/color] [COLOR=#0000ff]/* select */[/color] 
  [COLOR=#804040][b]end[/b][/color] [COLOR=#0000ff]/* while */[/color]
  [COLOR=#804040][b]return[/b][/color] line
Output
Code:
 1. eRQZ7bJ1kdkE3GXo7rWz
 2. vf0YAc3ix2AagjEhrC7n
 3. ZaSplIT5fL2CHnVn3aHH
 4. cLIW3aS9pzNXe7Snv5Lp
 5. Nr2X4OxZNNs2Q9ezlFeo
 6. FKl06G1jVp0t6ZgMHSwe
 7. dPjd1tkLW0R2u2wqgdyw
 8. oLZFU5MjxvUIkjWS2Rgf
 9. ZbAIjD55QqPUUJORbTYo
10. AZu5oseMYj40nAIkcbgQ
 
I simplified the function random_line() :
Code:
[COLOR=#0000ff]/************** main program *********************/[/color]
nr_lines [COLOR=#804040][b]=[/b][/color] 10   [COLOR=#0000ff]/* nr of lines to be generated   */[/color]
lines_used. [COLOR=#804040][b]=[/b][/color] 0 [COLOR=#0000ff]/* stem for storing lines  */[/color]

i [COLOR=#804040][b]=[/b][/color] 0
[COLOR=#804040][b]do [/b][/color][COLOR=#804040][b]while[/b][/color][COLOR=#804040][b] [/b][/color][COLOR=#008080]([/color]i [COLOR=#804040][b]<[/b][/color] nr_lines[COLOR=#008080])[/color]
  line [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]random_line()[/color]
  [COLOR=#804040][b]if[/b][/color] lines_used.line [COLOR=#804040][b]=[/b][/color] 1 [COLOR=#804040][b]then[/b][/color] 
    [COLOR=#804040][b]nop[/b][/color] [COLOR=#0000ff]/* this line was generated before */[/color]
  [COLOR=#804040][b]else[/b][/color] [COLOR=#804040][b]do[/b][/color]
    lines_used.line [COLOR=#804040][b]=[/b][/color] 1 [COLOR=#0000ff]/* mark line in stem */[/color]
    lines_used.0 [COLOR=#804040][b]=[/b][/color] lines_used.0 [COLOR=#804040][b]+[/b][/color] 1
    i [COLOR=#804040][b]=[/b][/color] i [COLOR=#804040][b]+[/b][/color] 1
    [COLOR=#804040][b]say[/b][/color] [COLOR=#008080]right([/color] i[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]'. '[/color][COLOR=#804040][b],[/b][/color] 4[COLOR=#008080])[/color] [COLOR=#804040][b]||[/b][/color] line [COLOR=#0000ff]/* print unique line */[/color]
  [COLOR=#804040][b]end[/b][/color] 
[COLOR=#804040][b]end[/b][/color]

[COLOR=#804040][b]exit[/b][/color] 

[COLOR=#0000ff]/******************* functions *******************/[/color]
[COLOR=#008080]random_line [/color][COLOR=#804040][b]:[/b][/color]
[COLOR=#0000ff]/* creates random line of 20 characters+digits */[/color]
line [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""[/color]
nr_char [COLOR=#804040][b]=[/b][/color] 0
[COLOR=#804040][b]do [/b][/color][COLOR=#804040][b]while[/b][/color][COLOR=#804040][b] [/b][/color][COLOR=#008080]([/color]nr_char [COLOR=#804040][b]<[/b][/color] 20[COLOR=#008080])[/color]
  r [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]random([/color]255[COLOR=#008080])[/color]
  [COLOR=#804040][b]if[/b][/color] [COLOR=#008080]([/color]r [COLOR=#804040][b]>=[/b][/color] 48 [COLOR=#804040][b]&[/b][/color] r [COLOR=#804040][b]<=[/b][/color] 57[COLOR=#008080])[/color] [COLOR=#804040][b]|[/b][/color] [highlight #ffff00][COLOR=#000000],[/color][/highlight]
     [COLOR=#008080]([/color]r [COLOR=#804040][b]>=[/b][/color] 65 [COLOR=#804040][b]&[/b][/color] r [COLOR=#804040][b]<=[/b][/color] 90[COLOR=#008080])[/color] [COLOR=#804040][b]|[/b][/color] [highlight #ffff00][COLOR=#000000],[/color][/highlight]
     [COLOR=#008080]([/color]r [COLOR=#804040][b]>=[/b][/color] 97 [COLOR=#804040][b]&[/b][/color] r [COLOR=#804040][b]<=[/b][/color] 122[COLOR=#008080])[/color] [COLOR=#804040][b]then[/b][/color] [COLOR=#804040][b]do[/b][/color]
      [COLOR=#0000ff]/* character 0..9, A..Z, a..z */[/color]
      char [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]D2C([/color]r[COLOR=#008080])[/color]
      nr_char [COLOR=#804040][b]=[/b][/color] nr_char [COLOR=#804040][b]+[/b][/color] 1
      line [COLOR=#804040][b]=[/b][/color] line [COLOR=#804040][b]||[/b][/color] char
  [COLOR=#804040][b]end[/b][/color]
[COLOR=#804040][b]end[/b][/color] [COLOR=#0000ff]/* while */[/color]
[COLOR=#804040][b]return[/b][/color] line
 
Couldn't resist a post here, this might be faster, and also makes it easier to alter the alowable characters, so it's easy to remove the letter 'o' and number 0 if these characters are too similar, just remove them from the string. Or add specials if you want!

Phil
Code:
/* REXX */
dgts='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
maxlines=10 ; numchars=20
used.=0 ; lines=0
do while lines<maxlines
	code=''
	do numchars ; code=code||substr(dgts,random(length(dgts)-1)+1,1) ; end
	if used.code=0 then do
		used.code=1 ; lines=lines+1 ; say right(lines,length(maxlines),'0')||'. '||code
		end
end
exit 0

Output:
Code:
01. 4YrSEX5Ft0HvS7XqdCAA
02. 74XQKNjZncSveH8r8U0j
03. XNxv3nKlaIHertk0gcc6
04. w1HzeSkIA3v1VsYM3JGN
05. kInJ689toGYb3r70Vw08
06. bu1eLPFm4PHNblPefIQp
07. d1VWMOaUSuYTNBheCdx2
08. 26wGXxxwsehRtFCfg9Jj
09. YmSZ3revpPfM8aAv81ed
10. rnVTGVlLPGR1KpeBgpZh

Press ENTER key to exit...
 
OK so it'll be quicker still if we remove the redundant calls to Length()

Phil

Code:
/* REXX */
dgts='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
maxlines=10 ; numchars=20
used.=0 ; lines=0 ; rmax=length(dgts)-1 ; ixlen=length(maxlines)
do while lines<maxlines
	code=''
	do numchars ; code=code||substr(dgts,random(rmax)+1,1) ; end
	if used.code=0 then do
		used.code=1 ; lines=lines+1 ; say right(lines,ixlen,'0')||'. '||code
		end
end
exit 0
 
Just tested the speed of both progs;-

To generate 50,000 unique 20 digit codes (on my not-so-fast old pc)

My posted code took 72.163 seconds elapsed
Mikrom's posted code took 75.484 seconds elapsed

Just goes to show that however you write your code, Rexx does a good job!
But I did manage to shave a little off the run-time, just a little... ;-)

Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top