Hello,

On Wed, Jun 13, 2012 at 01:04:51AM +0200, Nicolas
François wrote:
> On Wed, Jun 13, 2012 at 12:15:53AM +0400,
> pashev.i...@gmail.com wrote:
> > But random() returns numbers in the range from
> > 0 to 2^31-1 [2].
> > 
> > So, random()/RAND_MAX could result in a value > 1.
> 
> Theoretically agreed.
> The random(3) manpage is misleading here
> (although no problem on glibc)

HP-UX, Tru64 and SunOS5.9 manpages on random()
don't mention RAND_MAX at all and state that
random() returns an integer from 0 to 2^31-1

The problem here is "on some systems random() can
exceed RAND_MAX". In fact, I'm more interested
about systems where random() cannot exceed
RAND_MAX. Do such systems have RAND_MAX==2^31-1?
Or do they have random() with another range of
possible return values? In the latter case we
should have to check for non-standard random()
in ./configure script.

-- 
With best regards,
xrgtn

Attachment: random-hpux.3m.Z
Description: Binary data

Attachment: random-tru64.3.gz
Description: Binary data

<!DOCTYPE REFENTRY PUBLIC "-//Sun Microsystems//DTD DocBook V3.0-Based SolBook 
Subset V2.0//EN" [
<!--ArborText, Inc., 1988-1999, v.4002-->
<!ENTITY % commonents SYSTEM "smancommon.ent">
%commonents;
<!ENTITY % booktitles SYSTEM "booktitles.ent">
%booktitles;
]>
<refentry id="random-3c">
<!-- @(#)random.3c 1.6 02/02/14 SMI; -->
<refmeta><refentrytitle>random</refentrytitle><manvolnum>3C</manvolnum>
<refmiscinfo class="date">20 Sep 2000</refmiscinfo>
<refmiscinfo class="sectdesc">&man3c;</refmiscinfo>
<refmiscinfo class="software">&release;</refmiscinfo>
<refmiscinfo class="arch">generic</refmiscinfo>
<refmiscinfo class="copyright"> Copyright (c) 1983 Regents of the University of 
California.  All rights reserved.  The Berkeley software License Agreement 
specifies the terms and conditions for redistribution.  Copyright (c) 2000, Sun 
Microsystems, Inc.  All Rights Reserved  Portions Copyright (c) 1992,
X/Open Company Limited  All Rights Reserved</refmiscinfo>
</refmeta>
<refnamediv><refname>random</refname><refname>srandom</refname><refname>initstate</refname><refname>setstate</refname><refpurpose>pseudorandom
 number functions</refpurpose></refnamediv>
<refsynopsisdiv remap="ansi"><title>&synp-tt;</title><funcsynopsis>
<funcsynopsisinfo>#include &lt;stdlib.h></funcsynopsisinfo>
<funcdef>long 
<function>random</function></funcdef><paramdef>void</paramdef></funcsynopsis>
<funcsynopsis>
<funcdef>void <function>srandom</function></funcdef><paramdef>unsigned int 
<parameter>seed</parameter></paramdef></funcsynopsis>
<funcsynopsis>
<funcdef>char *<function>initstate</function></funcdef>
<paramdef>unsigned int <parameter>seed</parameter></paramdef>
<paramdef>char <parameter>*state</parameter></paramdef><paramdef>size_t 
<parameter>size</parameter></paramdef></funcsynopsis>
<funcsynopsis>
<funcdef>char *<function>setstate</function></funcdef><paramdef>const char 
*<parameter>state</parameter></paramdef></funcsynopsis>
</refsynopsisdiv>
<refsect1><title>&desc-tt;</title>
<indexterm significance="normal"><primary>pseudorandom number 
functions</primary><secondary sortas="setstate">&mdash; 
setstate</secondary></indexterm><indexterm significance="normal"><primary 
sortas="setstate">setstate &mdash; pseudorandom number 
functions</primary></indexterm><indexterm significance="normal">
<primary>pseudorandom number functions</primary><secondary 
sortas="initstate">&mdash; initstate</secondary></indexterm><indexterm 
significance="normal"><primary sortas="initstate">initstate &mdash; 
pseudorandom number functions</primary></indexterm><indexterm 
significance="normal"><primary>pseudorandom
number functions</primary><secondary sortas="srandom">&mdash; 
srandom</secondary></indexterm><indexterm significance="normal"><primary 
sortas="srandom">srandom &mdash; pseudorandom number 
functions</primary></indexterm><indexterm 
significance="normal"><primary>pseudorandom number functions</primary><secondary
sortas="random">&mdash; random</secondary></indexterm><indexterm 
significance="normal"><primary sortas="random">random &mdash; pseudorandom 
number functions</primary></indexterm>
<para>The <function>random</function> function uses a nonlinear additive 
feedback random-number generator employing a default state array size of 31 
long integers to return successive pseudo-random numbers in the range from 0 to 
2<superscript>31</superscript> &minus;1. The period of this random-number
generator is approximately 16 x (2<superscript>31</superscript> &minus;1). The 
size of the state array determines the period of the random-number generator. 
Increasing the state array size increases the period.</para>
<para>The <function>srandom</function> function initializes the current state 
array using the value of <parameter>seed</parameter>.</para>
<para>The <function>random</function> and <function>srandom</function> 
functions have (almost) the same calling sequence and initialization properties 
as <function>rand</function> and <function>srand</function> (see <link 
linkend="rand-3c"><citerefentry><refentrytitle>rand</refentrytitle><manvolnum>3C
</manvolnum></citerefentry></link>). The difference is that <link 
linkend="rand-3c"><citerefentry><refentrytitle>rand</refentrytitle><manvolnum>3C</manvolnum></citerefentry></link>
 produces a much less random sequence&mdash;in fact, the low dozen bits 
generated by rand go through a cyclic pattern. All
the bits generated by <function>random</function> are usable.</para>
<para>The algorithm from <function>rand</function> is used by 
<function>srandom</function> to generate the 31 state integers. Because of 
this, different <function>srandom</function> seeds often produce, within an 
offset, the same sequence of low order bits from <function>random</function>. 
If low order
bits are used directly, <function>random</function> should be initialized with 
<function>setstate</function> using high quality random values.</para>
<para>Unlike <function>srand</function>, <function>srandom</function> does not 
return the old seed because the amount of state information used is much more 
than a single word. Two other routines are provided to deal with 
restarting/changing random number generators. With 256 bytes of state 
information,
the period of the random-number generator is greater than 
2<superscript>69</superscript>, which should be sufficient for most 
purposes.</para>
<para>Like <link 
linkend="rand-3c"><citerefentry><refentrytitle>rand</refentrytitle><manvolnum>3C</manvolnum></citerefentry></link>,
 <function>random</function> produces by default a sequence of numbers that can 
be duplicated by calling <function>srandom</function> with 1 as the seed.</para>
<para>The <function>initstate</function> and <function>setstate</function> 
functions handle restarting and changing random-number generators.  The 
<function>initstate</function> function allows a state array, pointed to by the 
<parameter>state</parameter> argument, to be initialized for future use. The 
<command>
size</command> argument, which specifies the size in bytes of the state array, 
is used by <function>initstate</function> to decide what type of random-number 
generator to use; the larger the state array, the more random the numbers.  
Values for the amount of state information are 8, 32, 64, 128, and 256
bytes.  Other values greater than 8 bytes are rounded down to the nearest one 
of these values.  For values smaller than 8, <function>random</function> uses a 
simple linear congruential random number generator.  The 
<parameter>seed</parameter> argument specifies a starting point for the 
random-number sequence
and provides for restarting at the same point.  The 
<function>initstate</function> function returns a pointer to the previous state 
information array.</para>
<para>If <function>initstate</function> has not been called, then 
<function>random</function> behaves as though <function>initstate</function> 
had been called with <parameter>seed</parameter>&thinsp;=&thinsp;1 and 
<parameter>size</parameter>&thinsp;=&thinsp;128.</para>
<para>If <function>initstate</function> is called with 
<parameter>size</parameter>&thinsp;&lt;&thinsp;8, then 
<function>random</function> uses a simple linear congruential random number 
generator.</para>
<para>Once a state has been initialized, <function>setstate</function> allows 
switching between state arrays. The array defined by the 
<parameter>state</parameter> argument is used for further random-number 
generation until <function>initstate</function> is called or 
<function>setstate</function> is called
again. The <function>setstate</function> function returns a pointer to the 
previous state array.</para>
</refsect1>
<refsect1><title>&rtrn-tt;</title>
<para>The <function>random</function> function returns the generated 
pseudo-random number.</para>
<para>The <function>srandom</function> function returns no value.</para>
<para>Upon successful completion, <function>initstate</function> and 
<function>setstate</function> return a pointer to the previous state array.  
Otherwise, a null pointer is returned.</para>
</refsect1>
<refsect1><title>&erro-tt;</title>
<para>No errors are defined.</para>
</refsect1>
<refsect1><title>USAGE</title>
<para>After initialization, a state array can be restarted at a different point 
in one of two ways:</para>
<itemizedlist mark="bullet">
<listitem><para>The <function>initstate</function> function can be used, with 
the desired seed, state array, and size of the array.</para></listitem>
<listitem><para>The <function>setstate</function> function, with the desired 
state, can be used, followed by <function>srandom</function> with the desired 
seed. The advantage of using both of these functions is that the size of the 
state array does not have to be saved once it is initialized.</para></listitem>
</itemizedlist>
</refsect1>
<refsect1><title>&exam-tt;</title>
<example role="example"><title>Initialize an array.</title>
<para>The following example demonstrates the use of 
<function>initstate</function> to intialize an array. It also demonstrates how 
to initialize an array and pass it to <function>setstate</function>.</para>
<para><programlisting># include &lt;stdlib.h>
static unsigned int state0[32];
static unsigned int state1[32] = {
     3,
     0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
     0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
     0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
     0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
     0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
     0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
     0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
     0xf5ad9d0e, 0x8999220b, 0x27fb47b9
     };
main() {
     unsigned seed;
     int n;
     seed = 1;
     n = 128;
     (void)initstate(seed, (char *)state0, n);
     printf("random() = %d0\n", random());
     (void)setstate((char *)state1);
     printf("random() = %d0\n", random());
}</programlisting></para>
</example>
</refsect1>
<refsect1><title>&attr-tt;</title>
<para>See <olink targetdocent="REFMAN5" 
localinfo="attributes-5"><citerefentry><refentrytitle>attributes</refentrytitle><manvolnum>5</manvolnum></citerefentry></olink>
 for descriptions of the following attributes:</para>
<informaltable frame="all" orient="port">
<tgroup cols="2" colsep="1" rowsep="1"><colspec colnum="1" colname="col1" 
colwidth="1*" colsep="1" align="left"><colspec colnum="2" colname="col2" 
colwidth="1*" align="left">
<thead>
<row><entry rowsep="1" align="center">ATTRIBUTE TYPE</entry><entry rowsep="1" 
align="center">ATTRIBUTE VALUE</entry></row>
</thead>
<tbody>
<row><entry>MT-Level</entry><entry>See <literal>NOTES</literal> 
below.</entry></row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1><title>&also-tt;</title>
<para><link 
linkend="drand48-3c"><citerefentry><refentrytitle>drand48</refentrytitle><manvolnum>3C</manvolnum></citerefentry></link>,
 <link 
linkend="rand-3c"><citerefentry><refentrytitle>rand</refentrytitle><manvolnum>3C</manvolnum></citerefentry></link>,
 <olink targetdocent="REFMAN5" localinfo="attributes-5"><citerefentry>
<refentrytitle>attributes</refentrytitle><manvolnum>5</manvolnum></citerefentry></olink></para>
</refsect1>
<refsect1><title>&note-tt;</title>
<para>The <function>random</function> and <function>srandom</function> 
functions are unsafe in multithreaded applications.</para>
<para>Use of these functions in multithreaded applications is 
unsupported.</para>
<para>For <function>initstate</function> and <function>setstate</function>, the 
<parameter>state</parameter> argument must be aligned on an 
<literal>int</literal> boundary.</para>
<para>Newer and better performing random number generators such as 
<function>addrans</function> and <function>lcrans</function> are available with 
the SUNWspro package.</para>
</refsect1>
</refentry>
<!--end of file-->

Attachment: signature.asc
Description: Digital signature

Reply via email to