Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 23rd, 2005, 02:22 AM
Arnau Rebassa
Guest
 
Posts: n/a
Default Random not so random

Hi everybody,

I'm doing the following query:

select * from messages order by random() limit 1;

in the table messages I have more than 200 messages and a lot of times, the
message retrieved is the same. Anybody knows how I could do a more "random"
random?

Thank you very much

--
Arnau

__________________________________________________ _______________
Consigue aquí las mejores y mas recientes ofertas de trabajo EE.UU.
http://latino.msn.com/empleos


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

  #2  
Old November 23rd, 2005, 02:22 AM
Jean-Luc Lachance
Guest
 
Posts: n/a
Default Re: Random not so random

Use a SERIAL id on messages, then

Select * from messages
where id = int8( random() * currval({sequence_name}));

Arnau Rebassa wrote:
[color=blue]
> Hi everybody,
>
> I'm doing the following query:
>
> select * from messages order by random() limit 1;
>
> in the table messages I have more than 200 messages and a lot of times,
> the message retrieved is the same. Anybody knows how I could do a more
> "random" random?
>
> Thank you very much
>
> --
> Arnau
>
> __________________________________________________ _______________
> Consigue aquí las mejores y mas recientes ofertas de trabajo EE.UU.
> http://latino.msn.com/empleos
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>[/color]

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

  #3  
Old November 23rd, 2005, 02:22 AM
Greg Stark
Guest
 
Posts: n/a
Default Re: Random not so random


"Arnau Rebassa" <arebassa@hotmail.com> writes:
[color=blue]
> select * from messages order by random() limit 1;
>
> in the table messages I have more than 200 messages and a lot of times, the
> message retrieved is the same. Anybody knows how I could do a more "random"
> random?[/color]

What OS is this? Postgres is just using your OS's random()/srandom() calls. On
some platforms these may be poorly implemented and not very random.

However of the various choices available I think random/srandom are a good
choice. I'm surprised you're finding it not very random.

Incidentally, are you reconnecting every time or is it that multiple calls in
a single session are returning the same record? It ought not make a difference
as Postgres is careful to seed the random number generator with something
reasonable though.

In a quick test of my own on linux with glibc 2.3.2.ds1 (no, I have no idea
what the ds1 means) It seems fairly random to me:

test=> create table test4 as (select (select case when b.b then a else a end from test order by random() limit 1) as b from b limit 1000);
SELECT
test=> select count(*),b from test4 group by b;
count | b
-------+---
210 | 5
195 | 4
183 | 3
203 | 2
209 | 1
(5 rows)

And the same thing holds if I test just the low order bits too:

test=> create table test4 as (select (select case when b.b then a else a end from test order by random() limit 1) as b from b limit 1000);
SELECT
test=> select count(*),b from test4 group by b;
count | b
-------+---
249 | 4
241 | 3
259 | 2
251 | 1
(4 rows)

--
greg


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles