SPL: random integer
Posted in 1999
Topics: Stored Procedures & SPL
Hi, We're developing an Web application with DDW, Webdatablade en Informix UDO 9.14 UC6. I'm writing a recursive SPL procedure in which I need a unique integer every time this procedure is invoked (also when it is called by itself). I tried two things: - To use a variable which was created using the 'current' function. But the this date (year to fraction) variable stays the same in the entire procedure call. - Create a temp table wit a serial8. Do inserts an request its number en delete it again. Problem: multi-user application, so the users are intefering with each other, while inserting en selecting. Is there another way to create a random integer. Toine Schijvenaars.
Sorry,
I should have consulted the newsgroup archive first.
On 1997/12/08 Jonathan Leffler wrote:
Since that's the case, we need a proper random number generator. On the
assumption that the C Standard committee knew roughly what they were doing
when they specified a random number generator in the ISO 9899:1990 standard,
here's an implementation of that generator in two functions, sp_setseed(0
and sp_random(). The range of returned values is 0..32767. Note the comment
about MOD. That was in a 9.12.UC2 IUS system.
Yours,
Jonathan Leffler (johnl@informix.com) #include <witticism.h>
-- @(#)$Id: random.spl,v 1.2 1997/12/08 19:31:44 johnl Exp $
--
-- Simple emulation of SRAND and RAND in SPL
-- Using random number generator suggested by C standard (ISO 9899:1990)
CREATE PROCEDURE sp_setseed(n INTEGER)
DEFINE GLOBAL seed DECIMAL(10) DEFAULT 1; LET seed = n;
END PROCEDURE;
CREATE PROCEDURE sp_random() RETURNING INTEGER;DEFINE GLOBAL seed DECIMAL(10) DEFAULT 1;
DEFINE d DECIMAL(20,0);
LET d = (seed * 1103515245) + 12345;
-- MOD function does not handle 20-digit values... Dammit!!
LET seed = d - 4294967296 * TRUNC(d / 4294967296);
RETURN MOD(TRUNC(seed / 65536), 32768);
END PROCEDURE;
----- Original Message -----
From: Toine Schijvenaars <tsch00@pop3.nl.net>
Newsgroups: comp.databases.informix
Sent: Monday, August 30, 1999 1:57 PM
Subject: SPL: random integer
> Hi,
>
> We're developing an Web application with DDW, Webdatablade en Informix UDO
> 9.14 UC6.
>
> I'm writing a recursive SPL procedure in which I need a unique integer
every
> time this procedure is invoked (also when it is called by itself).
> I tried two things:
> - To use a variable which was created using the 'current' function. But
the
> this date (year to fraction) variable stays the same in the entire
procedure
> call.
> - Create a temp table wit a serial8. Do inserts an request its number en
> delete it again. Problem: multi-user application, so the users are
> intefering with each other, while inserting en selecting.
>
> Is there another way to create a random integer.
> Toine Schijvenaars.
>
>
>
>
>
>
>
>
Toine Schijvenaars wrote:
> We're developing an Web application with DDW, Webdatablade en
> Informix UDO 9.14 UC6.
>
> I'm writing a recursive SPL procedure in which I need a unique
> integer every time this procedure is invoked (also when it is
> called by itself).
Usually, you can do this with a defaulted argument, as in:
CREATE PROCEDURE xyz(..., level INTEGER DEFAULT 0) ...
The user is told not to supply a level; the procedure bumps the
level number each time it calls itself. If that is not adequate
for you, then there are alternatives:
> I tried two things:
> - To use a variable which was created using the 'current' function.
> But the this date (year to fraction) variable stays the same in
> the entire procedure call.
There are, apparently, some DBINFO options to return the real time.
> - Create a temp table wit a serial8. Do inserts an request its
> number en delete it again. Problem: multi-user application, so
> the users are intefering with each other, while inserting en
> selecting.
Not if it is a temp table; each user gets their own temp table.
> Is there another way to create a random integer.
Of course. I'm pretty sure there are random number generator
stored procedures in the IIUG archives (http://www.iiug.org).
If not, then contact me at the office and I can send you what
I've got archived in my system and try to get that to the IIUG too.
--
Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net)
Guardian of DBD::Informix v0.60 -- see http://www.perl.com/CPAN
#include <disclaimer.h>
Toine Schijvenaars (tsch00@pop3.nl.net) wrote: : We're developing an Web application with DDW, Webdatablade en Informix UDO : 9.14 UC6. Check out ftp://www.iiug/org/pub/Informix_Archive/pub/random_udr.tar.Z This is a collection of random number generators written in 'C'. They return uniform, binomial and normally distributed pseudo-random values. -- ===================================================================== Paul Brown ^..^ pbrown@postgres.Berkeley.EDU (oo) - Oink! #include <std_disclaimer.h> "Think global - act loco!" - Zippy the Pinhead =====================================================================
You could try using a 'Last number' table and give each invokation of your SPL a successively higher number. If you do use a random function, you will need to program for the possibility of duplicates. Rudy On Mon, 30 Aug 1999 13:57:03 +0200, "Toine Schijvenaars" <tsch00@pop3.nl.net> wrote: >Hi, > >We're developing an Web application with DDW, Webdatablade en Informix UDO >9.14 UC6. > >I'm writing a recursive SPL procedure in which I need a unique integer every >time this procedure is invoked (also when it is called by itself). >I tried two things: >- To use a variable which was created using the 'current' function. But the >this date (year to fraction) variable stays the same in the entire procedure >call. >- Create a temp table wit a serial8. Do inserts an request its number en >delete it again. Problem: multi-user application, so the users are >intefering with each other, while inserting en selecting. > >Is there another way to create a random integer. > Toine Schijvenaars. > > > > > > > >