annotate libinterp/corefcn/rand.cc @ 18794:03c2671493f9

maint: Periodic merge of gui-release to default.
author Rik <rik@octave.org>
date Mon, 02 Jun 2014 11:17:59 -0700
parents a142f35f3cb6
children 0850b5212619
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
1
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
2 /*
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
3
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17662
diff changeset
4 Copyright (C) 1996-2013 John W. Eaton
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
5 Copyright (C) 2009 VZLU Prague
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
6
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
7 This file is part of Octave.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
8
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
9 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
10 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
11 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
12 option) any later version.
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
13
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
14 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
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
16 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
17 for more details.
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
18
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
19 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
20 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
21 <http://www.gnu.org/licenses/>.
2928
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
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
26 #include <config.h>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
27 #endif
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
28
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
29 #include <ctime>
13727
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
30 #if defined (HAVE_UNORDERED_MAP)
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
31 #include <unordered_map>
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
32 #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
33 #include <tr1/unordered_map>
13727
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
34 #endif
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
35 #include <string>
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
36
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
37 #include "f77-fcn.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
38 #include "lo-mappers.h"
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
39 #include "oct-rand.h"
4153
6b96ce9f5743 [project @ 2002-11-06 20:38:49 by jwe]
jwe
parents: 3887
diff changeset
40 #include "quit.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
41
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
42 #include "defun.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
43 #include "error.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
44 #include "gripes.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
45 #include "oct-obj.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
46 #include "unwind-prot.h"
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
47 #include "utils.h"
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
48 #include "ov-re-mat.h"
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
49
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
50 /*
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
51 %!shared __random_statistical_tests__
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
52 %! ## 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
53 %! __random_statistical_tests__ = 0;
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
54 */
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
55
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
56 static octave_value
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
57 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
58 const std::string& distribution, bool additional_arg = false)
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
59 {
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
60 octave_value retval;
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
61 NDArray a;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
62 int idx = 0;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
63 dim_vector dims;
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
64 bool is_single = false;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
65
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
66 unwind_protect frame;
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
67 // Restore current distribution on any exit.
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
68 frame.add_fcn (octave_rand::distribution,
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
69 octave_rand::distribution ());
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
70
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
71 octave_rand::distribution (distribution);
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
72
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
73 if (nargin > 0 && args(nargin-1).is_string ())
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
74 {
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
75 std::string s_arg = args(nargin-1).string_value ();
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
76
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
77 if (s_arg == "single")
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
78 {
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
79 is_single = true;
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
80 nargin--;
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
81 }
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
82 else if (s_arg == "double")
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
83 nargin--;
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
84 }
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
85
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
86 if (additional_arg)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
87 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
88 if (nargin == 0)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
89 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
90 error ("%s: expecting at least one argument", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
91 goto done;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
92 }
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
93 else if (args(0).is_string ())
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
94 additional_arg = false;
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
95 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
96 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
97 a = args(0).array_value ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
98 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
99 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
100 error ("%s: expecting scalar or matrix arguments", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
101 goto done;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
102 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
103 idx++;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
104 nargin--;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
105 }
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
106 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
107
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
108 switch (nargin)
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 0:
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 if (additional_arg)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
113 dims = a.dims ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
114 else
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 dims.resize (2);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
117
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
118 dims(0) = 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
119 dims(1) = 1;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
120 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
121 goto gen_matrix;
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
122 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
123 break;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
124
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
125 case 1:
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
126 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
127 octave_value tmp = args(idx);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
128
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
129 if (tmp.is_string ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
130 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
131 std::string s_arg = tmp.string_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
132
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
133 if (s_arg == "dist")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
134 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
135 retval = octave_rand::distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
136 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
137 else if (s_arg == "seed")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
138 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
139 retval = octave_rand::seed ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
140 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
141 else if (s_arg == "state" || s_arg == "twister")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
142 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
143 retval = octave_rand::state (fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
144 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
145 else if (s_arg == "uniform")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
146 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
147 octave_rand::uniform_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
148 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
149 else if (s_arg == "normal")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
150 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
151 octave_rand::normal_distribution ();
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 (s_arg == "exponential")
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 octave_rand::exponential_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
156 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
157 else if (s_arg == "poisson")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
158 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
159 octave_rand::poisson_distribution ();
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 if (s_arg == "gamma")
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 octave_rand::gamma_distribution ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
164 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
165 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
166 error ("%s: unrecognized string argument", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
167 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
168 else if (tmp.is_scalar_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
169 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
170 double dval = tmp.double_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
171
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
172 if (xisnan (dval))
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
173 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
174 error ("%s: NaN is invalid matrix dimension", fcn);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
175 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
176 else
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 dims.resize (2);
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(0) = NINTbig (tmp.double_value ());
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
181 dims(1) = NINTbig (tmp.double_value ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
182
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
183 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
184 goto gen_matrix;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
185 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
186 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
187 else if (tmp.is_range ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
188 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
189 Range r = tmp.range_value ();
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
190
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
191 if (r.all_elements_are_ints ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
192 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
193 octave_idx_type n = r.nelem ();
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
194
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
195 dims.resize (n);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
196
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
197 octave_idx_type base = NINTbig (r.base ());
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
198 octave_idx_type incr = NINTbig (r.inc ());
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
199
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
200 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
201 {
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
202 //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
203 //compatibility
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
204 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
205 base += incr;
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
206 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
207
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
208 goto gen_matrix;
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
209
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
210 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
211 else
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
212 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
213 fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
214 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
215 else if (tmp.is_matrix_type ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
216 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
217 Array<int> iv = tmp.int_vector_value (true);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
218
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
219 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
220 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
221 octave_idx_type len = iv.length ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
222
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
223 dims.resize (len);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
224
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
225 for (octave_idx_type i = 0; i < len; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
226 {
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
227 //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
228 //compatibility
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
229 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
230 dims(i) = elt >=0 ? elt : 0;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
231 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
232
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
233 goto gen_matrix;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
234 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
235 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
236 error ("%s: expecting integer vector", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
237 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
238 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
239 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
240 gripe_wrong_type_arg ("rand", tmp);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
241 return retval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
242 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
243 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
244 break;
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
245
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
246 default:
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
247 {
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
248 octave_value tmp = args(idx);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
249
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
250 if (nargin == 2 && tmp.is_string ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
251 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
252 std::string ts = tmp.string_value ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents: 4665
diff changeset
253
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
254 if (ts == "seed")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
255 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
256 if (args(idx+1).is_real_scalar ())
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
257 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
258 double d = args(idx+1).double_value ();
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
259
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
260 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
261 octave_rand::seed (d);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
262 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
263 else if (args(idx+1).is_string ()
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
264 && args(idx+1).string_value () == "reset")
10709
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::reset ();
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
266 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
267 error ("%s: seed must be a real scalar", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
268 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
269 else if (ts == "state" || ts == "twister")
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
270 {
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
271 if (args(idx+1).is_string ()
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
272 && args(idx+1).string_value () == "reset")
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
273 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
274 else
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
275 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
276 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
277 ColumnVector (args(idx+1).vector_value(false, true));
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
278
10709
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
279 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
280 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
281 }
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
282 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
283 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
284 error ("%s: unrecognized string argument", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
285 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
286 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
287 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
288 dims.resize (nargin);
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
289
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
290 for (int i = 0; i < nargin; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
291 {
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
292 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
293 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
294 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
295 error ("%s: expecting integer arguments", fcn);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
296 goto done;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
297 }
12905
f7a8d1dafda3 Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 12639
diff changeset
298 //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
299 dims(i) = elt >= 0 ? elt : 0;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
300 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
301
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
302 goto gen_matrix;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
303 }
4543
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
304 }
79df15d4470c [project @ 2003-10-18 03:53:52 by jwe]
jwe
parents: 4307
diff changeset
305 break;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
306 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
307
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
308 done:
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
309
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
310 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
311
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
312 gen_matrix:
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
313
5355
cf44c749ba52 [project @ 2005-05-18 11:43:37 by jwe]
jwe
parents: 5307
diff changeset
314 dims.chop_trailing_singletons ();
cf44c749ba52 [project @ 2005-05-18 11:43:37 by jwe]
jwe
parents: 5307
diff changeset
315
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
316 if (is_single)
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
317 {
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
318 if (additional_arg)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
319 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
320 if (a.length () == 1)
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
321 return octave_rand::float_nd_array (dims, a(0));
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
322 else
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
323 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
324 if (a.dims () != dims)
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
325 {
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
326 error ("%s: mismatch in argument size", fcn);
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
327 return retval;
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
328 }
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
329 octave_idx_type len = a.length ();
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
330 FloatNDArray m (dims);
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
331 float *v = m.fortran_vec ();
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
332 for (octave_idx_type i = 0; i < len; i++)
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
333 v[i] = octave_rand::float_scalar (a(i));
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
334 return m;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
335 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 10066
diff changeset
336 }
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
337 else
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
338 return octave_rand::float_nd_array (dims);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
339 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
340 else
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
341 {
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
342 if (additional_arg)
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
343 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
344 if (a.length () == 1)
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
345 return octave_rand::nd_array (dims, a(0));
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
346 else
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
347 {
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14706
diff changeset
348 if (a.dims () != dims)
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
349 {
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
350 error ("%s: mismatch in argument size", fcn);
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
351 return retval;
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
352 }
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
353 octave_idx_type len = a.length ();
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
354 NDArray m (dims);
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
355 double *v = m.fortran_vec ();
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
356 for (octave_idx_type i = 0; i < len; i++)
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
357 v[i] = octave_rand::scalar (a(i));
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
358 return m;
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
359 }
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
360 }
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
361 else
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
362 return octave_rand::nd_array (dims);
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
363 }
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
364 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
365
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
366 DEFUN (rand, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
367 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
368 @deftypefn {Built-in Function} {} rand (@var{n})\n\
18436
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
369 @deftypefnx {Built-in Function} {} rand (@var{m}, @var{n}, @dots{})\n\
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
370 @deftypefnx {Built-in Function} {} rand ([@var{m} @var{n} @dots{}])\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
371 @deftypefnx {Built-in Function} {@var{v} =} rand (\"state\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
372 @deftypefnx {Built-in Function} {} rand (\"state\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
373 @deftypefnx {Built-in Function} {} rand (\"state\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
374 @deftypefnx {Built-in Function} {@var{v} =} rand (\"seed\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
375 @deftypefnx {Built-in Function} {} rand (\"seed\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
376 @deftypefnx {Built-in Function} {} rand (\"seed\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
377 @deftypefnx {Built-in Function} {} rand (@dots{}, \"single\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
378 @deftypefnx {Built-in Function} {} rand (@dots{}, \"double\")\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
379 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
380 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
381 for @code{eye}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
382 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
383 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
384 form\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 v = rand (\"state\")\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
388 @end example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
389 \n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
390 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
391 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
392 using the form\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 rand (\"state\", v)\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
396 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
397 \n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
398 @noindent\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
399 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
400 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
401 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
402 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
403 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
404 available, otherwise from CPU time, wall clock time, and the current\n\
14620
cd375519eab0 doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents: 14610
diff changeset
405 fraction of a second. Note that this differs from @sc{matlab}, which\n\
14610
41d7e23f5734 Document rand's seed difference from Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14501
diff changeset
406 always initializes the state to the same state at startup. To obtain\n\
14620
cd375519eab0 doc: Periodic grammar check of documentation
Rik <octave@nomad.inbox5.com>
parents: 14610
diff changeset
407 behavior comparable to @sc{matlab}, initialize with a deterministic state\n\
14610
41d7e23f5734 Document rand's seed difference from Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14501
diff changeset
408 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
409 \n\
8325
b93ac0586e4b spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents: 7780
diff changeset
410 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
411 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
412 T. Nishimura,\n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
413 @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
414 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
415 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
416 @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
417 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
418 several returned values together, otherwise the generator state\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
419 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
420 \n\
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
421 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
422 The new generator is used by default\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
423 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
424 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
425 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
426 sequences as used by the old generators. To do this the keyword\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
427 @qcode{\"seed\"} is used to specify that the old generators should be use,\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
428 as in\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
429 \n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
430 @example\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
431 rand (\"seed\", val)\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
432 @end example\n\
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
433 \n\
10846
a4f482e66b65 Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents: 10840
diff changeset
434 @noindent\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
435 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
436 generator can be queried with\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
437 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
438 @example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
439 s = rand (\"seed\")\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
440 @end example\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
441 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
442 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
443 @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
444 To cause @code{rand} to once again use the new generators, the\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
445 keyword @qcode{\"state\"} should be used to reset the state of the\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
446 @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
447 \n\
92a85ed5b86e Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents: 10687
diff changeset
448 The state or seed of the generator can be reset to a new random value\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
449 using the @qcode{\"reset\"} keyword.\n\
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
450 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
451 The class of the value returned can be controlled by a trailing\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
452 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
453 classes.\n\
5798
7e7ed81f5566 [project @ 2006-05-09 17:24:33 by jwe]
jwe
parents: 5782
diff changeset
454 @seealso{randn, rande, randg, randp}\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
455 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
456 {
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
457 octave_value retval;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
458
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
459 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
460
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
461 retval = do_rand (args, nargin, "rand", "uniform");
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
462
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
463 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
464 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
465
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
466 // FIXME: The old generator (selected when "seed" is set) will not
8871
fb1c929dbbb7 tests vs. 64-bit indexing
John W. Eaton <jwe@octave.org>
parents: 8828
diff changeset
467 // 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
468
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
469 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
470 %!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
471 %! 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
472 %! 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
473 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
474 %!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
475 %! 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
476 %! 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
477 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
478 %!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
479 %! 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
480 %! 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
481 %! 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
482 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
483 %! 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
484 %! assert (x, z);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
485 %!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
486 %! 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
487 %! 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
488 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
489 %!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
490 %!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
491 %! 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
492 %! 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
493 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
494 %!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
495 %! 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
496 %! 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
497 %! 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
498 %! assert (x, y);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
499 %! 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
500 %! assert (x, z);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
501 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
502
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
503 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
504 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
505 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
506 %! rand ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
507 %! 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
508 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
509 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
510 %! rand ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
511 %! 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
512 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
513 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
514 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
515 %! rand ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
516 %! x = rand (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
517 %! 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
518 %! 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
519 %! 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
520 %! 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
521 %! 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
522 %! assert (kurtosis (x), -6/5, 0.0094);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
523 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
524 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
525 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
526 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
527 %! rand ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
528 %! x = rand (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
529 %! 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
530 %! 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
531 %! 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
532 %! 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
533 %! 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
534 %! assert (kurtosis (x), -6/5, 0.0094);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
535 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
536 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
537
17258
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
538 /*
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
539 %!# Test out-of-range values as rand() seeds. See oct-rand.cc: double2uint32().
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
540 %!function v = __rand_sample__ (initval)
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
541 %! rand ("state", initval);
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
542 %! v = rand (1, 6);
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
543 %!endfunction
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
544 %!
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
545 %!assert (__rand_sample__ (0), __rand_sample__ (2^32))
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
546 %!assert (__rand_sample__ (-2), __rand_sample__ (2^32-2))
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
547 %!assert (__rand_sample__ (Inf), __rand_sample__ (NaN))
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
548 %!assert (! isequal (__rand_sample__ (-1), __rand_sample__ (-2)))
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
549 */
213ee68b59da Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents: 15195
diff changeset
550
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
551 static std::string current_distribution = octave_rand::distribution ();
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
552
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
553 DEFUN (randn, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
554 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
555 @deftypefn {Built-in Function} {} randn (@var{n})\n\
18436
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
556 @deftypefnx {Built-in Function} {} randn (@var{m}, @var{n}, @dots{})\n\
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
557 @deftypefnx {Built-in Function} {} randn ([@var{m} @var{n} @dots{}])\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
558 @deftypefnx {Built-in Function} {@var{v} =} randn (\"state\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
559 @deftypefnx {Built-in Function} {} randn (\"state\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
560 @deftypefnx {Built-in Function} {} randn (\"state\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
561 @deftypefnx {Built-in Function} {@var{v} =} randn (\"seed\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
562 @deftypefnx {Built-in Function} {} randn (\"seed\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
563 @deftypefnx {Built-in Function} {} randn (\"seed\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
564 @deftypefnx {Built-in Function} {} randn (@dots{}, \"single\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
565 @deftypefnx {Built-in Function} {} randn (@dots{}, \"double\")\n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10155
diff changeset
566 Return a matrix with normally distributed random\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
567 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
568 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
569 \n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
570 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
571 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
572 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
573 The class of the value returned can be controlled by a trailing\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
574 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
575 classes.\n\
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
576 \n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
577 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
578 @cite{Ziggurat Method for Generating Random Variables},\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
579 J. Statistical Software, vol 5, 2000,\n\
18679
a142f35f3cb6 doc: Fix unbalanced parentheses in documentation.
Rik <rik@octave.org>
parents: 18574
diff changeset
580 @url{http://www.jstatsoft.org/v05/i08/}\n\
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
581 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
582 @seealso{rand, rande, randg, randp}\n\
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 3325
diff changeset
583 @end deftypefn")
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
584 {
4307
fd034cd46aea [project @ 2003-01-24 19:20:50 by jwe]
jwe
parents: 4153
diff changeset
585 octave_value retval;
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
586
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
587 int nargin = args.length ();
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
588
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
589 retval = do_rand (args, nargin, "randn", "normal");
2928
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
590
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
591 return retval;
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
592 }
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
593
295f037b4b3e [project @ 1997-05-05 05:32:33 by jwe]
jwe
parents:
diff changeset
594 /*
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
595 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
596 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
597 %! randn ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
598 %! 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
599 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
600 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
601 %! randn ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
602 %! 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
603 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
604 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
605 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
606 %! randn ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
607 %! x = randn (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
608 %! 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
609 %! 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
610 %! 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
611 %! assert (kurtosis (x), 0, 0.04);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
612 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
613 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
614 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
615 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
616 %! randn ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
617 %! x = randn (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
618 %! 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
619 %! 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
620 %! 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
621 %! assert (kurtosis (x), 0, 0.04);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
622 %! endif
5730
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
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
625 DEFUN (rande, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
626 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
627 @deftypefn {Built-in Function} {} rande (@var{n})\n\
18436
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
628 @deftypefnx {Built-in Function} {} rande (@var{m}, @var{n}, @dots{})\n\
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
629 @deftypefnx {Built-in Function} {} rande ([@var{m} @var{n} @dots{}])\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
630 @deftypefnx {Built-in Function} {@var{v} =} rande (\"state\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
631 @deftypefnx {Built-in Function} {} rande (\"state\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
632 @deftypefnx {Built-in Function} {} rande (\"state\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
633 @deftypefnx {Built-in Function} {@var{v} =} rande (\"seed\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
634 @deftypefnx {Built-in Function} {} rande (\"seed\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
635 @deftypefnx {Built-in Function} {} rande (\"seed\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
636 @deftypefnx {Built-in Function} {} rande (@dots{}, \"single\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
637 @deftypefnx {Built-in Function} {} rande (@dots{}, \"double\")\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
638 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
639 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
640 \n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
641 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
642 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
643 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
644 The class of the value returned can be controlled by a trailing\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
645 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
646 classes.\n\
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
647 \n\
10791
3140cb7a05a1 Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents: 10787
diff changeset
648 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
649 @cite{Ziggurat Method for Generating Random Variables},\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
650 J. Statistical Software, vol 5, 2000,\n\
18679
a142f35f3cb6 doc: Fix unbalanced parentheses in documentation.
Rik <rik@octave.org>
parents: 18574
diff changeset
651 @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
652 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
653 @seealso{rand, randn, randg, randp}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
654 @end deftypefn")
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
655 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
656 octave_value retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
657
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
658 int nargin = args.length ();
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
659
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
660 retval = do_rand (args, nargin, "rande", "exponential");
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
661
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
662 return retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
663 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
664
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
665 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
666 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
667 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
668 %! rande ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
669 %! 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
670 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
671 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
672 %! rande ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
673 %! 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
674 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
675 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
676 %! ## statistical tests may fail occasionally
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
677 %! rande ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
678 %! x = rande (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
679 %! 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
680 %! 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
681 %! 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
682 %! 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
683 %! assert (kurtosis (x), 6, 0.7);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
684 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
685 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
686 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
687 %! ## statistical tests may fail occasionally
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
688 %! rande ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
689 %! x = rande (100000, 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
690 %! 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
691 %! 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
692 %! 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
693 %! 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
694 %! assert (kurtosis (x), 6, 0.7);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
695 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
696 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
697
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
698 DEFUN (randg, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
699 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
700 @deftypefn {Built-in Function} {} randg (@var{n})\n\
18436
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
701 @deftypefnx {Built-in Function} {} randg (@var{m}, @var{n}, @dots{})\n\
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
702 @deftypefnx {Built-in Function} {} randg ([@var{m} @var{n} @dots{}])\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
703 @deftypefnx {Built-in Function} {@var{v} =} randg (\"state\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
704 @deftypefnx {Built-in Function} {} randg (\"state\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
705 @deftypefnx {Built-in Function} {} randg (\"state\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
706 @deftypefnx {Built-in Function} {@var{v} =} randg (\"seed\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
707 @deftypefnx {Built-in Function} {} randg (\"seed\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
708 @deftypefnx {Built-in Function} {} randg (\"seed\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
709 @deftypefnx {Built-in Function} {} randg (@dots{}, \"single\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
710 @deftypefnx {Built-in Function} {} randg (@dots{}, \"double\")\n\
14854
5ae9f0f77635 maint: Use Octave coding conventions for coddling parenthis is DLD-FUNCTIONS directory
Rik <octave@nomad.inbox5.com>
parents: 14853
diff changeset
711 Return a matrix with @code{gamma (@var{a},1)} distributed random elements.\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
712 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
713 except for the argument @var{a}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
714 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
715 This can be used to generate many distributions:\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
716 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
717 @table @asis\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
718 @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
719 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
720 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
721 r = b * randg (a)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
722 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
723 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
724 @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
725 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
726 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
727 @group\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
728 r1 = randg (a, 1)\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
729 r = r1 / (r1 + randg (b, 1))\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
730 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
731 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
732 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
733 @item @code{Erlang (a, n)}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
734 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
735 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
736 r = a * randg (n)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
737 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
738 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
739 @item @code{chisq (df)} for @code{df > 0}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
740 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
741 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
742 r = 2 * randg (df / 2)\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
743 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
744 \n\
12639
4d777e05d47c doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents: 11586
diff changeset
745 @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
746 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
747 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
748 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
749 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
750 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
751 @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
752 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
753 @example\n\
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
754 @group\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
755 ## r1 equals 1 if n1 is infinite\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
756 r1 = 2 * randg (n1 / 2) / n1\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
757 ## r2 equals 1 if n2 is infinite\n\
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
758 r2 = 2 * randg (n2 / 2) / n2\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
759 r = r1 / r2\n\n\
7096
81bed50b9feb [project @ 2007-11-02 16:13:43 by jwe]
jwe
parents: 7029
diff changeset
760 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
761 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
762 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
763 @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
764 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
765 @example\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
766 r = randp ((1 - p) / p * randg (n))\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
767 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
768 \n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
769 @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
770 (use chisq if @code{L = 0})\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
771 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
772 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
773 @group\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
774 r = randp (L / 2)\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
775 r(r > 0) = 2 * randg (r(r > 0))\n\
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
776 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
777 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
778 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
779 \n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
780 @item @code{Dirichlet (a1, @dots{} ak)}\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
781 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
782 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
783 @group\n\
9041
853f96e8008f Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
784 r = (randg (a1), @dots{}, randg (ak))\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
785 r = r / sum (r)\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9041
diff changeset
786 @end group\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
787 @end example\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
788 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
789 @end table\n\
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
790 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
791 The class of the value returned can be controlled by a trailing\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
792 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
793 classes.\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
794 @seealso{rand, randn, rande, randp}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
795 @end deftypefn")
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
796 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
797 octave_value retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
798
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
799 int nargin = args.length ();
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
800
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
801 if (nargin < 1)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
802 error ("randg: insufficient arguments");
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
803 else
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
804 retval = do_rand (args, nargin, "randg", "gamma", true);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
805
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
806 return retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
807 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
808
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
809 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
810 %!test
14501
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 %! 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
813
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
814 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
815 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
816 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
817 %! 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
818 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
819 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
820 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
821 %! 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
822 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
823 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
824 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
825 %! 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
826 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
827 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
828 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
829 %! 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
830 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
831 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
832 %! randg ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
833 %! 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
834 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
835 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
836 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
837 %! 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
838 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
839 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
840 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
841 %! 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
842 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
843 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
844 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
845 %! 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
846 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
847 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
848 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
849 %! 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
850 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
851 %! ## Test fixed seed
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
852 %! randg ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
853 %! assert (randg (100, 1, 6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625], 1e-4);
18573
53ba52143af7 Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents: 18436
diff changeset
854 %!test
53ba52143af7 Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents: 18436
diff changeset
855 %! ## Test out-of-bounds values produce NaN w/old-style generators & floats
53ba52143af7 Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents: 18436
diff changeset
856 %! randg ("seed", 1);
53ba52143af7 Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents: 18436
diff changeset
857 %! result = randg ([-2 Inf], "single");
18574
756e906bdfda Fix %!test comparing NaN (single) to NaN (double) in cset 53ba52143af7.
Rik <rik@octave.org>
parents: 18573
diff changeset
858 %! assert (result, single ([NaN NaN]));
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
859
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
860 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
861 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
862 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
863 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
864 %! a = 0.1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
865 %! 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
866 %! 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
867 %! 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
868 %! 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
869 %! assert (kurtosis (x), 6/a, 50);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
870 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
871 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
872 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
873 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
874 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
875 %! a = 0.95;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
876 %! 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
877 %! 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
878 %! 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
879 %! 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
880 %! 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
881 %! endif
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
882 %!test
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
883 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
884 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
885 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
886 %! a = 1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
887 %! 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
888 %! 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
889 %! 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
890 %! 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
891 %! assert (kurtosis (x), 6/a, 2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
892 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
893 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
894 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
895 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
896 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
897 %! a = 10;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
898 %! 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
899 %! 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
900 %! 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
901 %! 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
902 %! assert (kurtosis (x), 6/a, 0.5);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
903 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
904 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
905 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
906 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
907 %! randg ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
908 %! a = 100;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
909 %! 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
910 %! 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
911 %! 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
912 %! 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
913 %! 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
914 %! endif
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
915 %!test
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
916 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
917 %!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
918 %!test
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
919 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
920 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
921 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
922 %! a = 0.1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
923 %! 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
924 %! 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
925 %! 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
926 %! 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
927 %! assert (kurtosis (x), 6/a, 50);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
928 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
929 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
930 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
931 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
932 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
933 %! a = 0.95;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
934 %! 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
935 %! 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
936 %! 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
937 %! 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
938 %! assert (kurtosis (x), 6/a, 2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
939 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
940 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
941 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
942 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
943 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
944 %! a = 1;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
945 %! 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
946 %! 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
947 %! 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
948 %! 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
949 %! assert (kurtosis (x), 6/a, 2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
950 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
951 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
952 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
953 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
954 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
955 %! a = 10;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
956 %! 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
957 %! 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
958 %! 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
959 %! 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
960 %! assert (kurtosis (x), 6/a, 0.5);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
961 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
962 %!test
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
963 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
964 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
965 %! randg ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
966 %! a = 100;
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
967 %! 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
968 %! 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
969 %! 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
970 %! 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
971 %! assert (kurtosis (x), 6/a, 0.2);
6437
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
972 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
973 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
974
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
975 DEFUN (randp, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
976 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
977 @deftypefn {Built-in Function} {} randp (@var{l}, @var{n})\n\
18436
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
978 @deftypefnx {Built-in Function} {} randp (@var{l}, @var{m}, @var{n}, @dots{})\n\
5d14a08e7533 doc: Change docstring for rand* functions from [N, M] to [M, N] (bug #41496).
Rik <rik@octave.org>
parents: 17812
diff changeset
979 @deftypefnx {Built-in Function} {} randp (@var{l}, [@var{m} @var{n} @dots{}])\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
980 @deftypefnx {Built-in Function} {@var{v} =} randp (\"state\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
981 @deftypefnx {Built-in Function} {} randp (\"state\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
982 @deftypefnx {Built-in Function} {} randp (\"state\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
983 @deftypefnx {Built-in Function} {@var{v} =} randp (\"seed\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
984 @deftypefnx {Built-in Function} {} randp (\"seed\", @var{v})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
985 @deftypefnx {Built-in Function} {} randp (\"seed\", \"reset\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
986 @deftypefnx {Built-in Function} {} randp (@dots{}, \"single\")\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
987 @deftypefnx {Built-in Function} {} randp (@dots{}, \"double\")\n\
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 10155
diff changeset
988 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
989 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
990 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
991 argument @var{l}.\n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
992 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
993 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
994 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
995 \n\
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
996 @table @asis\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
997 @item For scalar @var{l} @leq{} 12, use direct method.\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
998 W.H. Press, et al., @cite{Numerical Recipes in C},\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
999 Cambridge University Press, 1992.\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
1000 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1001 @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
1002 W.H. Press, et al., @cite{Numerical Recipes in C},\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
1003 Cambridge University Press, 1992.\n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
1004 \n\
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
1005 @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
1006 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
1007 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1008 @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
1009 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
1010 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
1011 unimodal distributions}, Doctoral Dissertation, 156pp., Technical\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1012 University Graz, Austria, 1994.\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10791
diff changeset
1013 \n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1014 @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
1015 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
1016 D 50 p1284, 1994.\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1017 @end table\n\
14655
43db83eff9db Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents: 14620
diff changeset
1018 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
1019 The class of the value returned can be controlled by a trailing\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
1020 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17258
diff changeset
1021 classes.\n\
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6541
diff changeset
1022 @seealso{rand, randn, rande, randg}\n\
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1023 @end deftypefn")
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1024 {
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1025 octave_value retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1026
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1027 int nargin = args.length ();
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1028
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1029 if (nargin < 1)
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1030 error ("randp: insufficient arguments");
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1031 else
10782
d1f920d1ce0c simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 10709
diff changeset
1032 retval = do_rand (args, nargin, "randp", "poisson", true);
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1033
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1034 return retval;
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1035 }
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1036
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1037 /*
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1038 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1039 %! randp ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1040 %! 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
1041 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1042 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1043 %! randp ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1044 %! 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
1045 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1046 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1047 %! randp ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1048 %! 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
1049 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1050 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1051 %! randp ("state", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1052 %! 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
1053 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1054 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1055 %! randp ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1056 %! %%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
1057 %! 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
1058 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1059 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1060 %! randp ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1061 %! 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
1062 %!test
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1063 %! ## Test fixed state
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1064 %! randp ("seed", 1);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1065 %! 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
1066 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1067 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1068 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1069 %! randp ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1070 %! 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
1071 %! 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
1072 %! 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
1073 %! 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
1074 %! 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
1075 %! 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
1076 %! 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
1077 %! endfor
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1078 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1079 %!test
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1080 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1081 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1082 %! randp ("state", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1083 %! 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
1084 %! 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
1085 %! 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
1086 %! 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
1087 %! 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
1088 %! 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
1089 %! 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
1090 %! endfor
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1091 %! endif
190dfe8b8f08 [project @ 2007-03-23 14:17:31 by dbateman]
dbateman
parents: 6198
diff changeset
1092 %!test
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1093 %! randp ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1094 %! 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
1095 %!test
6449
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1096 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1097 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1098 %! randp ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1099 %! 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
1100 %! 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
1101 %! 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
1102 %! 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
1103 %! 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
1104 %! 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
1105 %! 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
1106 %! endfor
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1107 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1108 %!test
6449
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1109 %! if (__random_statistical_tests__)
17336
b81b9d079515 Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents: 17281
diff changeset
1110 %! ## statistical tests may fail occasionally.
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1111 %! randp ("seed", 12);
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
1112 %! 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
1113 %! 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
1114 %! 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
1115 %! 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
1116 %! 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
1117 %! 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
1118 %! 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
1119 %! endfor
a5de12c0f968 [project @ 2007-03-24 11:11:36 by dbateman]
dbateman
parents: 6443
diff changeset
1120 %! endif
5730
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1121 */
109fdf7b3dcb [project @ 2006-04-03 19:18:26 by jwe]
jwe
parents: 5355
diff changeset
1122
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
1123 DEFUN (randperm, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1124 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
1125 @deftypefn {Built-in Function} {} randperm (@var{n})\n\
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14854
diff changeset
1126 @deftypefnx {Built-in Function} {} randperm (@var{n}, @var{m})\n\
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1127 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
1128 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
1129 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
1130 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
1131 O(@var{m}) memory is used as well. The randomization is performed using\n\
14853
72b8b39e12be doc: Periodic grammarcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 14846
diff changeset
1132 rand(). All permutations are equally likely.\n\
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1133 @seealso{perms}\n\
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1134 @end deftypefn")
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1135 {
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
1136
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
1137 #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
1138 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
1139 #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
1140 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
1141 #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
1142
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1143 int nargin = args.length ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1144 octave_value retval;
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1145
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1146 if (nargin == 1 || nargin == 2)
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1147 {
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1148 octave_idx_type n, m;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
1149
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1150 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
1151
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1152 if (nargin == 2)
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1153 m = args(1).idx_type_value (true);
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1154 else
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1155 m = n;
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1156
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1157 if (m < 0 || n < 0)
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
1158 error ("randperm: M and N must be non-negative");
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1159
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1160 if (m > n)
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1161 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
1162
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
1163 // 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
1164 // whole vector for tracking the truncated shuffle.
17662
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1165 bool short_shuffle = m < n/5;
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
1166
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1167 if (! error_state)
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1168 {
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1169 // Generate random numbers.
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1170 NDArray r = octave_rand::nd_array (dim_vector (1, m));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1171 double *rvec = r.fortran_vec ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1172
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
1173 octave_idx_type idx_len = short_shuffle ? m : n;
17662
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1174 Array<octave_idx_type> idx;
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1175 try
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1176 {
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1177 idx = Array<octave_idx_type> (dim_vector (1, idx_len));
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1178 }
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1179 catch (std::bad_alloc)
17662
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1180 {
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1181 // Looks like n is too big and short_shuffle is false.
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1182 // Let's try again, but this time with the alternative.
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1183 idx_len = m;
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1184 short_shuffle = true;
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1185 idx = Array<octave_idx_type> (dim_vector (1, idx_len));
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1186 }
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1187
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1188 octave_idx_type *ivec = idx.fortran_vec ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1189
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
1190 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
1191 ivec[i] = i;
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1192
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
1193 if (short_shuffle)
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1194 {
13727
478efc95cb7a Test unordered_map header location and namespace.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 13255
diff changeset
1195 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
1196
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
1197 // 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
1198 // 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
1199 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
1200 {
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
1201 octave_idx_type k = i +
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1202 gnulib::floor (rvec[i] * (n - i));
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
1203
14688
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1204 //For shuffling first m entries, no need to use extra
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1205 //storage
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1206 if (k < 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
1207 {
14688
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1208 std::swap (ivec[i], ivec[k]);
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
1209 }
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
1210 else
14688
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1211 {
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1212 if (map.find (k) == map.end ())
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1213 map[k] = k;
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
1214
14688
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1215 std::swap (ivec[i], map[k]);
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1216 }
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
1217 }
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
1218 }
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
1219 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
1220 {
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
1221
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
1222 // 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
1223 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
1224 {
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
1225 octave_idx_type k = i +
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1226 gnulib::floor (rvec[i] * (n - i));
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
1227 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
1228 }
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1229 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1230
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1231 // 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
1232 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
1233 rvec[i] = ivec[i] + 1;
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1234
13186
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1235 if (m < n)
2896c083576a Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 13026
diff changeset
1236 idx.resize (dim_vector (1, m));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1237
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1238 // 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
1239 retval = new octave_matrix (r, idx_vector (idx));
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1240 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1241 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1242 else
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1243 print_usage ();
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1244
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1245 return retval;
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1246 }
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1247
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1248 /*
14688
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1249 %!assert (sort (randperm (20)), 1:20)
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1250 %!assert (length (randperm (20,10)), 10)
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1251
17662
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1252 ## Test biggish N (bug #39378)
17812
fffd0c0ca2dc maint: A few Octave coding convention tweaks
Rik <rik@octave.org>
parents: 17787
diff changeset
1253 %!assert (length (randperm (30000^2, 100000)), 100000)
17662
0b7f5c56f853 Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 17336
diff changeset
1254
14688
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1255 %!test
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1256 %! rand ("seed", 0);
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1257 %! for i = 1:100
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1258 %! p = randperm (305, 30);
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1259 %! assert (length (unique (p)), 30);
42edbe6f6a8d Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 14138
diff changeset
1260 %! endfor
9647
54f45f883a53 optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents: 9377
diff changeset
1261 */