Subject Re: selecting a random record within the db
Author Adam
--- In firebird-support@yahoogroups.com, "rebel_rob_98"
<rebel_rob_98@y...> wrote:
> Hello All,
>
> I would like to select a random record from a table and return the
> results on a web page.

Random, I assume you mean uniform distribution. To achieve this goal,
you will need to know the record count of the dataset you are
selecting, and a select count(*) is not very efficient. For smallish
tables it is fine.

I can do this with ASP however it requires a
> couple of trips to the database (which I am hoping to avoid).

> I was
> hoping to accomplish this selection within a view or stored procedure.

You will need to use a Stored Procedure to do this.
You will also need to find (or write) a UDF to do this. I know rfunc
(rfunc.sourceforge.com) has one but I have not used it.

The basic stored procedure would be:

1. Run the select count(*) and store it in a variable n
2. Using your UDF function, generate a random number between 0 and n-1
and store it in a variable m
3. Run the SQL:

select first 1 skip :n
from ....

(I do not know whether you can use a parameter inside the skip. If you
can't then there is still the option of storing your query in a varchar
and using execute statement to do it).

> However, I can not find any information regarding the availability of
> rnd() or random() for firebird.

There is no built-in random number generator, BUT it is not very hard
to add your own.


Adam