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!

random generator 2

Status
Not open for further replies.

richardsae

Programmer
Jul 29, 2004
3
CA
Hi,
Is there any random number generator function in 4GL ??

Thanks
 
BigCalm is, generally, correct. But if you are running the Korn shell, it provides a RANDOM variable returning a number between 0 and 32767. Use this kludge:

First, create shell script random.sh:

#!/bin/ksh

echo $RANDOM > /tmp/random.txt
# end script

Second, the stub 4GL program executes the random.sh script and loads the number into a temp table:

DATABASE testdb

MAIN
DEFINE ret_int, tmp_val INTEGER,
cmd CHAR(60)

CREATE TEMP TABLE random_tmp
(
qty INTEGER
) WITH NO LOG

RUN "random.sh" RETURNING ret_int
LOAD FROM "/tmp/random.txt" INSERT INTO random_tmp
SELECT * INTO tmp_val FROM random_tmp

END MAIN
# end 4GL program

If you are thinking of performing

RUN "echo $RANDOM > /tmp/random.txt"

don't bother; it won't work. Evidently, when the RUN command spawns a shell, it's the Bourne shell and /bin/sh doesn't support RANDOM.

Regards,

Ed
 
olded, and this ?
RUN "ksh -c 'echo $RANDOM' > /tmp/random.txt"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

Here is my version of sql to generate random number, based on date + time + session id. If you need smaller number, you may remove date part from the select list, but do include session id, because it is unique for each execution.

select substr(current year to day ,1,4)||
substr(current year to day ,6,2)||
substr(current year to day ,9,2)||
substr(current hour to second ,1,2)||
substr(current hour to second ,4,2)||
substr(current hour to second ,7,2)||
dbinfo('sessionid')
from systables where tabid=1;

Regards,
Shriyan
 
Hi Olded,

Thank you very much for your help.

Is there any way to create an Alpha numeric random
number ? e.g. AF12TY, ADR789.
Thanks in advance.
 
richardsae:

First, PHV's suggestion was a good one. (That's why he gets a star. Shriyan's looks interesting. I'll have to look into it.) Using it eliminates the need for an external shell script.

As for your second question, the shell variable RANDOM all by itself will not include alphas. As I said, it returns an integer 0 to 32767. However, if you want to write a shell script that loads a shell array with the proper characters and create some sort of lookup into the array, that should work.

That's a lot of work. Fortunately, author Randal K. Michael in his book "Mastering Unix Shell Scripting" has already done something like this. It's in Chapter 10 "Creating Pseudo-Random Passwords" where he covers the script. Perhaps you could modify it to fit your needs?

Anyway, you can download the source for the entire book free at:


Regards,

Ed
 
Hi Ed, PHV, Bigcalm & vpshriyan,

Thank you very much for all of your help.



 
Hi,

Here is my version of sql to generate/create an alpha numeric random number, without resorting into shell layers!

You can tweak it to generate as many alphas or numerals as you wish to see.

select
substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ',
substr(lpad(dbinfo('sessionid'),10,'0'),10,1)+1*2,2)||
substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ',
substr(current hour to second ,8,1)+1*2,2)||
dbinfo('sessionid')
from systables
where tabid=1 ;

Regards,
Shriyan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top