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
Index: src/backend/utils/adt/float.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/float.c,v
retrieving revision 1.149
diff -c -r1.149 float.c
*** src/backend/utils/adt/float.c       27 Feb 2007 23:48:08 -0000      1.149
--- src/backend/utils/adt/float.c       11 Apr 2007 18:48:42 -0000
***************
*** 1783,1790 ****
  setseed(PG_FUNCTION_ARGS)
  {
        float8          seed = PG_GETARG_FLOAT8(0);
!       int                     iseed = (int) (seed * MAX_RANDOM_VALUE);
  
        srandom((unsigned int) iseed);
  
        PG_RETURN_VOID();
--- 1783,1800 ----
  setseed(PG_FUNCTION_ARGS)
  {
        float8          seed = PG_GETARG_FLOAT8(0);
!       int                     iseed;
!       
!       if (seed < 0 || seed > 1)
!               elog(WARNING, "setseed parameter %f out of expected range 
[0,1]", seed);
! 
!       /* 
!        * map seed range from [0, 1] to [-1, 1] to get the
!        * full range of possible seed values.
!        */
!       seed = 2 * (seed - 0.5);
  
+       iseed = (int) (seed * MAX_RANDOM_VALUE);
        srandom((unsigned int) iseed);
  
        PG_RETURN_VOID();
---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

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

Reply via email to