advice on weighted random selection - Mailing list pgsql-general

From Jeff Davis
Subject advice on weighted random selection
Date
Msg-id 200303091340.30391.jdavis-pgsql@empires.org
Whole thread Raw
In response to Re: BLOBS (ODBC lo object) and pg_restore  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: advice on weighted random selection
List pgsql-general
I'd like to select a random record from a table, but I'd like to weight it.

For example, let's say I have a table like:

data1         data2       weight
----------------------------------------
1                  2              3.44
3                  4              0.94
5                  6              1.00
7                  8              2.00

I would like to select (data1,data2) from a random record, but I'd like (1,2)
to be 3.44 times as likely as (5,6). I would also like (7,8) to be 2 times as
likely as (5,6), and (1,2) to be 1.72 times as likely as (7,8).

Does that make mathematical sense? I think I can do it if weight is an integer
(but it would be a really bad hack), but I can't think of a nice way with
fractions.

Thanks,
    Jeff Davis

pgsql-general by date:

Previous
From: Oliver Elphick
Date:
Subject: Re: Replacing rpm installation with tarball
Next
From: Matt Johnson
Date:
Subject: Re: cross tab confusion