Skip site navigation (1) Skip section navigation (2)

Re: [Fwd: Re: surprising results with random()]

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: John R Pierce <pierce(at)hogranch(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [Fwd: Re: surprising results with random()]
Date: 2009-02-24 00:16:02
Message-ID: 28819.1235434562@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-general
John R Pierce <pierce(at)hogranch(dot)com> writes:
> ries van Twisk wrote:
>> should the function not look like this???
>> 
>> CREATE OR REPLACE VIEW test_view AS
>> SELECT
>> CASE
>> WHEN random() < .333333333 THEN '1'
>> WHEN random() < .5 THEN '2'
>> ELSE '3'
>> END AS test_value
>> 
>> FROM client;

> actually, I'd think that should be .66666

Nah, ries is correct.  Think of it this way: one-third of the time the
first WHEN succeeds, and you get '1'.  In *half of the remaining cases*,
you want '2', so the second test should be against 0.5.

> but... wouldn't    floor(random() * 3.0) + 1  be a simpler way to do this?

Agreed...

			regards, tom lane

In response to

pgsql-general by date

Next:From: rafDate: 2009-02-24 00:16:18
Subject: Re: surprising results with random()
Previous:From: Brent WoodDate: 2009-02-23 23:24:05
Subject: Re: surprising results with random()

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group