Re: random number generator
Posted in 1995
sco@mchr.nteltec.com (Stephanie) writes:
>I have need in a 4GL program to aquire some random numbers.
>I have tried to use the UNIX random number generator and I am
>getting off the wall numbers in return. Does anyone out there
>have an informix random number generator that they would be
>willing to share?
How random is random? I find that pullind SECONDS or FRACTION from
CURRENT is sually random enough, but if not, here's how we called the c
srand function from 4GL for a project a couple years ago:
=======================================================================
Dennis J. Pimple dennisp@informix.com Opinions expressed
Senior Consultant -------------------- are mine, and do not
Informix Software Inc Voice: 303-850-0210 necessarily reflect
Denver Colorado USA Fax: 303-779-4025 those of my employer.
#! /bin/sh
# This is a shell archive. Remove anything before this line, then feed it
# into a shell via "sh file" or similar. To overwrite existing files,
# type "sh file -c".
# The tool that generated this appeared in the comp.sources.unix newsgroup;
# send mail to comp-sources-unix@uunet.uu.net if you want that tool.
# If this archive is complete, you will see the following message at the end:
# "End of shell archive."
# Contents: grand.4gl grand.c
# Wrapped by dennisp@puma on Sat Dec 31 21:58:24 1994
PATH=/bin:/usr/bin:/usr/ucb ; export PATHif test -f 'grand.4gl' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \\"'grand.4gl'\\"
else
echo shar: Extracting \\"'grand.4gl'\\" \\(757 characters\\)
sed "s/^X//" >'grand.4gl' <<'END_OF_FILE'
X# an example of using the grand c function to get a random number
X
XDEFINE rseed SMALLINT # random seed number; 1-60
XDEFINE stos DATETIME SECOND TO SECOND # used to get the rseed
XDEFINE ctos CHAR(2)
XDEFINE loopct SMALLINT # number of loops made
XDEFINE igrand INTEGER
XDEFINE offset FLOAT
X
XFOR loopct = 1 TO 10
X # get the random seed number from the seconds tick of the clock
X LET stos = CURRENT
X LET ctos = stos
X LET rseed = ctos CLIPPED
X LET rseed = rseed + 1
X # get the random number from the c function
X LET igrand = grand(rseed,loopct)
X # igrand is a number between 0 and 32,767
X LET igrand = igrand + 1
X # I need a number > 0 and <= 1
X LET offset = igrand / 32768
XEND FOR
END_OF_FILE
if test 757 -ne `wc -c <'grand.4gl'`; then
echo shar: \\"'grand.4gl'\\" unpacked with wrong size!
fi
# end of 'grand.4gl'
fi
if test -f 'grand.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \\"'grand.c'\\"
else
echo shar: Extracting \\"'grand.c'\\" \\(860 characters\\)
sed "s/^X//" >'grand.c' <<'END_OF_FILE'
X/*--------------------------------------------------------------------
X Arguments: # of arguments passed from 4gl CALL == 2
X Purpose: to generate a random number
X Returns: random number
X 4GL Invocation: CALL grand(seed,iteration_count)
X RETURNING random_number
X OR LET random_number = grand(seed,iteration_count)
X where "seed" is a random number starting point
X AND iteration_count is the nth invocation of rand() after seeding
X AND "random number" is random number between 0 and 2-to-the-15th-minus1
X -------------------------------------------------------------------- */
X
Xgrand(nargs)
Xint nargs;
X{
Xint rand(),imaseed,imactr;
Xvoid srand();
Xpopint(&imactr); /* get 4gl variable into iteration counter */
Xpopint(&imaseed); /*get 4gl variable into seed*/
Xsrand(imaseed); /* seed it */
Xwhile (imactr-- >1)
X rand();
Xretint(rand());
Xreturn(1);
X}
END_OF_FILE
if test 860 -ne `wc -c <'grand.c'`; then
echo shar: \\"'grand.c'\\" unpacked with wrong size!
fi
# end of 'grand.c'
fi
echo shar: End of shell archive.
exit 0