Subject | Re: [firebird-support] How to use rand()? |
---|---|
Author | Nick Upson |
Post date | 2007-12-07T07:11:19Z |
you can do something like this
SET TERM ^ ;
ALTER PROCEDURE GET_RANDOM_FORTUNE
RETURNS (
FORTUNE Varchar(1000) )
AS
num integer;
BEGIN
SELECT count(*)
FROM FORTUNE
INTO :num;
num = rand() * num;
for SELECT FIRST (:num) FORTUNE
FROM FORTUNE
ORDER BY fortune
INTO :FORTUNE;
do
begin
end
SUSPEND;
END^
SET TERM ; ^
SET TERM ^ ;
ALTER PROCEDURE GET_RANDOM_FORTUNE
RETURNS (
FORTUNE Varchar(1000) )
AS
num integer;
BEGIN
SELECT count(*)
FROM FORTUNE
INTO :num;
num = rand() * num;
for SELECT FIRST (:num) FORTUNE
FROM FORTUNE
ORDER BY fortune
INTO :FORTUNE;
do
begin
end
SUSPEND;
END^
SET TERM ; ^
On 07/12/2007, weiwufeng <blackspace@...> wrote:
>
> I want to use rand() function to get a random row from a FORTUNE table.But I always get same a row when I reconnect to my database.
> The function description tell me:"Note the random number generator is seeded using the current time."
> Why I always get same row? How I change the seed?
>
> It is Firebird-2.0.3.12981-1-Win32.exe that I use.
>
> /*****************************************
> *
> * r a n d
> *
> *****************************************
> *
> * Functional description:
> * Returns a random number between 0
> * and 1. Note the random number
> * generator is seeded using the current
> * time.
> *
> *****************************************/
> DECLARE EXTERNAL FUNCTION rand
> RETURNS DOUBLE PRECISION BY VALUE
> ENTRY_POINT 'IB_UDF_rand' MODULE_NAME 'ib_udf';
>
>
> SET TERM ^ ;
> ALTER PROCEDURE GET_RANDOM_FORTUNE
> RETURNS (
> FORTUNE Varchar(1000) )
> AS
> BEGIN
> SELECT FIRST 1 FORTUNE
> FROM FORTUNE
> ORDER BY rand()
> INTO :FORTUNE;
> SUSPEND;
>
> END^
> SET TERM ; ^
>
>
> --
> weiwufeng <blackspace@...>
>
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Visit http://www.firebirdsql.org and click the Resources item
> on the main (top) menu. Try Knowledgebase and FAQ links !
>
> Also search the knowledgebases at http://www.ibphoenix.com
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Yahoo! Groups Links
>
>
>
>