File: Sampling-from-a-random-number-generator.html

package info (click to toggle)
gsl-ref-html 1.10-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,496 kB
  • ctags: 2,955
  • sloc: makefile: 33
file content (111 lines) | stat: -rw-r--r-- 6,460 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<html lang="en">
<head>
<title>Sampling from a random number generator - GNU Scientific Library -- Reference Manual</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Scientific Library -- Reference Manual">
<meta name="generator" content="makeinfo 4.8">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Random-Number-Generation.html" title="Random Number Generation">
<link rel="prev" href="Random-number-generator-initialization.html" title="Random number generator initialization">
<link rel="next" href="Auxiliary-random-number-generator-functions.html" title="Auxiliary random number generator functions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 The GSL Team.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``GNU General Public License'' and ``Free Software
Needs Free Documentation'', the Front-Cover text being ``A GNU Manual'',
and with the Back-Cover Text being (a) (see below).  A copy of the
license is included in the section entitled ``GNU Free Documentation
License''.

(a) The Back-Cover Text is: ``You have freedom to copy and modify this
GNU Manual, like GNU software.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Sampling-from-a-random-number-generator"></a>
Next:&nbsp;<a rel="next" accesskey="n" href="Auxiliary-random-number-generator-functions.html">Auxiliary random number generator functions</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Random-number-generator-initialization.html">Random number generator initialization</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Random-Number-Generation.html">Random Number Generation</a>
<hr>
</div>

<h3 class="section">17.4 Sampling from a random number generator</h3>

<p>The following functions return uniformly distributed random numbers,
either as integers or double precision floating point numbers.  Inline versions of these function are used when <code>HAVE_INLINE</code> is defined. 
To obtain non-uniform distributions see <a href="Random-Number-Distributions.html">Random Number Distributions</a>.

<div class="defun">
&mdash; Function: unsigned long int <b>gsl_rng_get</b> (<var>const gsl_rng * r</var>)<var><a name="index-gsl_005frng_005fget-1481"></a></var><br>
<blockquote><p>This function returns a random integer from the generator <var>r</var>.  The
minimum and maximum values depend on the algorithm used, but all
integers in the range [<var>min</var>,<var>max</var>] are equally likely.  The
values of <var>min</var> and <var>max</var> can determined using the auxiliary
functions <code>gsl_rng_max (r)</code> and <code>gsl_rng_min (r)</code>. 
</p></blockquote></div>

<div class="defun">
&mdash; Function: double <b>gsl_rng_uniform</b> (<var>const gsl_rng * r</var>)<var><a name="index-gsl_005frng_005funiform-1482"></a></var><br>
<blockquote><p>This function returns a double precision floating point number uniformly
distributed in the range [0,1).  The range includes 0.0 but excludes 1.0. 
The value is typically obtained by dividing the result of
<code>gsl_rng_get(r)</code> by <code>gsl_rng_max(r) + 1.0</code> in double
precision.  Some generators compute this ratio internally so that they
can provide floating point numbers with more than 32 bits of randomness
(the maximum number of bits that can be portably represented in a single
<code>unsigned long int</code>). 
</p></blockquote></div>

<div class="defun">
&mdash; Function: double <b>gsl_rng_uniform_pos</b> (<var>const gsl_rng * r</var>)<var><a name="index-gsl_005frng_005funiform_005fpos-1483"></a></var><br>
<blockquote><p>This function returns a positive double precision floating point number
uniformly distributed in the range (0,1), excluding both 0.0 and 1.0. 
The number is obtained by sampling the generator with the algorithm of
<code>gsl_rng_uniform</code> until a non-zero value is obtained.  You can use
this function if you need to avoid a singularity at 0.0. 
</p></blockquote></div>

<div class="defun">
&mdash; Function: unsigned long int <b>gsl_rng_uniform_int</b> (<var>const gsl_rng * r, unsigned long int n</var>)<var><a name="index-gsl_005frng_005funiform_005fint-1484"></a></var><br>
<blockquote><p>This function returns a random integer from 0 to n-1 inclusive
by scaling down and/or discarding samples from the generator <var>r</var>. 
All integers in the range [0,n-1] are produced with equal
probability.  For generators with a non-zero minimum value an offset
is applied so that zero is returned with the correct probability.

        <p>Note that this function is designed for sampling from ranges smaller
than the range of the underlying generator.  The parameter <var>n</var>
must be less than or equal to the range of the generator <var>r</var>. 
If <var>n</var> is larger than the range of the generator then the function
calls the error handler with an error code of <code>GSL_EINVAL</code> and
returns zero.

        <p>In particular, this function is not intended for generating the full range of
unsigned integer values <!-- {$[0,2^{32}-1]$} -->
[0,2^32-1]. Instead
choose a generator with the maximal integer range and zero mimimum
value, such as <code>gsl_rng_ranlxd1</code>, <code>gsl_rng_mt19937</code> or
<code>gsl_rng_taus</code>, and sample it directly using
<code>gsl_rng_get</code>.  The range of each generator can be found using
the auxiliary functions described in the next section. 
</p></blockquote></div>

<hr>The GNU Scientific Library - a free numerical library licensed under the GNU GPL<br>Back to the <a href="/software/gsl/">GNU Scientific Library Homepage</a></body></html>