setseed accepts bad seeds

Started by Kris Jurkaabout 19 years ago9 messagesbugs
Jump to latest
#1Kris Jurka
books@ejurka.com

Why doesn't setseed complain when given a seed value outside of its
expected range? The documentation claims that 0 -> 1 should be used,
but shouldn't it actually be -1 -> 1 to get the full range of the seed?
If passed a value outside this range you always get the same value which
isn't going to produce a good random number stream.

Kris Jurka

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kris Jurka (#1)
Re: setseed accepts bad seeds

Kris Jurka <books@ejurka.com> writes:

Why doesn't setseed complain when given a seed value outside of its
expected range?

Why should it complain? The use of the value is totally unspecified
anyway.

regards, tom lane

#3Kris Jurka
books@ejurka.com
In reply to: Tom Lane (#2)
Re: setseed accepts bad seeds

On Wed, 11 Apr 2007, Tom Lane wrote:

Kris Jurka <books@ejurka.com> writes:

Why doesn't setseed complain when given a seed value outside of its
expected range?

Why should it complain? The use of the value is totally unspecified
anyway.

Because the user is likely using it incorrectly. I'm not sure what you
mean by totally unspecified. The documentation[1]http://www.postgresql.org/docs/8.2/static/functions-math.html#FUNCTIONS-MATH-FUNC-TABLE states:

set seed for subsequent random() calls (value between 0 and 1.0)

When a user calls setseed(5), setseed(500), or setseed(-500) they get the
same seed value each time which is surely not what they intended. At
minimum I think it should raise a warning.

Also I think that documentation should be corrected to indicate that
vaules -1 to 1 are the correct seed value range or it should it should map
0->1 to the entire seed space, not just half of it as is currently done.
The decision of which change to make is unclear because it's a change to
either the call signature or to the generated values for a given user
supplied seed.

Kris Jurka

[1]: http://www.postgresql.org/docs/8.2/static/functions-math.html#FUNCTIONS-MATH-FUNC-TABLE

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kris Jurka (#3)
Re: setseed accepts bad seeds

Kris Jurka <books@ejurka.com> writes:

On Wed, 11 Apr 2007, Tom Lane wrote:

Kris Jurka <books@ejurka.com> writes:

Why doesn't setseed complain when given a seed value outside of its
expected range?

Why should it complain? The use of the value is totally unspecified
anyway.

Because the user is likely using it incorrectly.

It's not really possible to use it "incorrectly", AFAICS. Any value you
might pass to it will result in a specific new seed value. Nowhere is
there any guarantee of what the mapping is, and it's obviously
impossible to guarantee that the mapping is one-to-one, so any user
assumptions about what a specific seed value might "mean" seem broken
regardless.

regards, tom lane

#5Kris Jurka
books@ejurka.com
In reply to: Tom Lane (#4)
Re: setseed accepts bad seeds

On Wed, 11 Apr 2007, Tom Lane wrote:

It's not really possible to use it "incorrectly", AFAICS. Any value you
might pass to it will result in a specific new seed value. Nowhere is
there any guarantee of what the mapping is, and it's obviously
impossible to guarantee that the mapping is one-to-one, so any user
assumptions about what a specific seed value might "mean" seem broken
regardless.

Then please consider this patch which checks the range and maps the
provided value to the entire seed space.

Kris Jurka

Attachments:

setseed.patchtext/plain; charset=US-ASCII; name=setseed.patchDownload+12-11
#6Bruce Momjian
bruce@momjian.us
In reply to: Kris Jurka (#5)
Re: setseed accepts bad seeds

This has been saved for the 8.4 release:

http://momjian.postgresql.org/cgi-bin/pgpatches_hold

---------------------------------------------------------------------------

Kris Jurka wrote:

On Wed, 11 Apr 2007, Tom Lane wrote:

It's not really possible to use it "incorrectly", AFAICS. Any value you
might pass to it will result in a specific new seed value. Nowhere is
there any guarantee of what the mapping is, and it's obviously
impossible to guarantee that the mapping is one-to-one, so any user
assumptions about what a specific seed value might "mean" seem broken
regardless.

Then please consider this patch which checks the range and maps the
provided value to the entire seed space.

Kris Jurka

Content-Description:

[ Attachment, skipping... ]

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://www.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kris Jurka (#5)
Re: setseed accepts bad seeds

Kris Jurka <books@ejurka.com> writes:

On Wed, 11 Apr 2007, Tom Lane wrote:

It's not really possible to use it "incorrectly", AFAICS. Any value you
might pass to it will result in a specific new seed value. Nowhere is
there any guarantee of what the mapping is, and it's obviously
impossible to guarantee that the mapping is one-to-one, so any user
assumptions about what a specific seed value might "mean" seem broken
regardless.

Then please consider this patch which checks the range and maps the
provided value to the entire seed space.

I'm still not very happy about this. It'll change the behavior of
existing applications, in the service of no goal that I consider
convincing.

I'd be inclined to leave the mapping alone and just insert a warning
(or hard error) for inputs outside the range -1 to 1.

regards, tom lane

#8Kris Jurka
books@ejurka.com
In reply to: Tom Lane (#7)
Re: setseed accepts bad seeds

On Mon, 10 Mar 2008, Tom Lane wrote:

I'd be inclined to leave the mapping alone and just insert a warning
(or hard error) for inputs outside the range -1 to 1.

Here's a patch that errors out for out of range values.

Kris Jurka

Attachments:

setseed-range-check.patchtext/plain; charset=US-ASCII; name=setseed-range-check.patchDownload+10-6
#9Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kris Jurka (#8)
Re: setseed accepts bad seeds

Kris Jurka <books@ejurka.com> writes:

On Mon, 10 Mar 2008, Tom Lane wrote:

I'd be inclined to leave the mapping alone and just insert a warning
(or hard error) for inputs outside the range -1 to 1.

Here's a patch that errors out for out of range values.

Applied, thanks.

regards, tom lane