by Barry Rochford
| Barry Rochford, the president of the NY Alpha Users' Group, is an Alpha Developer located in Milford Square, PA. He can be reached at brochford@enter.net. Barry was motivated to write this article in response to a question posed on the Alpha Software User-supported Message Board. |
Recently a person had a problem using the RAND Function to generate a UNIQUE Random Number. Although the RAND() Function does a good job of coming up with a random number, it cannot guarantee that it is unique. That you have to take care of on your own. I thought about it, and it seemed like it should be fairly easy to accomplish.
This person needed to enter a 3 character Prefix, get a unique Random Number, then concatenate the result as a new Account Number. I created a very small table named "MY_TABLE". The table has 3 fields:
The table is Indexed by Account_no, and the index name is "Account_Index".
I created a small, very simple form ("My_form") for the Data Input:
Fig. 1 My_form
The first step was to write down "Pseudo Code" of what had to be done.
If not in ENTER mode then end (You don't want to change an existing number) Get a random number from 1 999999999 (I used 1-15 for testing) Is the random number already on the file? If yes, go back, get another one, otherwise, OK, it's unique, put it in the my_number field on the Form Finished
Pseudo Code
Now that I had my Pseudo Code, I started the Xbasic Script for the "My_number" field CanArrive Event.
''XBasic My_number CanArrive Event
' Barry Rochford 2-7-1999
dim Random_number as n 'raw random number (numeric)
dim test_number as c 'random number, left zero filled, 9 Characters
dim mode as c
dim count_times as n 'counter for error testing
dim reply as n
mode=parentform.mode_get() 'Only calculate if in Entry Mode
if mode<>"ENTER" then
end
end if
count_times=0
get_random_number:
random_number=1+rand()*999999999 'For testing I had a limit of 1 - 15
test_number=padl(ltrim(str(random_number,9,0)),9,"0")
' random_number is numeric, convert it to character and
' left zero fill so it is 9 characters
if exist(test_number,"My_table","number_index")then
' If not unique, get another number
count_times=count_times+1
if count_times>50000 then
reply=ui_msg_box("NOTICE"."Tried 50,000 numbers, none were Unique."+chr(10)+chr(10)+ "You have two options:"+chr(10)+" Press RETRY to try 50,000 more times, or"+chr(10)+" Press CANCEL to quit!",53)
' (NOTE: the above is 1 continuous line in the Code Editor)
if reply=4 then
' If retry was pressed, set counter to zero
count_times=0
else
end
end if
end if
goto get_random_number
end if
my_number.value=test_number
end
I only added a simple test: if after 50,000 tries it did not find a unique number, the Operator has the choice of trying another 50,000 times or just quitting. In a real-life situation, you may want to get a lot more sophisticated. Or you could just do as other Mainframe Programmers (including myself) said in the 60s, 70s & 80s, "who cares if the year is only 2 digits, I won't be working for this Company in the year 2000 anyway."
2/21/99
Don't forget, we need your feedback to make this site better!