advice on weighted random selection

From: Jeff Davis <jdavis-pgsql(at)empires(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: advice on weighted random selection
Date: 2003-03-09 21:40:30
Message-ID: 200303091340.30391.jdavis-pgsql@empires.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Matt Johnson 2003-03-09 23:38:25 Re: cross tab confusion
Previous Message Eric B.Ridge 2003-03-09 21:34:43 Re: [GENERAL] division by zero