annotate src/DLD-FUNCTIONS/rand.cc @ 14610:41d7e23f5734

Document rand's seed difference from Matlab * rand.cc (Frand): Mention in docstring that seed is randomly chosen, not always fixed at startup. Give hint on how to obtain Matlab-like behaviour.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 08 May 2012 23:23:02 -0400
parents 60e5cf354d80
children cd375519eab0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
1 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
2
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 14038
diff changeset
3 Copyright (C) 1996-2012 John W. Eaton
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
4 Copyright (C) 2009 VZLU Prague
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
5
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
11 option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
12
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16 for more details.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
17
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
20 <http://www.gnu.org/licenses/>.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
21
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
22 */
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
23
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #include <config.h>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
27
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28 #include <ctime>
13727
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
29 #if defined (HAVE_UNORDERED_MAP)
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
30 #include <unordered_map>
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
31 #elif defined (HAVE_TR1_UNORDERED_MAP)
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
32 #include <tr1/unordered_map>
13727
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
33 #endif
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
34 #include <string>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
35
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
36 #include "f77-fcn.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
37 #include "lo-mappers.h"
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
38 #include "oct-rand.h"
4153
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3887
diff changeset
39 #include "quit.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
40
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
41 #include "defun-dld.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
42 #include "error.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
43 #include "gripes.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
44 #include "oct-obj.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
45 #include "unwind-prot.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
46 #include "utils.h"
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
47 #include "ov-re-mat.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
48
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
49 /*
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
50 %!shared __random_statistical_tests__
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
51 %! # Flag whether the statistical tests should be run in "make check" or not
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
52 %! __random_statistical_tests__ = 0;
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
53 */
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
54
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
55 static octave_value
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
56 do_rand (const octave_value_list& args, int nargin, const char *fcn,
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
57 const std::string& distribution, bool additional_arg = false)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
58 {
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
59 octave_value retval;
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
60 NDArray a;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
61 int idx = 0;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
62 dim_vector dims;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
63
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
64 unwind_protect frame;
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
65 // Restore current distribution on any exit.
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
66 frame.add_fcn (octave_rand::distribution,
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
67 octave_rand::distribution ());
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
68
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
69 octave_rand::distribution (distribution);
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
70
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
71 if (additional_arg)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
72 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
73 if (nargin == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
74 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
75 error ("%s: expecting at least one argument", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
76 goto done;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
77 }
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
78 else if (args(0).is_string())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
79 additional_arg = false;
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
80 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
81 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
82 a = args(0).array_value ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
83 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
84 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
85 error ("%s: expecting scalar or matrix arguments", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
86 goto done;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
87 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
88 idx++;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
89 nargin--;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
90 }
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
91 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
92
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
93 switch (nargin)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
94 {
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
95 case 0:
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
96 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
97 if (additional_arg)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
98 dims = a.dims ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
99 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
100 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
101 dims.resize (2);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
102
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
103 dims(0) = 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
104 dims(1) = 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
105 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
106 goto gen_matrix;
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
107 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
108 break;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
109
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
110 case 1:
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
111 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
112 octave_value tmp = args(idx);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
113
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
114 if (tmp.is_string ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
115 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
116 std::string s_arg = tmp.string_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
117
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
118 if (s_arg == "dist")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
119 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
120 retval = octave_rand::distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
121 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
122 else if (s_arg == "seed")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
123 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
124 retval = octave_rand::seed ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
125 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
126 else if (s_arg == "state" || s_arg == "twister")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
127 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
128 retval = octave_rand::state (fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
129 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
130 else if (s_arg == "uniform")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
131 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
132 octave_rand::uniform_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
133 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
134 else if (s_arg == "normal")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
135 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
136 octave_rand::normal_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
137 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
138 else if (s_arg == "exponential")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
139 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
140 octave_rand::exponential_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
141 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
142 else if (s_arg == "poisson")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
143 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
144 octave_rand::poisson_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
145 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
146 else if (s_arg == "gamma")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
147 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
148 octave_rand::gamma_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
149 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
150 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
151 error ("%s: unrecognized string argument", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
152 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
153 else if (tmp.is_scalar_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
154 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
155 double dval = tmp.double_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
156
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
157 if (xisnan (dval))
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
158 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
159 error ("%s: NaN is invalid matrix dimension", fcn);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
160 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
161 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
162 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
163 dims.resize (2);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
164
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
165 dims(0) = NINTbig (tmp.double_value ());
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
166 dims(1) = NINTbig (tmp.double_value ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
167
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
168 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
169 goto gen_matrix;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
170 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
171 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
172 else if (tmp.is_range ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
173 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
174 Range r = tmp.range_value ();
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
175
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
176 if (r.all_elements_are_ints ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
177 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
178 octave_idx_type n = r.nelem ();
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
179
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
180 dims.resize (n);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
181
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
182 octave_idx_type base = NINTbig (r.base ());
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
183 octave_idx_type incr = NINTbig (r.inc ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
184
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
185 for (octave_idx_type i = 0; i < n; i++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
186 {
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
187 //Negative dimensions are treated as zero for Matlab
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
188 //compatibility
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
189 dims(i) = base >= 0 ? base : 0;
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
190 base += incr;
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
191 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
192
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
193 goto gen_matrix;
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
194
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
195 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
196 else
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
197 error ("%s: all elements of range must be integers",
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
198 fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
199 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
200 else if (tmp.is_matrix_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
201 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
202 Array<int> iv = tmp.int_vector_value (true);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
203
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
204 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
205 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
206 octave_idx_type len = iv.length ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
207
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
208 dims.resize (len);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
209
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
210 for (octave_idx_type i = 0; i < len; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
211 {
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
212 //Negative dimensions are treated as zero for Matlab
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
213 //compatibility
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
214 octave_idx_type elt = iv(i);
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
215 dims(i) = elt >=0 ? elt : 0;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
216 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
217
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
218 goto gen_matrix;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
219 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
220 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
221 error ("%s: expecting integer vector", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
222 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
223 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
224 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
225 gripe_wrong_type_arg ("rand", tmp);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
226 return retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
227 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
228 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
229 break;
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
230
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
231 default:
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
232 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
233 octave_value tmp = args(idx);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
234
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
235 if (nargin == 2 && tmp.is_string ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
236 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
237 std::string ts = tmp.string_value ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents: 4665
diff changeset
238
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
239 if (ts == "seed")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
240 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
241 if (args(idx+1).is_real_scalar ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
242 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
243 double d = args(idx+1).double_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
244
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
245 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
246 octave_rand::seed (d);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
247 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
248 else if (args(idx+1).is_string ()
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
249 && args(idx+1).string_value() == "reset")
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
250 octave_rand::reset ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
251 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
252 error ("%s: seed must be a real scalar", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
253 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
254 else if (ts == "state" || ts == "twister")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
255 {
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
256 if (args(idx+1).is_string ()
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
257 && args(idx+1).string_value() == "reset")
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
258 octave_rand::reset (fcn);
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
259 else
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
260 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
261 ColumnVector s =
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
262 ColumnVector (args(idx+1).vector_value(false, true));
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
263
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
264 if (! error_state)
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
265 octave_rand::state (s, fcn);
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
266 }
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
267 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
268 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
269 error ("%s: unrecognized string argument", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
270 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
271 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
272 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
273 dims.resize (nargin);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
274
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
275 for (int i = 0; i < nargin; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
276 {
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
277 octave_idx_type elt = args(idx+i).int_value ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
278 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
279 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
280 error ("%s: expecting integer arguments", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
281 goto done;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
282 }
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
283 //Negative is zero for Matlab compatibility
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
284 dims(i) = elt >= 0 ? elt : 0;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
285 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
286
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
287 goto gen_matrix;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
288 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
289 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
290 break;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
291 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
292
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
293 done:
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
294
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
295 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
296
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
297 gen_matrix:
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
298
5355
cf44c749ba52 [project @ 2005-05-18 11:43:37 by jwe]
jwe
parents: 5307
diff changeset
299 dims.chop_trailing_singletons ();
cf44c749ba52 [project @ 2005-05-18 11:43:37 by jwe]
jwe
parents: 5307
diff changeset
300
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
301 if (additional_arg)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
302 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
303 if (a.length() == 1)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
304 return octave_rand::nd_array (dims, a(0));
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
305 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
306 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
307 if (a.dims() != dims)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
308 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
309 error ("%s: mismatch in argument size", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
310 return retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
311 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
312 octave_idx_type len = a.length ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
313 NDArray m (dims);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
314 double *v = m.fortran_vec ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
315 for (octave_idx_type i = 0; i < len; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
316 v[i] = octave_rand::scalar (a(i));
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
317 return m;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
318 }
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
319 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
320 else
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
321 return octave_rand::nd_array (dims);
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
322 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
323
4665
dece11da64ed [project @ 2003-11-25 15:37:32 by jwe]
jwe
parents: 4664
diff changeset
324 DEFUN_DLD (rand, args, ,
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
325 "-*- texinfo -*-\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
326 @deftypefn {Loadable Function} {} rand (@var{n})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
327 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m}, @dots{})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
328 @deftypefnx {Loadable Function} {} rand ([@var{n} @var{m} @dots{}])\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
329 @deftypefnx {Loadable Function} {@var{v} =} rand (\"state\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
330 @deftypefnx {Loadable Function} {} rand (\"state\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
331 @deftypefnx {Loadable Function} {} rand (\"state\", \"reset\")\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
332 @deftypefnx {Loadable Function} {@var{v} =} rand (\"seed\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
333 @deftypefnx {Loadable Function} {} rand (\"seed\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
334 @deftypefnx {Loadable Function} {} rand (\"seed\", \"reset\")\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
335 Return a matrix with random elements uniformly distributed on the\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
336 interval (0, 1). The arguments are handled the same as the arguments\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
337 for @code{eye}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
338 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
339 You can query the state of the random number generator using the\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
340 form\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
341 \n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
342 @example\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
343 v = rand (\"state\")\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
344 @end example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
345 \n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
346 This returns a column vector @var{v} of length 625. Later, you can\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
347 restore the random number generator to the state @var{v}\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
348 using the form\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
349 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
350 @example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
351 rand (\"state\", v)\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
352 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
353 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
354 @noindent\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
355 You may also initialize the state vector from an arbitrary vector of\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
356 length @leq{} 625 for @var{v}. This new state will be a hash based on the\n\
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5782
diff changeset
357 value of @var{v}, not @var{v} itself.\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
358 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
359 By default, the generator is initialized from @code{/dev/urandom} if it is\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
360 available, otherwise from CPU time, wall clock time, and the current\n\
14610
41d7e23f5734 Document rand's seed difference from Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14501
diff changeset
361 fraction of a second. Note that this differs from @sc{Matlab}, which\n\
41d7e23f5734 Document rand's seed difference from Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14501
diff changeset
362 always initializes the state to the same state at startup. To obtain\n\
41d7e23f5734 Document rand's seed difference from Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14501
diff changeset
363 behavior comparable to @sc{Matlab}, initialize with a deterministic state\n\
41d7e23f5734 Document rand's seed difference from Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14501
diff changeset
364 vector in Octave's startup files (@pxref{Startup Files}).\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
365 \n\
8325
b93ac0586e4b spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents: 7780
diff changeset
366 To compute the pseudo-random sequence, @code{rand} uses the Mersenne\n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
367 Twister with a period of @math{2^{19937}-1} (See M. Matsumoto and\n\
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
368 T. Nishimura,\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
369 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform\n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
370 pseudorandom number generator}, ACM Trans. on\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
371 Modeling and Computer Simulation Vol. 8, No. 1, pp. 3-30, January 1998,\n\
7171
9bc096bc59d4 [project @ 2007-11-13 18:02:51 by jwe]
jwe
parents: 7096
diff changeset
372 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
373 Do @strong{not} use for cryptography without securely hashing\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
374 several returned values together, otherwise the generator state\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
375 can be learned after reading 624 consecutive values.\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
376 \n\
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
377 Older versions of Octave used a different random number generator.\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
378 The new generator is used by default\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
379 as it is significantly faster than the old generator, and produces\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
380 random numbers with a significantly longer cycle time. However, in\n\
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5782
diff changeset
381 some circumstances it might be desirable to obtain the same random\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
382 sequences as used by the old generators. To do this the keyword\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
383 \"seed\" is used to specify that the old generators should be use,\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
384 as in\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
385 \n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
386 @example\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
387 rand (\"seed\", val)\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
388 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
389 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
390 @noindent\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
391 which sets the seed of the generator to @var{val}. The seed of the\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
392 generator can be queried with\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
393 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
394 @example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
395 s = rand (\"seed\")\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
396 @end example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
397 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
398 However, it should be noted that querying the seed will not cause\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
399 @code{rand} to use the old generators, only setting the seed will.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
400 To cause @code{rand} to once again use the new generators, the\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
401 keyword \"state\" should be used to reset the state of the @code{rand}.\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
402 \n\
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
403 The state or seed of the generator can be reset to a new random value\n\
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
404 using the \"reset\" keyword.\n\
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5782
diff changeset
405 @seealso{randn, rande, randg, randp}\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
406 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
407 {
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
408 octave_value retval;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
409
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
410 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
411
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
412 retval = do_rand (args, nargin, "rand", "uniform");
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
413
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
414 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
415 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
416
8871
fb1c929dbbb7 tests vs. 64-bit indexing
John W. Eaton <jwe@octave.org>
parents: 8828
diff changeset
417 // FIXME -- The old generator (selected when "seed" is set) will not
fb1c929dbbb7 tests vs. 64-bit indexing
John W. Eaton <jwe@octave.org>
parents: 8828
diff changeset
418 // work properly if compiled to use 64-bit integers.
fb1c929dbbb7 tests vs. 64-bit indexing
John W. Eaton <jwe@octave.org>
parents: 8828
diff changeset
419
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
420 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
421 %!test # "state" can be a scalar
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
422 %! rand ("state", 12); x = rand (1,4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
423 %! rand ("state", 12); y = rand (1,4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
424 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
425 %!test # "state" can be a vector
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
426 %! rand ("state", [12,13]); x = rand (1,4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
427 %! rand ("state", [12;13]); y = rand (1,4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
428 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
429 %!test # querying "state" doesn't disturb sequence
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
430 %! rand ("state", 12); rand (1,2); x = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
431 %! rand ("state", 12); rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
432 %! s = rand ("state"); y = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
433 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
434 %! rand ("state", s); z = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
435 %! assert (x, z);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
436 %!test # "seed" must be a scalar
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
437 %! rand ("seed", 12); x = rand (1,4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
438 %! rand ("seed", 12); y = rand (1,4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
439 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
440 %!error <seed must be a real scalar> rand ("seed", [12,13])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
441 %!test # querying "seed" returns a value which can be used later
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
442 %! s = rand ("seed"); x = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
443 %! rand ("seed", s); y = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
444 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
445 %!test # querying "seed" doesn't disturb sequence
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
446 %! rand ("seed", 12); rand (1,2); x = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
447 %! rand ("seed", 12); rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
448 %! s = rand ("seed"); y = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
449 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
450 %! rand ("seed", s); z = rand (1,2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
451 %! assert (x, z);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
452 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
453
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
454 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
455 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
456 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
457 %! rand ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
458 %! assert (rand (1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
459 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
460 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
461 %! rand ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
462 %! assert (rand (1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759], 1e-6);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
463 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
464 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
465 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
466 %! rand ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
467 %! x = rand (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
468 %! assert (max (x) < 1); #*** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
469 %! assert (min (x) > 0); #*** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
470 %! assert (mean (x), 0.5, 0.0024);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
471 %! assert (var (x), 1/48, 0.0632);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
472 %! assert (skewness (x), 0, 0.012);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
473 %! assert (kurtosis (x), -6/5, 0.0094);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
474 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
475 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
476 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
477 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
478 %! rand ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
479 %! x = rand (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
480 %! assert (max (x) < 1); #*** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
481 %! assert (min (x) > 0); #*** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
482 %! assert (mean (x), 0.5, 0.0024);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
483 %! assert (var (x), 1/48, 0.0632);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
484 %! assert (skewness (x), 0, 0.012);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
485 %! assert (kurtosis (x), -6/5, 0.0094);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
486 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
487 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
488
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
489 static std::string current_distribution = octave_rand::distribution ();
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
490
4665
dece11da64ed [project @ 2003-11-25 15:37:32 by jwe]
jwe
parents: 4664
diff changeset
491 DEFUN_DLD (randn, args, ,
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
492 "-*- texinfo -*-\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
493 @deftypefn {Loadable Function} {} randn (@var{n})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
494 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m}, @dots{})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
495 @deftypefnx {Loadable Function} {} randn ([@var{n} @var{m} @dots{}])\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
496 @deftypefnx {Loadable Function} {@var{v} =} randn (\"state\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
497 @deftypefnx {Loadable Function} {} randn (\"state\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
498 @deftypefnx {Loadable Function} {} randn (\"state\", \"reset\")\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
499 @deftypefnx {Loadable Function} {@var{v} =} randn (\"seed\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
500 @deftypefnx {Loadable Function} {} randn (\"seed\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
501 @deftypefnx {Loadable Function} {} randn (\"seed\", \"reset\")\n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10155
diff changeset
502 Return a matrix with normally distributed random\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
503 elements having zero mean and variance one. The arguments are\n\
7780
08125419efcb Extend explanation of randn's return value
Thomas Weber <thomas.weber.mail@gmail.com>
parents: 7533
diff changeset
504 handled the same as the arguments for @code{rand}.\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
505 \n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
506 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique''\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
507 to transform from a uniform to a normal distribution.\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
508 \n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
509 Reference: G. Marsaglia and W.W. Tsang,\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
510 @cite{Ziggurat Method for Generating Random Variables},\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
511 J. Statistical Software, vol 5, 2000,\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
512 @url{http://www.jstatsoft.org/v05/i08/})\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
513 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
514 @seealso{rand, rande, randg, randp}\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
515 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
516 {
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
517 octave_value retval;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
518
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
519 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
520
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
521 retval = do_rand (args, nargin, "randn", "normal");
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
522
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
523 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
524 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
525
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
526 /*
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
527 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
528 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
529 %! randn ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
530 %! assert (randn (1, 6), [-2.666521678978671 -0.7381719971724564 1.507903992673601 0.6019427189162239 -0.450661261143348 -0.7054431351574116], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
531 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
532 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
533 %! randn ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
534 %! assert (randn (1, 6), [-1.039402365684509 -1.25938892364502 0.1968704611063004 0.3874166905879974 -0.5976632833480835 -0.6615074276924133], 1e-6);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
535 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
536 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
537 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
538 %! randn ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
539 %! x = randn (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
540 %! assert (mean (x), 0, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
541 %! assert (var (x), 1, 0.02);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
542 %! assert (skewness (x), 0, 0.02);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
543 %! assert (kurtosis (x), 0, 0.04);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
544 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
545 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
546 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
547 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
548 %! randn ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
549 %! x = randn (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
550 %! assert (mean (x), 0, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
551 %! assert (var (x), 1, 0.02);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
552 %! assert (skewness (x), 0, 0.02);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
553 %! assert (kurtosis (x), 0, 0.04);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
554 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
555 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
556
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
557 DEFUN_DLD (rande, args, ,
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
558 "-*- texinfo -*-\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
559 @deftypefn {Loadable Function} {} rande (@var{n})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
560 @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m}, @dots{})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
561 @deftypefnx {Loadable Function} {} rande ([@var{n} @var{m} @dots{}])\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
562 @deftypefnx {Loadable Function} {@var{v} =} rande (\"state\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
563 @deftypefnx {Loadable Function} {} rande (\"state\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
564 @deftypefnx {Loadable Function} {} rande (\"state\", \"reset\")\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
565 @deftypefnx {Loadable Function} {@var{v} =} rande (\"seed\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
566 @deftypefnx {Loadable Function} {} rande (\"seed\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
567 @deftypefnx {Loadable Function} {} rande (\"seed\", \"reset\")\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
568 Return a matrix with exponentially distributed random elements. The\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
569 arguments are handled the same as the arguments for @code{rand}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
570 \n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
571 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique''\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
572 to transform from a uniform to an exponential distribution.\n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
573 \n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
574 Reference: G. Marsaglia and W.W. Tsang,\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
575 @cite{Ziggurat Method for Generating Random Variables},\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
576 J. Statistical Software, vol 5, 2000,\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
577 @url{http://www.jstatsoft.org/v05/i08/})\n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
578 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
579 @seealso{rand, randn, randg, randp}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
580 @end deftypefn")
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
581 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
582 octave_value retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
583
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
584 int nargin = args.length ();
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
585
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
586 retval = do_rand (args, nargin, "rande", "exponential");
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
587
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
588 return retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
589 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
590
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
591 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
592 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
593 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
594 %! rande ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
595 %! assert (rande (1, 6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
596 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
597 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
598 %! rande ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
599 %! assert (rande (1, 6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087], 1e-6);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
600 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
601 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
602 %! # statistical tests may fail occasionally
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
603 %! rande ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
604 %! x = rande (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
605 %! assert (min (x) > 0); # *** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
606 %! assert (mean (x), 1, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
607 %! assert (var (x), 1, 0.03);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
608 %! assert (skewness (x), 2, 0.06);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
609 %! assert (kurtosis (x), 6, 0.7);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
610 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
611 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
612 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
613 %! # statistical tests may fail occasionally
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
614 %! rande ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
615 %! x = rande (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
616 %! assert (min (x)>0); # *** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
617 %! assert (mean (x), 1, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
618 %! assert (var (x), 1, 0.03);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
619 %! assert (skewness (x), 2, 0.06);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
620 %! assert (kurtosis (x), 6, 0.7);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
621 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
622 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
623
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
624 DEFUN_DLD (randg, args, ,
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
625 "-*- texinfo -*-\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
626 @deftypefn {Loadable Function} {} randg (@var{n})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
627 @deftypefnx {Loadable Function} {} randg (@var{n}, @var{m}, @dots{})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
628 @deftypefnx {Loadable Function} {} randg ([@var{n} @var{m} @dots{}])\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
629 @deftypefnx {Loadable Function} {@var{v} =} randg (\"state\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
630 @deftypefnx {Loadable Function} {} randg (\"state\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
631 @deftypefnx {Loadable Function} {} randg (\"state\", \"reset\")\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
632 @deftypefnx {Loadable Function} {@var{v} =} randg (\"seed\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
633 @deftypefnx {Loadable Function} {} randg (\"seed\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
634 @deftypefnx {Loadable Function} {} randg (\"seed\", \"reset\")\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
635 Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
636 The arguments are handled the same as the arguments for @code{rand},\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
637 except for the argument @var{a}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
638 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
639 This can be used to generate many distributions:\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
640 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
641 @table @asis\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
642 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
643 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
644 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
645 r = b * randg (a)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
646 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
647 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
648 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
649 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
650 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
651 @group\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
652 r1 = randg (a, 1)\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
653 r = r1 / (r1 + randg (b, 1))\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
654 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
655 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
656 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
657 @item @code{Erlang (a, n)}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
658 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
659 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
660 r = a * randg (n)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
661 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
662 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
663 @item @code{chisq (df)} for @code{df > 0}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
664 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
665 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
666 r = 2 * randg (df / 2)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
667 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
668 \n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
669 @item @code{t (df)} for @code{0 < df < inf} (use randn if df is infinite)\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
670 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
671 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
672 r = randn () / sqrt (2 * randg (df / 2) / df)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
673 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
674 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
675 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
676 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
677 @example\n\
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
678 @group\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
679 ## r1 equals 1 if n1 is infinite\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
680 r1 = 2 * randg (n1 / 2) / n1\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
681 ## r2 equals 1 if n2 is infinite\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
682 r2 = 2 * randg (n2 / 2) / n2\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
683 r = r1 / r2\n\n\
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
684 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
685 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
686 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
687 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
688 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
689 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
690 r = randp ((1 - p) / p * randg (n))\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
691 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
692 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
693 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
694 (use chisq if @code{L = 0})\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
695 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
696 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
697 @group\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
698 r = randp (L / 2)\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
699 r(r > 0) = 2 * randg (r(r > 0))\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
700 r(df > 0) += 2 * randg (df(df > 0)/2)\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
701 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
702 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
703 \n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
704 @item @code{Dirichlet (a1, @dots{} ak)}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
705 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
706 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
707 @group\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
708 r = (randg (a1), @dots{}, randg (ak))\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
709 r = r / sum (r)\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
710 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
711 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
712 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
713 @end table\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
714 @seealso{rand, randn, rande, randp}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
715 @end deftypefn")
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
716 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
717 octave_value retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
718
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
719 int nargin = args.length ();
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
720
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
721 if (nargin < 1)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
722 error ("randg: insufficient arguments");
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
723 else
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
724 retval = do_rand (args, nargin, "randg", "gamma", true);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
725
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
726 return retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
727 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
728
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
729 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
730 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
731 %! randg ("state", 12)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
732 %! assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]); # *** Please report
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
733
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
734 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
735 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
736 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
737 %! assert (randg (0.1, 1, 6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
738 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
739 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
740 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
741 %! assert (randg (0.95, 1, 6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
742 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
743 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
744 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
745 %! assert (randg (1, 1, 6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
746 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
747 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
748 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
749 %! assert (randg (10, 1, 6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059], 1e-5);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
750 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
751 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
752 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
753 %! assert (randg (100, 1, 6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118], 1e-4);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
754 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
755 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
756 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
757 %! assert (randg (0.1, 1, 6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
758 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
759 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
760 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
761 %! assert (randg (0.95, 1, 6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
762 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
763 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
764 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
765 %! assert (randg (1, 1, 6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303], 1e-6);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
766 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
767 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
768 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
769 %! assert (randg (10, 1, 6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186], 1e-5);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
770 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
771 %! # Test fixed seed
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
772 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
773 %! assert (randg (100, 1, 6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625], 1e-4);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
774
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
775 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
776 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
777 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
778 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
779 %! a = 0.1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
780 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
781 %! assert (mean (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
782 %! assert (var (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
783 %! assert (skewness (x), 2/sqrt (a), 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
784 %! assert (kurtosis (x), 6/a, 50);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
785 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
786 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
787 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
788 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
789 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
790 %! a = 0.95;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
791 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
792 %! assert (mean (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
793 %! assert (var (x), a, 0.04);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
794 %! assert (skewness (x), 2/sqrt (a), 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
795 %! assert (kurtosis (x), 6/a, 2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
796 %! endif
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
797 %!test
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
798 %! if (__random_statistical_tests__)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
799 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
800 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
801 %! a = 1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
802 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
803 %! assert (mean (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
804 %! assert (var (x), a, 0.04);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
805 %! assert (skewness (x), 2/sqrt (a), 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
806 %! assert (kurtosis (x), 6/a, 2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
807 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
808 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
809 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
810 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
811 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
812 %! a = 10;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
813 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
814 %! assert (mean (x), a, 0.1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
815 %! assert (var (x), a, 0.5);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
816 %! assert (skewness (x), 2/sqrt (a), 0.1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
817 %! assert (kurtosis (x), 6/a, 0.5);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
818 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
819 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
820 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
821 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
822 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
823 %! a = 100;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
824 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
825 %! assert (mean (x), a, 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
826 %! assert (var (x), a, 2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
827 %! assert (skewness (x), 2/sqrt (a), 0.05);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
828 %! assert (kurtosis (x), 6/a, 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
829 %! endif
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
830 %!test
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
831 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
832 %!assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]) # *** Please report
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
833 %!test
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
834 %! if (__random_statistical_tests__)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
835 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
836 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
837 %! a = 0.1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
838 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
839 %! assert (mean (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
840 %! assert (var (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
841 %! assert (skewness (x), 2/sqrt (a), 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
842 %! assert (kurtosis (x), 6/a, 50);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
843 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
844 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
845 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
846 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
847 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
848 %! a = 0.95;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
849 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
850 %! assert (mean (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
851 %! assert (var (x), a, 0.04);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
852 %! assert (skewness (x), 2/sqrt (a), 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
853 %! assert (kurtosis (x), 6/a, 2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
854 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
855 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
856 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
857 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
858 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
859 %! a = 1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
860 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
861 %! assert (mean (x), a, 0.01);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
862 %! assert (var (x), a, 0.04);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
863 %! assert (skewness (x), 2/sqrt (a), 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
864 %! assert (kurtosis (x), 6/a, 2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
865 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
866 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
867 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
868 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
869 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
870 %! a = 10;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
871 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
872 %! assert (mean (x), a, 0.1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
873 %! assert (var (x), a, 0.5);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
874 %! assert (skewness (x), 2/sqrt (a), 0.1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
875 %! assert (kurtosis (x), 6/a, 0.5);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
876 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
877 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
878 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
879 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
880 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
881 %! a = 100;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
882 %! x = randg (a, 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
883 %! assert (mean (x), a, 0.2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
884 %! assert (var (x), a, 2);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
885 %! assert (skewness (x), 2/sqrt (a), 0.05);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
886 %! assert (kurtosis (x), 6/a, 0.2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
887 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
888 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
889
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
890 DEFUN_DLD (randp, args, ,
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
891 "-*- texinfo -*-\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
892 @deftypefn {Loadable Function} {} randp (@var{l}, @var{n})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
893 @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m}, @dots{})\n\
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
894 @deftypefnx {Loadable Function} {} randp (@var{l}, [@var{n} @var{m} @dots{}])\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
895 @deftypefnx {Loadable Function} {@var{v} =} randp (\"state\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
896 @deftypefnx {Loadable Function} {} randp (\"state\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
897 @deftypefnx {Loadable Function} {} randp (\"state\", \"reset\")\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
898 @deftypefnx {Loadable Function} {@var{v} =} randp (\"seed\")\n\
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
899 @deftypefnx {Loadable Function} {} randp (\"seed\", @var{v})\n\
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
900 @deftypefnx {Loadable Function} {} randp (\"seed\", \"reset\")\n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10155
diff changeset
901 Return a matrix with Poisson distributed random elements with mean value\n\
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10155
diff changeset
902 parameter given by the first argument, @var{l}. The arguments\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
903 are handled the same as the arguments for @code{rand}, except for the\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
904 argument @var{l}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
905 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
906 Five different algorithms are used depending on the range of @var{l}\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
907 and whether or not @var{l} is a scalar or a matrix.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
908 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
909 @table @asis\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
910 @item For scalar @var{l} @leq{} 12, use direct method.\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
911 W.H. Press, et al., @cite{Numerical Recipes in C},\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
912 Cambridge University Press, 1992.\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
913 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
914 @item For scalar @var{l} > 12, use rejection method.[1]\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
915 W.H. Press, et al., @cite{Numerical Recipes in C},\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
916 Cambridge University Press, 1992.\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
917 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
918 @item For matrix @var{l} @leq{} 10, use inversion method.[2]\n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
919 E. Stadlober, et al., WinRand source code, available via FTP.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
920 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
921 @item For matrix @var{l} > 10, use patchwork rejection method.\n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
922 E. Stadlober, et al., WinRand source code, available via FTP, or\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
923 H. Zechner, @cite{Efficient sampling from continuous and discrete\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
924 unimodal distributions}, Doctoral Dissertation, 156pp., Technical\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
925 University Graz, Austria, 1994.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
926 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
927 @item For @var{l} > 1e8, use normal approximation.\n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
928 L. Montanet, et al., @cite{Review of Particle Properties}, Physical Review\n\
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
929 D 50 p1284, 1994.\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
930 @end table\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
931 @seealso{rand, randn, rande, randg}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
932 @end deftypefn")
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
933 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
934 octave_value retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
935
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
936 int nargin = args.length ();
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
937
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
938 if (nargin < 1)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
939 error ("randp: insufficient arguments");
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
940 else
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
941 retval = do_rand (args, nargin, "randp", "poisson", true);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
942
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
943 return retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
944 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
945
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
946 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
947 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
948 %! randp ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
949 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
950 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
951 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
952 %! randp ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
953 %! assert (randp (5, 1, 6), [5 5 3 7 7 3])
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
954 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
955 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
956 %! randp ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
957 %! assert (randp (15, 1, 6), [13 15 8 18 18 15])
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
958 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
959 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
960 %! randp ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
961 %! assert (randp (1e9, 1, 6), [999915677 999976657 1000047684 1000019035 999985749 999977692], -1e-6)
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
962 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
963 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
964 %! randp ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
965 %! %%assert (randp (5, 1, 6), [8 2 3 6 6 8])
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
966 %! assert (randp (5, 1, 5), [8 2 3 6 6])
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
967 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
968 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
969 %! randp ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
970 %! assert (randp (15, 1, 6), [15 16 12 10 10 12])
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
971 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
972 %! # Test fixed state
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
973 %! randp ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
974 %! assert (randp (1e9, 1, 6), [1000006208 1000012224 999981120 999963520 999963072 999981440], -1e-6)
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
975 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
976 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
977 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
978 %! randp ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
979 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
980 %! x = randp (a (1), 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
981 %! assert (min (x) >= 0); # *** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
982 %! assert (mean (x), a(1), a(2));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
983 %! assert (var (x), a(1), 0.02*a(1));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
984 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
985 %! assert (kurtosis (x), 1/a(1), 3*a(3));
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
986 %! endfor
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
987 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
988 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
989 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
990 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
991 %! randp ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
992 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
993 %! x = randp (a(1)*ones (100000, 1), 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
994 %! assert (min (x) >= 0); # *** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
995 %! assert (mean (x), a(1), a(2));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
996 %! assert (var (x), a(1), 0.02*a(1));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
997 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
998 %! assert (kurtosis (x), 1/a(1), 3*a(3));
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
999 %! endfor
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1000 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1001 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1002 %! randp ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1003 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1004 %!test
6449
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1005 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1006 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1007 %! randp ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1008 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1009 %! x = randp (a(1), 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1010 %! assert (min (x) >= 0); # *** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1011 %! assert (mean (x), a(1), a(2));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1012 %! assert (var (x), a(1), 0.02*a(1));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1013 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1014 %! assert (kurtosis (x), 1/a(1), 3*a(3));
6449
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1015 %! endfor
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1016 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1017 %!test
6449
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1018 %! if (__random_statistical_tests__)
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1019 %! # statistical tests may fail occasionally.
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1020 %! randp ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1021 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1022 %! x = randp (a(1)*ones (100000, 1), 100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1023 %! assert (min (x) >= 0); # *** Please report this!!! ***
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1024 %! assert (mean (x), a(1), a(2));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1025 %! assert (var (x), a(1), 0.02*a(1));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1026 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1027 %! assert (kurtosis (x), 1/a(1), 3*a(3));
6449
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1028 %! endfor
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1029 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1030 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1031
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1032 DEFUN_DLD (randperm, args, ,
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1033 "-*- texinfo -*-\n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10155
diff changeset
1034 @deftypefn {Loadable Function} {} randperm (@var{n})\n\
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1035 @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1036 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1037 If @var{m} is supplied, return @var{m} unique entries, sampled without\n\
14038
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13739
diff changeset
1038 replacement from @code{1:@var{n}}. The complexity is O(@var{n}) in\n\
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1039 memory and O(@var{m}) in time, unless @var{m} < @var{n}/5, in which case\n\
14038
b0cdd60db5e5 doc: Grammarcheck documentation ahead of 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents: 13739
diff changeset
1040 O(@var{m}) memory is used as well. The randomization is performed using\n\
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1041 rand(). All permutations are equally likely.\n\
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1042 @seealso{perms}\n\
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1043 @end deftypefn")
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1044 {
13739
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1045
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1046 #ifdef USE_UNORDERED_MAP_WITH_TR1
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1047 using std::tr1::unordered_map;
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1048 #else
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1049 using std::unordered_map;
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1050 #endif
0206484682c6 rand.cc: Don't bring the whole std namespace into scope, only unordered_map
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13727
diff changeset
1051
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1052 int nargin = args.length ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1053 octave_value retval;
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1054
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1055 if (nargin == 1 || nargin == 2)
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1056 {
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1057 octave_idx_type n, m;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
1058
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1059 n = args(0).idx_type_value (true);
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1060
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1061 if (nargin == 2)
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1062 m = args(1).idx_type_value (true);
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1063 else
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1064 m = n;
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1065
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1066 if (m < 0 || n < 0)
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1067 error ("randperm: M and N must be non-negative");
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1068
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1069 if (m > n)
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1070 error ("randperm: M must be less than or equal to N");
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1071
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1072 // Quick and dirty heuristic to decide if we allocate or not the
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1073 // whole vector for tracking the truncated shuffle.
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1074 bool short_shuffle = m < n/5 && m < 1e5;
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1075
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1076 if (! error_state)
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1077 {
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1078 // Generate random numbers.
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1079 NDArray r = octave_rand::nd_array (dim_vector (1, m));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1080 double *rvec = r.fortran_vec ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1081
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1082 octave_idx_type idx_len = short_shuffle ? m : n;
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1083 Array<octave_idx_type> idx (dim_vector (1, idx_len));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1084 octave_idx_type *ivec = idx.fortran_vec ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1085
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1086 for (octave_idx_type i = 0; i < idx_len; i++)
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1087 ivec[i] = i;
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1088
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1089 if (short_shuffle)
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1090 {
13727
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
1091 unordered_map<octave_idx_type, octave_idx_type> map (m);
13255
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1092
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1093 // Perform the Knuth shuffle only keeping track of moved
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1094 // entries in the map
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1095 for (octave_idx_type i = 0; i < m; i++)
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1096 {
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1097 octave_idx_type k = i +
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1098 gnulib::floor (rvec[i] * (n - i));
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1099
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1100 if (map.find(k) == map.end())
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1101 {
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1102 map[k] = ivec[i];
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1103 ivec[i] = k;
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1104 }
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1105 else
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1106 std::swap (ivec[i], map[k]);
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1107
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1108 }
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1109 }
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1110 else
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1111 {
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1112
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1113 // Perform the Knuth shuffle of the first m entries
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1114 for (octave_idx_type i = 0; i < m; i++)
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1115 {
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1116 octave_idx_type k = i +
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1117 gnulib::floor (rvec[i] * (n - i));
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1118 std::swap (ivec[i], ivec[k]);
dd3c5325039c Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13186
diff changeset
1119 }
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1120 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1121
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1122 // Convert to doubles, reusing r.
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1123 for (octave_idx_type i = 0; i < m; i++)
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1124 rvec[i] = ivec[i] + 1;
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1125
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1126 if (m < n)
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1127 idx.resize (dim_vector (1, m));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1128
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1129 // Now create an array object with a cached idx_vector.
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
1130 retval = new octave_matrix (r, idx_vector (idx));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1131 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1132 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1133 else
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1134 print_usage ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1135
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1136 return retval;
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1137 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1138
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1139 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1140 %!assert (sort (randperm (20)), 1:20)
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1141 %!assert (length (randperm (20,10)), 10)
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1142 */