lvg 发表于 2007-06-01 13:15

存储过程中如何产生随机数?

请教:
      存储过程中如何产生随机数?

wenlq 发表于 2007-06-01 19:00

from iiug faq


4.8.8 How do I generate random numbers in SPL?
On 8th Dec 1997 johnl@informix.com (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.

-- @(#)$Id: ifaq04c.htm,v 1.11 1999/01/09 14:30:06 root 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;

wenlq 发表于 2007-06-01 19:06

http://www.iiug.org/faqs/informix-faq/index.html

查 random

lvg 发表于 2007-06-01 19:18

呵呵 wenlq大哥谢谢
页: [1]
查看完整版本: 存储过程中如何产生随机数?