annotate libs/commons-math-2.1/docs/userguide/random.html @ 10:5f2c5fb36e93

commons-math-2.1 added
author dwinter
date Tue, 04 Jan 2011 10:00:53 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
2
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
3
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
4
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
5
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
6
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
7
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
8
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
9
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
11
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
12
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
13 <html xmlns="http://www.w3.org/1999/xhtml">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
14 <head>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
15 <title>Math - The Commons Math User Guide - Data Generation</title>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
16 <style type="text/css" media="all">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
17 @import url("../css/maven-base.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
18 @import url("../css/maven-theme.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
19 @import url("../css/site.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
20 </style>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
23 </head>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
24 <body class="composite">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
25 <div id="banner">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
26 <span id="bannerLeft">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
27
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
28 Commons Math User Guide
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
29
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
30 </span>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
31 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
32 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
33 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
34 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
35 <div id="breadcrumbs">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
36
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
37
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
38
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
39
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
40
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
41
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
42
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
43
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
44 <div class="xright">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
45
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
46
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
47
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
48
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
49
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
50
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
51
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
52 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
53 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
54 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
55 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
56 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
57 <div id="leftColumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
58 <div id="navcolumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
59
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
60
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
61
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
62
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
63
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
64
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
65
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
66
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
67 <h5>User Guide</h5>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
68 <ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
69
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
70 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
71 <a href="../userguide/index.html">Contents</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
72 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
73
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
74 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
75 <a href="../userguide/overview.html">Overview</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
76 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
77
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
78 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
79 <a href="../userguide/stat.html">Statistics</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
80 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
81
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
82 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
83 <strong>Data Generation</strong>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
84 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
85
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
86 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
87 <a href="../userguide/linear.html">Linear Algebra</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
88 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
89
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
90 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
92 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
93
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
94 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
95 <a href="../userguide/special.html">Special Functions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
96 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
97
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
98 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
99 <a href="../userguide/utilities.html">Utilities</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
100 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
101
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
102 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
103 <a href="../userguide/complex.html">Complex Numbers</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
104 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
105
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
106 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
107 <a href="../userguide/distribution.html">Distributions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
108 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
109
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
110 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
111 <a href="../userguide/fraction.html">Fractions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
112 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
113
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
114 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
115 <a href="../userguide/transform.html">Transform Methods</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
116 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
117
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
118 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
119 <a href="../userguide/geometry.html">3D Geometry</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
120 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
121
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
122 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
123 <a href="../userguide/optimization.html">Optimization</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
124 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
125
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
126 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
128 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
129
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
130 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
132 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
133 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
136 </a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
137
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
138
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
139
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
140
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
141
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
142
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
143
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
144
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
145 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
146 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
147 <div id="bodyColumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
148 <div id="contentBox">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
149 <div class="section"><h2><a name="a2_Data_Generation"></a>2 Data Generation</h2>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
150 <div class="section"><h3><a name="a2.1_Overview"></a>2.1 Overview</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
151 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
152 The Commons Math random package includes utilities for
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
153 <ul><li>generating random numbers</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
154 <li>generating random strings</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
155 <li>generating cryptographically secure sequences of random numbers or
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
156 strings</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
157 <li>generating random samples and permutations</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
158 <li>analyzing distributions of values in an input file and generating
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
159 values &quot;like&quot; the values in the file</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
160 <li>generating data for grouped frequency distributions or
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
161 histograms</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
162 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
163 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
164 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
165 The source of random data used by the data generation utilities is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
166 pluggable. By default, the JDK-supplied PseudoRandom Number Generator
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
167 (PRNG) is used, but alternative generators can be &quot;plugged in&quot; using an
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
168 adaptor framework, which provides a generic facility for replacing
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
169 <code>java.util.Random</code> with an alternative PRNG. Another very
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
170 good PRNG suitable for Monte-Carlo analysis (but <strong>not</strong>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
171 for cryptography) provided by the library is the Mersenne twister from
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
172 Makoto Matsumoto and Takuji Nishimura
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
173 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
174 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
175 Sections 2.2-2.6 below show how to use the commons math API to generate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
176 different kinds of random data. The examples all use the default
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
177 JDK-supplied PRNG. PRNG pluggability is covered in 2.7. The only
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
178 modification required to the examples to use alternative PRNGs is to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
179 replace the argumentless constructor calls with invocations including
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
180 a <code>RandomGenerator</code> instance as a parameter.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
181 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
182 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
183 <div class="section"><h3><a name="a2.2_Random_numbers"></a>2.2 Random numbers</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
184 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
185 The <a href="../apidocs/org/apache/commons/math/random/RandomData.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
186 org.apache.commons.math.RandomData</a> interface defines methods for
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
187 generating random sequences of numbers. The API contracts of these methods
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
188 use the following concepts:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
189 <dl><dt>Random sequence of numbers from a probability distribution</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
190 <dd>There is no such thing as a single &quot;random number.&quot; What can be
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
191 generated are <i>sequences</i> of numbers that appear to be random. When
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
192 using the built-in JDK function <code>Math.random(),</code> sequences of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
193 values generated follow the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
194 <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda3662.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
195 Uniform Distribution</a>, which means that the values are evenly spread
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
196 over the interval between 0 and 1, with no sub-interval having a greater
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
197 probability of containing generated values than any other interval of the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
198 same length. The mathematical concept of a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
199 <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda36.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
200 probability distribution</a> basically amounts to asserting that different
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
201 ranges in the set of possible values of a random variable have
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
202 different probabilities of containing the value. Commons Math supports
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
203 generating random sequences from the following probability distributions.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
204 The javadoc for the <code>nextXxx</code> methods in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
205 <code>RandomDataImpl</code> describes the algorithms used to generate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
206 random deviates from each of these distributions.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
207 <ul><li><a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda3662.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
208 uniform distribution</a></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
209 <li><a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda3667.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
210 exponential distribution</a></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
211 <li><a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda366j.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
212 poisson distribution</a></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
213 <li><a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda3661.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
214 Gaussian distribution</a></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
215 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
216 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
217 <dt>Cryptographically secure random sequences</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
218 <dd>It is possible for a sequence of numbers to appear random, but
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
219 nonetheless to be predictable based on the algorithm used to generate the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
220 sequence. If in addition to randomness, strong unpredictability is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
221 required, it is best to use a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
222 <a href="http://www.wikipedia.org/wiki/Cryptographically_secure_pseudo-random_number_generator" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
223 secure random number generator</a> to generate values (or strings). The
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
224 nextSecureXxx methods in the <code>RandomDataImpl</code> implementation of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
225 the <code>RandomData</code> interface use the JDK <code>SecureRandom</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
226 PRNG to generate cryptographically secure sequences. The
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
227 <code>setSecureAlgorithm</code> method allows you to change the underlying
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
228 PRNG. These methods are <strong>much slower</strong> than the corresponding
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
229 &quot;non-secure&quot; versions, so they should only be used when cryptographic
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
230 security is required.</dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
231 <dt>Seeding pseudo-random number generators</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
232 <dd>By default, the implementation provided in <code>RandomDataImpl</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
233 uses the JDK-provided PRNG. Like most other PRNGs, the JDK generator
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
234 generates sequences of random numbers based on an initial &quot;seed value&quot;.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
235 For the non-secure methods, starting with the same seed always produces the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
236 same sequence of values. Secure sequences started with the same seeds will
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
237 diverge. When a new <code>RandomDataImpl</code> is created, the underlying
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
238 random number generators are <strong>not</strong> initialized. The first
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
239 call to a data generation method, or to a <code>reSeed()</code> method
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
240 initializes the appropriate generator. If you do not explicitly seed the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
241 generator, it is by default seeded with the current time in milliseconds.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
242 Therefore, to generate sequences of random data values, you should always
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
243 instantiate <strong>one</strong><code>RandomDataImpl</code> and use it
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
244 repeatedly instead of creating new instances for subsequent values in the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
245 sequence. For example, the following will generate a random sequence of 50
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
246 long integers between 1 and 1,000,000, using the current time in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
247 milliseconds as the seed for the JDK PRNG:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
248 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
249 RandomData randomData = new RandomDataImpl();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
250 for (int i = 0; i &lt; 1000; i++) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
251 value = randomData.nextLong(1, 1000000);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
252 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
253 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
254 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
255
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
256 The following will not in general produce a good random sequence, since the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
257 PRNG is reseeded each time through the loop with the current time in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
258 milliseconds:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
259 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
260 for (int i = 0; i &lt; 1000; i++) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
261 RandomDataImpl randomData = new RandomDataImpl();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
262 value = randomData.nextLong(1, 1000000);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
263 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
264 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
265 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
266
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
267 The following will produce the same random sequence each time it is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
268 executed:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
269 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
270 RandomData randomData = new RandomDataImpl();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
271 randomData.reSeed(1000);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
272 for (int i = 0; i = 1000; i++) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
273 value = randomData.nextLong(1, 1000000);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
274 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
275 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
276 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
277
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
278 The following will produce a different random sequence each time it is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
279 executed.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
280 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
281 RandomData randomData = new RandomDataImpl();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
282 randomData.reSeedSecure(1000);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
283 for (int i = 0; i &lt; 1000; i++) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
284 value = randomData.nextSecureLong(1, 1000000);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
285 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
286 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
287 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
288 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
289 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
290 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
291 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
292 <div class="section"><h3><a name="a2.3_Random_Vectors"></a>2.3 Random Vectors</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
293 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
294 Some algorithm requires random vectors instead of random scalars. When the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
295 components of these vectors are uncorrelated, they may be generated simply
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
296 one at a time and packed together in the vector. The <a href="../apidocs/org/apache/commons/math/random/UncorrelatedRandomVectorGenerator.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
297 org.apache.commons.math.UncorrelatedRandomVectorGenerator</a> class
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
298 does however simplify this process by setting the mean and deviation of each
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
299 component once and generating complete vectors. When the components are correlated
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
300 however, generating them is much more difficult. The <a href="../apidocs/org/apache/commons/math/random/CorrelatedRandomVectorGenerator.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
301 org.apache.commons.math.CorrelatedRandomVectorGenerator</a> class
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
302 provides this service. In this case, the user must set up a complete covariance matrix
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
303 instead of a simple standard deviations vector. This matrix gathers both the variance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
304 and the correlation information of the probability law.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
305 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
306 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
307 The main use for correlated random vector generation is for Monte-Carlo
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
308 simulation of physical problems with several variables, for example to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
309 generate error vectors to be added to a nominal vector. A particularly
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
310 common case is when the generated vector should be drawn from a <a href="http://en.wikipedia.org/wiki/Multivariate_normal_distribution" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
311 Multivariate Normal Distribution</a>.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
312 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
313 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
314 <div class="section"><h3><a name="a2.4_Random_Strings"></a>2.4 Random Strings</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
315 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
316 The methods <code>nextHexString</code> and <code>nextSecureHexString</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
317 can be used to generate random strings of hexadecimal characters. Both
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
318 of these methods produce sequences of strings with good dispersion
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
319 properties. The difference between the two methods is that the second is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
320 cryptographically secure. Specifically, the implementation of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
321 <code>nextHexString(n)</code> in <code>RandomDataImpl</code> uses the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
322 following simple algorithm to generate a string of <code>n</code> hex digits:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
323 <ol type="1"><li>n/2+1 binary bytes are generated using the underlying Random</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
324 <li>Each binary byte is translated into 2 hex digits</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
325 </ol>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
326
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
327 The <code>RandomDataImpl</code> implementation of the &quot;secure&quot; version,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
328 <code>nextSecureHexString</code> generates hex characters in 40-byte
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
329 &quot;chunks&quot; using a 3-step process:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
330 <ol type="1"><li>20 random bytes are generated using the underlying
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
331 <code>SecureRandom.</code></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
332 <li>SHA-1 hash is applied to yield a 20-byte binary digest.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
333 <li>Each byte of the binary digest is converted to 2 hex digits</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
334 </ol>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
335
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
336 Similarly to the secure random number generation methods,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
337 <code>nextSecureHexString</code> is <strong>much slower</strong> than
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
338 the non-secure version. It should be used only for applications such as
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
339 generating unique session or transaction ids where predictability of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
340 subsequent ids based on observation of previous values is a security
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
341 concern. If all that is needed is an even distribution of hex characters
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
342 in the generated strings, the non-secure method should be used.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
343 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
344 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
345 <div class="section"><h3><a name="a2.5_Random_permutations_combinations_sampling"></a>2.5 Random permutations, combinations, sampling</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
346 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
347 To select a random sample of objects in a collection, you can use the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
348 <code>nextSample</code> method in the <code>RandomData</code> interface.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
349 Specifically, if <code>c</code> is a collection containing at least
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
350 <code>k</code> objects, and <code>randomData</code> is a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
351 <code>RandomData</code> instance <code>randomData.nextSample(c, k)</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
352 will return an <code>object[]</code> array of length <code>k</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
353 consisting of elements randomly selected from the collection. If
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
354 <code>c</code> contains duplicate references, there may be duplicate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
355 references in the returned array; otherwise returned elements will be
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
356 unique -- i.e., the sampling is without replacement among the object
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
357 references in the collection. </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
358 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
359 If <code>randomData</code> is a <code>RandomData</code> instance, and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
360 <code>n</code> and <code>k</code> are integers with
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
361 <code> k &lt;= n</code>, then
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
362 <code>randomData.nextPermutation(n, k)</code> returns an <code>int[]</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
363 array of length <code>k</code> whose whose entries are selected randomly,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
364 without repetition, from the integers <code>0</code> through
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
365 <code>n-1</code> (inclusive), i.e.,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
366 <code>randomData.nextPermutation(n, k)</code> returns a random
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
367 permutation of <code>n</code> taken <code>k</code> at a time.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
368 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
369 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
370 <div class="section"><h3><a name="a2.6_Generating_data_like_an_input_file"></a>2.6 Generating data 'like' an input file</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
371 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
372 Using the <code>ValueServer</code> class, you can generate data based on
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
373 the values in an input file in one of two ways:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
374 <dl><dt>Replay Mode</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
375 <dd> The following code will read data from <code>url</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
376 (a <code>java.net.URL</code> instance), cycling through the values in the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
377 file in sequence, reopening and starting at the beginning again when all
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
378 values have been read.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
379 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
380 ValueServer vs = new ValueServer();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
381 vs.setValuesFileURL(url);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
382 vs.setMode(ValueServer.REPLAY_MODE);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
383 vs.resetReplayFile();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
384 double value = vs.getNext();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
385 // ...Generate and use more values...
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
386 vs.closeReplayFile();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
387 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
388 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
389
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
390 The values in the file are not stored in memory, so it does not matter
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
391 how large the file is, but you do need to explicitly close the file
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
392 as above. The expected file format is \n -delimited (i.e. one per line)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
393 strings representing valid floating point numbers.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
394 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
395 <dt>Digest Mode</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
396 <dd>When used in Digest Mode, the ValueServer reads the entire input file
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
397 and estimates a probability density function based on data from the file.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
398 The estimation method is essentially the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
399 <a href="http://nedwww.ipac.caltech.edu/level5/March02/Silverman/Silver2_6.html" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
400 Variable Kernel Method</a> with Gaussian smoothing. Once the density
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
401 has been estimated, <code>getNext()</code> returns random values whose
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
402 probability distribution matches the empirical distribution -- i.e., if
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
403 you generate a large number of such values, their distribution should
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
404 &quot;look like&quot; the distribution of the values in the input file. The values
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
405 are not stored in memory in this case either, so there is no limit to the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
406 size of the input file. Here is an example:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
407 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
408 ValueServer vs = new ValueServer();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
409 vs.setValuesFileURL(url);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
410 vs.setMode(ValueServer.DIGEST_MODE);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
411 vs.computeDistribution(500); //Read file and estimate distribution using 500 bins
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
412 double value = vs.getNext();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
413 // ...Generate and use more values...
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
414 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
415 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
416
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
417 See the javadoc for <code>ValueServer</code> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
418 <code>EmpiricalDistribution</code> for more details. Note that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
419 <code>computeDistribution()</code> opens and closes the input file
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
420 by itself.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
421 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
422 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
423 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
424 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
425 <div class="section"><h3><a name="a2.7_PRNG_Pluggability"></a>2.7 PRNG Pluggability</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
426 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
427 To enable alternative PRNGs to be &quot;plugged in&quot; to the commons-math data
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
428 generation utilities and to provide a generic means to replace
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
429 <code>java.util.Random</code> in applications, a random generator
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
430 adaptor framework has been added to commons-math. The
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
431 <a href="../apidocs/org/apache/commons/math/random/RandomGenerator.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
432 org.apache.commons.math.RandomGenerator</a> interface abstracts the public
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
433 interface of <code>java.util.Random</code> and any implementation of this
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
434 interface can be used as the source of random data for the commons-math
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
435 data generation classes. An abstract base class,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
436 <a href="../apidocs/org/apache/commons/math/random/AbstractRandomGenerator.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
437 org.apache.commons.math.AbstractRandomGenerator</a> is provided to make
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
438 implementation easier. This class provides default implementations of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
439 &quot;derived&quot; data generation methods based on the primitive,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
440 <code>nextDouble().</code> To support generic replacement of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
441 <code>java.util.Random</code>, the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
442 <a href="../apidocs/org/apache/commons/math/random/RandomAdaptor.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
443 org.apache.commons.math.RandomAdaptor</a> class is provided, which
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
444 extends <code>java.util.Random</code> and wraps and delegates calls to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
445 a <code>RandomGenerator</code> instance.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
446 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
447 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
448 Examples:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
449 <dl><dt>Create a RandomGenerator based on RngPack's Mersenne Twister</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
450 <dd>To create a RandomGenerator using the RngPack Mersenne Twister PRNG
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
451 as the source of randomness, extend <code>AbstractRandomGenerator</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
452 overriding the derived methods that the RngPack implementation provides:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
453 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
454 import edu.cornell.lassp.houle.RngPack.RanMT;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
455 /**
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
456 * AbstractRandomGenerator based on RngPack RanMT generator.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
457 */
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
458 public class RngPackGenerator extends AbstractRandomGenerator {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
459
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
460 private RanMT random = new RanMT();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
461
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
462 public void setSeed(long seed) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
463 random = new RanMT(seed);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
464 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
465
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
466 public double nextDouble() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
467 return random.raw();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
468 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
469
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
470 public double nextGaussian() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
471 return random.gaussian();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
472 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
473
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
474 public int nextInt(int n) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
475 return random.choose(n);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
476 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
477
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
478 public boolean nextBoolean() {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
479 return random.coin();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
480 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
481 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
482 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
483 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
484 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
485 <dt>Use the Mersenne Twister RandomGenerator in place of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
486 <code>java.util.Random</code> in <code>RandomData</code></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
487 <dd><div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
488 RandomData randomData = new RandomDataImpl(new RngPackGenerator());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
489 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
490 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
491 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
492 <dt>Create an adaptor instance based on the Mersenne Twister generator
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
493 that can be used in place of a <code>Random</code></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
494 <dd><div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
495 RandomGenerator generator = new RngPackGenerator();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
496 Random random = RandomAdaptor.createAdaptor(generator);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
497 // random can now be used in place of a Random instance, data generation
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
498 // calls will be delegated to the wrapped Mersenne Twister
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
499 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
500 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
501 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
502 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
503 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
504 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
505 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
506
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
507 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
508 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
509 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
510 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
511 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
512 <div id="footer">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
513 <div class="xright">&#169;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
514 2003-2010
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
515
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
516
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
517
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
518
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
519
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
520
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
521
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
522
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
523
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
524 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
525 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
526 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
527 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
528 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
529 </body>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
530 </html>