Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9426047

Browse files
committed
Clean up bogosities in use of random(3) and srandom(3) --- do not assume
that RAND_MAX applies to them, since it doesn't. Instead add aconfig.h parameter MAX_RANDOM_VALUE. This is currently set at 2^31-1but could be auto-configured if that ever proves necessary. Also fixsome outright bugs like calling srand() where srandom() is appropriate.
1 parent259489b commit9426047

File tree

8 files changed

+47
-25
lines changed

8 files changed

+47
-25
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@
171171
<entry>convert floating point to integer</entry>
172172
<entry>integer(2.0)</entry>
173173
</row>
174+
<row>
175+
<entry>random()</entry>
176+
<entry>float8</entry>
177+
<entry>random value in the range 0.0 to 1.0</entry>
178+
<entry>random()</entry>
179+
</row>
180+
<row>
181+
<entry>setseed(float8)</entry>
182+
<entry>int</entry>
183+
<entry>set seed for subsequent random() calls</entry>
184+
<entry>setseed(0.54823)</entry>
185+
</row>
174186
</tbody>
175187
</tgroup>
176188
</table>

‎doc/src/sgml/ref/set.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/set.sgml,v 1.46 2000/07/14 15:27:14 thomas Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/set.sgml,v 1.47 2000/08/07 00:51:18 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -194,9 +194,9 @@ SET TIME ZONE { '<replaceable class="PARAMETER">timezone</replaceable>' | LOCAL
194194
<listitem>
195195
<para>
196196
The value for the seed to be used by the
197-
<function>random</function>catalogfunction.Significant
197+
<function>random</function> function.Allowed
198198
values are floating point numbers between 0 and 1, which
199-
are then multiplied byRAND_MAX. This product will
199+
are then multiplied by2^31-1. This product will
200200
silently overflow if a number outside the range is used.
201201
</para>
202202

‎src/backend/optimizer/geqo/geqo_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: geqo_main.c,v 1.22 2000/06/28 03:31:45 tgl Exp $
10+
* $Id: geqo_main.c,v 1.23 2000/08/07 00:51:23 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -106,9 +106,9 @@ geqo(Query *root)
106106
/* seed random number generator */
107107
/* XXX why is this done every time around? */
108108
if (Geqo_random_seed >=0)
109-
srandom(Geqo_random_seed);
109+
srandom((unsignedint)Geqo_random_seed);
110110
else
111-
srandom(time(NULL));
111+
srandom((unsignedint)time(NULL));
112112

113113
/* allocate genetic pool memory */
114114
pool=alloc_pool(pool_size,number_of_rels);

‎src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.158 2000/07/28 02:13:26 tgl Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.159 2000/08/07 00:51:30 tgl Exp $
1515
*
1616
* NOTES
1717
*
@@ -1850,7 +1850,7 @@ DoBackend(Port *port)
18501850
*/
18511851
random_seed=0;
18521852
gettimeofday(&now,&tz);
1853-
srandom(now.tv_usec);
1853+
srandom((unsignedint)now.tv_usec);
18541854

18551855
/* ----------------
18561856
* Now, build the argv vector that will be given to PostgresMain.
@@ -2029,7 +2029,6 @@ RandomSalt(char *salt)
20292029
staticlong
20302030
PostmasterRandom(void)
20312031
{
2032-
20332032
staticboolinitialized= false;
20342033

20352034
if (!initialized)

‎src/backend/utils/adt/float.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.67 2000/08/01 18:29:35 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.68 2000/08/07 00:51:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1315,7 +1315,7 @@ drandom(PG_FUNCTION_ARGS)
13151315
float8result;
13161316

13171317
/* result 0.0-1.0 */
1318-
result= ((double)random()) /RAND_MAX;
1318+
result= ((double)random()) /((double)MAX_RANDOM_VALUE);
13191319

13201320
PG_RETURN_FLOAT8(result);
13211321
}
@@ -1328,7 +1328,7 @@ Datum
13281328
setseed(PG_FUNCTION_ARGS)
13291329
{
13301330
float8seed=PG_GETARG_FLOAT8(0);
1331-
intiseed= (seed*RAND_MAX);
1331+
intiseed= (int) (seed*MAX_RANDOM_VALUE);
13321332

13331333
srandom((unsignedint)iseed);
13341334

‎src/backend/utils/adt/misc.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.19 2000/06/05 07:28:52 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/misc.c,v 1.20 2000/08/07 00:51:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -45,23 +45,25 @@ nonnullvalue(PG_FUNCTION_ARGS)
4545

4646
/*
4747
* oidrand (oid o, int4 X)-
48-
*takes in an oid and a int4 X, and will return 'true'
49-
*about 1/X ofthe time.
48+
*Takes in an oid and a int4 X, and will return 'true' about 1/X of
49+
*the time. If X == 0, this will always return true.
5050
* Useful for doing random sampling or subsetting.
51-
*if X == 0, this will always return true;
5251
*
5352
* Example use:
5453
* select * from TEMP where oidrand(TEMP.oid, 10)
5554
* will return about 1/10 of the tuples in TEMP
5655
*
56+
* NOTE: the OID input is not used at all. It is there just because of
57+
* an old optimizer bug: a qual expression containing no variables was
58+
* mistakenly assumed to be a constant. Pretending to access the row's OID
59+
* prevented the optimizer from treating the oidrand() result as constant.
5760
*/
5861

5962
staticboolrandom_initialized= false;
6063

6164
Datum
6265
oidrand(PG_FUNCTION_ARGS)
6366
{
64-
/* XXX seems like we ought to be using the oid for something? */
6567
#ifdefNOT_USED
6668
Oido=PG_GETARG_OID(0);
6769
#endif
@@ -96,7 +98,7 @@ oidsrand(PG_FUNCTION_ARGS)
9698
{
9799
int32X=PG_GETARG_INT32(0);
98100

99-
srand(X);
101+
srandom((unsignedint)X);
100102
random_initialized= true;
101103
PG_RETURN_BOOL(true);
102104
}

‎src/include/config.h.in

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* or in config.h afterwards. Of course, if you edit config.h, then your
99
* changes will be overwritten the next time you run configure.
1010
*
11-
* $Id: config.h.in,v 1.128 2000/07/28 02:13:40 tgl Exp $
11+
* $Id: config.h.in,v 1.129 2000/08/07 00:51:38 tgl Exp $
1212
*/
1313

1414
#ifndefCONFIG_H
@@ -484,6 +484,15 @@ extern long random(void);
484484
externvoidsrandom(unsignedintseed);
485485
#endif
486486

487+
/* The random() function is expected to yield values 0 .. MAX_RANDOM_VALUE */
488+
/* Currently, all known implementations yield 0..2^31-1, so we just hardwire
489+
* this constant. We could do a configure test if it proves to be necessary.
490+
* CAUTION: Think not to replace this with RAND_MAX. RAND_MAX defines the
491+
* maximum value of the older rand() function, which is often different from
492+
* --- and considerably inferior to --- random().
493+
*/
494+
#defineMAX_RANDOM_VALUE (0x7FFFFFFF)
495+
487496
/* Set to 1 if you have libz.a */
488497
#undef HAVE_LIBZ
489498

‎src/include/optimizer/geqo_random.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: geqo_random.h,v 1.6 2000/01/26 05:58:20 momjian Exp $
9+
* $Id: geqo_random.h,v 1.7 2000/08/07 00:51:42 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -26,13 +26,13 @@
2626

2727
#include<math.h>
2828

29-
#defineGEQOMASK 2147483647
29+
/* geqo_rand returns a random float value between 0 and 1 inclusive */
3030

31-
#definegeqo_rand() ((double)random()/GEQOMASK)
31+
#definegeqo_rand() (((double)random()) / ((double) MAX_RANDOM_VALUE))
3232

33-
/* geqo_randint returns integer value
34-
between lower and upper inclusive */
33+
/* geqo_randint returns integer value between lower and upper inclusive */
3534

36-
#definegeqo_randint(upper,lower) ( (int) floor( geqo_rand()*((upper-lower)+0.999999) ) +lower )
35+
#definegeqo_randint(upper,lower) \
36+
( (int) floor( geqo_rand()*(((upper)-(lower))+0.999999) ) + (lower) )
3737

3838
#endif/* GEQO_RANDOM_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp