Mercurial > octave
annotate libinterp/corefcn/rand.cc @ 20940:48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
* libinterp/corefcn/oct-obj.h: Replaced. Contains a #warning issued if used in
compilation and includes "ovl.h" instead. Original file renamed to
libinterp/octave-value/ovl.h
* libinterp/corefcn/oct-obj.cc: Renamed to libinterp/octave-value/ovl.cc.
* oct-obj.cc (ovl ()): Added new function to return empty octave_value_list.
* libinterp/corefcn/module.mk: Remove oct-obj.cc from build system.
* libinterp/octave-value/module.mk: Add ovl.h and ovl.cc to build system.
* mk-opts.pl, annotation-dialog.h, Cell.cc, __contourc__.cc, __dsearchn__.cc,
__lin_interpn__.cc, __pchip_deriv__.cc, __qp__.cc, balance.cc, besselj.cc,
betainc.cc, colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc,
debug.cc, defaults.cc, defun.cc, det.cc, dirfns.cc, dlmread.cc, eig.cc,
error.cc, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc,
gammainc.cc, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc,
graphics.cc, gripes.cc, help.cc, hess.cc, hex2num.cc, hook-fcn.h, input.cc,
input.h, inv.cc, kron.cc, load-save.cc, lookup.cc, ls-hdf5.cc, ls-mat-ascii.cc,
ls-mat4.cc, ls-mat5.cc, ls-oct-binary.cc, ls-oct-text.cc, lsode.cc, lu.cc,
luinc.cc, max.cc, mex.cc, oct-hist.cc, oct-lvalue.cc, oct-lvalue.h, oct-map.h,
oct-stream.cc, oct.h, ordschur.cc, pager.cc, pinv.cc, pr-output.cc, quad.cc,
quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, sparse-xpow.cc,
strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symtab.h, syscalls.cc, sysdep.cc,
time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, urlwrite.cc, utils.cc,
variables.cc, xpow.cc, __delaunayn__.cc, __glpk__.cc, __voronoi__.cc,
audiodevinfo.cc, audioread.cc, chol.cc, convhulln.cc, dmperm.cc, qr.cc,
symbfact.cc, mkbuiltins, ov-base-diag.h, ov-base-int.cc, ov-base-mat.cc,
ov-base-mat.h, ov-base-scalar.cc, ov-base-sparse.cc, ov-base-sparse.h,
ov-base.cc, ov-bool-mat.cc, ov-bool.cc, ov-builtin.cc, ov-cell.cc, ov-colon.cc,
ov-complex.cc, ov-cs-list.h, ov-cx-mat.cc, ov-dld-fcn.cc, ov-fcn.cc, ov-fcn.h,
ov-float.cc, ov-flt-complex.cc, ov-flt-cx-mat.cc, ov-flt-re-mat.cc,
ov-int16.cc, ov-int32.cc, ov-int64.cc, ov-int8.cc, ov-java.h, ov-mex-fcn.cc,
ov-perm.h, ov-range.cc, ov-re-mat.cc, ov-scalar.cc, ov-str-mat.cc,
ov-uint16.cc, ov-uint32.cc, ov-uint64.cc, ov-uint8.cc, ov-usr-fcn.cc,
ov-usr-fcn.h, ov.cc, octave.cc, op-b-b.cc, op-b-bm.cc, op-b-sbm.cc, op-bm-b.cc,
op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc, op-chm.cc, op-class.cc,
op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc, op-cm-scm.cc, op-cm-sm.cc,
op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc, op-cs-scm.cc, op-cs-sm.cc,
op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc, op-double-conv.cc, op-fcdm-fcdm.cc,
op-fcdm-fdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc,
op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc,
op-fdm-fdm.cc, op-float-conv.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc,
op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc,
op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc,
op-int-conv.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc,
op-m-sm.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-range.cc, op-s-cm.cc,
op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc,
op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc,
op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc,
op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc,
op-struct.cc, op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc,
parse.h, pt-arg-list.cc, pt-assign.cc, pt-binop.cc, pt-cbinop.cc, pt-cell.cc,
pt-colon.cc, pt-const.cc, pt-eval.h, pt-fcn-handle.cc, pt-funcall.h, pt-id.cc,
pt-idx.cc, pt-jump.cc, pt-mat.cc, pt-select.cc, pt-unop.cc, token.cc,
Array-sym.cc: replace '#include "oct-obj.h"' with '#include "ovl.h"'.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 18 Dec 2015 16:04:56 -0800 |
parents | e528d7ab1cad |
children | 63374982750b |
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 | 2 /* |
3 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19040
diff
changeset
|
4 Copyright (C) 1996-2015 John W. Eaton |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
2928 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
2928 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
2928 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
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 | 35 #include <string> |
36 | |
37 #include "f77-fcn.h" | |
38 #include "lo-mappers.h" | |
4307 | 39 #include "oct-rand.h" |
4153 | 40 #include "quit.h" |
2928 | 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 | 43 #include "error.h" |
44 #include "gripes.h" | |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20894
diff
changeset
|
45 #include "ovl.h" |
2928 | 46 #include "unwind-prot.h" |
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 | 49 |
6437 | 50 /* |
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 | 53 %! __random_statistical_tests__ = 0; |
54 */ | |
55 | |
4307 | 56 static octave_value |
5730 | 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 | 59 { |
5730 | 60 NDArray a; |
61 int idx = 0; | |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
62 bool is_single = false; |
2928 | 63 |
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
|
64 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
|
65 { |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
66 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
|
67 |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
68 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
|
69 { |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
70 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
|
71 nargin--; |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
72 } |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
73 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
|
74 nargin--; |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
75 } |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
76 |
5730 | 77 if (additional_arg) |
78 { | |
79 if (nargin == 0) | |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
80 error ("%s: at least one argument is required", fcn); |
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
|
81 else if (args(0).is_string ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
82 additional_arg = false; |
5730 | 83 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
84 { |
20711
7b608fadc663
Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents:
20704
diff
changeset
|
85 a = args(0).xarray_value ("%s: dimension must be a scalar integer", fcn); |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20687
diff
changeset
|
86 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
87 idx++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
88 nargin--; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
89 } |
5730 | 90 } |
2928 | 91 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
92 octave_value retval; |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
93 dim_vector dims; |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
94 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
95 unwind_protect frame; |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
96 // Restore current distribution on any exit. |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
97 frame.add_fcn (octave_rand::distribution, |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
98 octave_rand::distribution ()); |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
99 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
100 octave_rand::distribution (distribution); |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
101 |
4543 | 102 switch (nargin) |
2928 | 103 { |
4543 | 104 case 0: |
105 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
106 if (additional_arg) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
107 dims = a.dims (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
108 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
109 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
110 dims.resize (2); |
4543 | 111 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
112 dims(0) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
113 dims(1) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
114 } |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
115 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
116 goto gen_matrix; |
4543 | 117 } |
118 break; | |
2928 | 119 |
4543 | 120 case 1: |
121 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
122 octave_value tmp = args(idx); |
4543 | 123 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
124 if (tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
125 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
126 std::string s_arg = tmp.string_value (); |
2928 | 127 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
128 if (s_arg == "dist") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
129 retval = octave_rand::distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
130 else if (s_arg == "seed") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
131 retval = octave_rand::seed (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
132 else if (s_arg == "state" || s_arg == "twister") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
133 retval = octave_rand::state (fcn); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
134 else if (s_arg == "uniform") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
135 octave_rand::uniform_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
136 else if (s_arg == "normal") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
137 octave_rand::normal_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
138 else if (s_arg == "exponential") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
139 octave_rand::exponential_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
140 else if (s_arg == "poisson") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
141 octave_rand::poisson_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
142 else if (s_arg == "gamma") |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
143 octave_rand::gamma_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
144 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
145 error ("%s: unrecognized string argument", fcn); |
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 else if (tmp.is_scalar_type ()) |
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 double dval = tmp.double_value (); |
2928 | 150 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
151 if (xisnan (dval)) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
152 error ("%s: NaN is invalid matrix dimension", fcn); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
153 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
154 dims.resize (2); |
4543 | 155 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
156 dims(0) = NINTbig (tmp.double_value ()); |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
157 dims(1) = NINTbig (tmp.double_value ()); |
2928 | 158 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
159 goto gen_matrix; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
160 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
161 else if (tmp.is_range ()) |
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 Range r = tmp.range_value (); |
4543 | 164 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
165 if (! r.all_elements_are_ints ()) |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
166 error ("%s: all elements of range must be integers", fcn); |
4543 | 167 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
168 octave_idx_type n = r.numel (); |
4543 | 169 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
170 dims.resize (n); |
2928 | 171 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
172 octave_idx_type base = NINTbig (r.base ()); |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
173 octave_idx_type incr = NINTbig (r.inc ()); |
2928 | 174 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
175 for (octave_idx_type i = 0; i < n; i++) |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
176 { |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
177 // Negative dimensions treated as zero for Matlab compatibility |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
178 dims(i) = base >= 0 ? base : 0; |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
179 base += incr; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
180 } |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
181 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
182 goto gen_matrix; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
183 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
184 else if (tmp.is_matrix_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
185 { |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
186 Array<int> iv; |
4543 | 187 |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
188 try |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
189 { |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
190 iv = tmp.int_vector_value (true); |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
191 } |
20785
b6f2909e7f94
always throw exception after debugging with debug_on_error
John W. Eaton <jwe@octave.org>
parents:
20756
diff
changeset
|
192 catch (octave_execution_exception& e) |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
193 { |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20743
diff
changeset
|
194 error (e, "%s: dimensions must be a scalar or array of integers", fcn); |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
195 } |
4543 | 196 |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
197 octave_idx_type len = iv.numel (); |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
198 |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
199 dims.resize (len); |
2928 | 200 |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
201 for (octave_idx_type i = 0; i < len; i++) |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
202 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
203 // Negative dimensions treated as zero for Matlab compatibility |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
204 octave_idx_type elt = iv(i); |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
205 dims(i) = elt >=0 ? elt : 0; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
206 } |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
207 |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
208 goto gen_matrix; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
209 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
210 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
211 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
212 gripe_wrong_type_arg ("rand", tmp); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
213 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
214 } |
4543 | 215 } |
216 break; | |
217 | |
218 default: | |
219 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
220 octave_value tmp = args(idx); |
4543 | 221 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
222 if (nargin == 2 && tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
223 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
224 std::string ts = tmp.string_value (); |
5164 | 225 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
226 if (ts == "seed") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
227 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
228 if (args(idx+1).is_real_scalar ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
229 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
230 double d = args(idx+1).double_value (); |
2928 | 231 |
20678
4b00afb5e9c3
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
232 octave_rand::seed (d); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
233 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
234 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
|
235 && 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
|
236 octave_rand::reset (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
237 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
238 error ("%s: seed must be a real scalar", fcn); |
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 else if (ts == "state" || ts == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
241 { |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
242 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
|
243 && 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
|
244 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
|
245 else |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
246 { |
20687
5f04bfc7c17a
backout changeset 18f38ed43962
John W. Eaton <jwe@octave.org>
parents:
20682
diff
changeset
|
247 ColumnVector s = |
5f04bfc7c17a
backout changeset 18f38ed43962
John W. Eaton <jwe@octave.org>
parents:
20682
diff
changeset
|
248 ColumnVector (args(idx+1).vector_value(false, true)); |
5730 | 249 |
20678
4b00afb5e9c3
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
250 octave_rand::state (s, fcn); |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
251 } |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
252 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
253 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
254 error ("%s: unrecognized string argument", fcn); |
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 else |
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 dims.resize (nargin); |
4543 | 259 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
260 for (int i = 0; i < nargin; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
261 { |
20711
7b608fadc663
Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents:
20704
diff
changeset
|
262 octave_idx_type elt = args(idx+i).xint_value ("%s: dimension must be a scalar or array of integers", fcn); |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20687
diff
changeset
|
263 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
264 // Negative dimensions treated as zero for Matlab compatibility |
12905
f7a8d1dafda3
Let rand accept negative dimensions (bug #33301)
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12639
diff
changeset
|
265 dims(i) = elt >= 0 ? elt : 0; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
266 } |
4543 | 267 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
268 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
269 } |
4543 | 270 } |
271 break; | |
2928 | 272 } |
273 | |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
274 // No "goto gen_matrix" in code path. Must be done. |
2928 | 275 return retval; |
276 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
277 gen_matrix: |
2928 | 278 |
5355 | 279 dims.chop_trailing_singletons (); |
280 | |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
281 if (is_single) |
5730 | 282 { |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
283 if (additional_arg) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
284 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
285 if (a.numel () == 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
|
286 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
|
287 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
288 { |
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
|
289 if (a.dims () != dims) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
290 error ("%s: mismatch in argument size", fcn); |
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
291 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
292 octave_idx_type len = a.numel (); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
293 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
|
294 float *v = m.fortran_vec (); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
295 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
296 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
|
297 v[i] = octave_rand::float_scalar (a(i)); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
298 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
299 return m; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
300 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
301 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
302 else |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
303 return octave_rand::float_nd_array (dims); |
5730 | 304 } |
305 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
|
306 { |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
307 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
|
308 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
309 if (a.numel () == 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
|
310 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
|
311 else |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
312 { |
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
|
313 if (a.dims () != dims) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
314 error ("%s: mismatch in argument size", fcn); |
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
315 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
316 octave_idx_type len = a.numel (); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
317 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
|
318 double *v = m.fortran_vec (); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
319 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
320 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
|
321 v[i] = octave_rand::scalar (a(i)); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
322 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
323 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
|
324 } |
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 else |
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 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
|
328 } |
2928 | 329 } |
330 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
331 DEFUN (rand, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
332 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
333 @deftypefn {} {} rand (@var{n})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
334 @deftypefnx {} {} rand (@var{m}, @var{n}, @dots{})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
335 @deftypefnx {} {} rand ([@var{m} @var{n} @dots{}])\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
336 @deftypefnx {} {@var{v} =} rand (\"state\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
337 @deftypefnx {} {} rand (\"state\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
338 @deftypefnx {} {} rand (\"state\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
339 @deftypefnx {} {@var{v} =} rand (\"seed\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
340 @deftypefnx {} {} rand (\"seed\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
341 @deftypefnx {} {} rand (\"seed\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
342 @deftypefnx {} {} rand (@dots{}, \"single\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
343 @deftypefnx {} {} rand (@dots{}, \"double\")\n\ |
3369 | 344 Return a matrix with random elements uniformly distributed on the\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
345 interval (0, 1).\n\ |
5730 | 346 \n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
347 The arguments are handled the same as the arguments for @code{eye}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
348 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
349 You can query the state of the random number generator using the form\n\ |
2928 | 350 \n\ |
3369 | 351 @example\n\ |
5730 | 352 v = rand (\"state\")\n\ |
353 @end example\n\ | |
354 \n\ | |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
355 This returns a column vector @var{v} of length 625. Later, you can restore\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
356 the random number generator to the state @var{v} using the form\n\ |
5730 | 357 \n\ |
358 @example\n\ | |
359 rand (\"state\", v)\n\ | |
3369 | 360 @end example\n\ |
361 \n\ | |
362 @noindent\n\ | |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
363 You may also initialize the state vector from an arbitrary vector of length\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
364 @leq{} 625 for @var{v}. This new state will be a hash based on the value of\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
365 @var{v}, not @var{v} itself.\n\ |
5730 | 366 \n\ |
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 vector in Octave's startup files (@pxref{Startup Files}).\n\ |
5730 | 373 \n\ |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7780
diff
changeset
|
374 To compute the pseudo-random sequence, @code{rand} uses the Mersenne\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
375 Twister with a period of @math{2^{19937}-1}\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
376 (See @nospell{M. Matsumoto and T. Nishimura},\n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
377 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
378 pseudorandom number generator},\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
379 ACM Trans. on Modeling and Computer Simulation Vol. 8, No. 1, pp. 3--30,\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
380 January 1998,\n\ |
7171 | 381 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
382 Do @strong{not} use for cryptography without securely hashing several\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
383 returned values together, otherwise the generator state can be learned after\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
384 reading 624 consecutive values.\n\ |
5730 | 385 \n\ |
7096 | 386 Older versions of Octave used a different random number generator.\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
387 The new generator is used by default as it is significantly faster than the\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
388 old generator, and produces random numbers with a significantly longer cycle\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
389 time. However, in some circumstances it might be desirable to obtain the\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
390 same random sequences as produced by the old generators. To do this the\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
391 keyword @qcode{\"seed\"} is used to specify that the old generators should\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
392 be used, as in\n\ |
2928 | 393 \n\ |
3369 | 394 @example\n\ |
5730 | 395 rand (\"seed\", val)\n\ |
3369 | 396 @end example\n\ |
397 \n\ | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
398 @noindent\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
399 which sets the seed of the generator to @var{val}. The seed of the\n\ |
5730 | 400 generator can be queried with\n\ |
401 \n\ | |
402 @example\n\ | |
403 s = rand (\"seed\")\n\ | |
404 @end example\n\ | |
405 \n\ | |
406 However, it should be noted that querying the seed will not cause\n\ | |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
407 @code{rand} to use the old generators, only setting the seed will. To cause\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
408 @code{rand} to once again use the new generators, the keyword\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
409 @qcode{\"state\"} should be used to reset the state of the @code{rand}.\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
410 \n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
411 The state or seed of the generator can be reset to a new random value using\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
412 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
|
413 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17258
diff
changeset
|
414 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
|
415 @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
|
416 classes.\n\ |
5798 | 417 @seealso{randn, rande, randg, randp}\n\ |
3369 | 418 @end deftypefn") |
2928 | 419 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
420 return do_rand (args, args.length (), "rand", "uniform"); |
2928 | 421 } |
422 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
423 // FIXME: The old generator (selected when "seed" is set) will not |
8871 | 424 // work properly if compiled to use 64-bit integers. |
425 | |
5730 | 426 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
427 %!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
|
428 %! 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
|
429 %! 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
|
430 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
431 %!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
|
432 %! 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
|
433 %! 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
|
434 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
435 %!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
|
436 %! 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
|
437 %! 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
|
438 %! 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
|
439 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
440 %! 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
|
441 %! assert (x, z); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
442 %!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
|
443 %! 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
|
444 %! 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
|
445 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
446 %!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
|
447 %!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
|
448 %! 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
|
449 %! 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
|
450 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
451 %!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
|
452 %! 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
|
453 %! 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
|
454 %! 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
|
455 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
456 %! 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
|
457 %! assert (x, z); |
5730 | 458 */ |
459 | |
460 /* | |
461 %!test | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
462 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
463 %! rand ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
464 %! assert (rand (1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382], 1e-6); |
6437 | 465 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
466 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
467 %! rand ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
468 %! assert (rand (1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759], 1e-6); |
5730 | 469 %!test |
6437 | 470 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
471 %! ## 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
|
472 %! rand ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
473 %! x = rand (100000, 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
474 %! 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
|
475 %! 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
|
476 %! 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
|
477 %! 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
|
478 %! 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
|
479 %! assert (kurtosis (x), -6/5, 0.0094); |
6437 | 480 %! endif |
481 %!test | |
482 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
483 %! ## 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
|
484 %! rand ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
485 %! x = rand (100000, 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
486 %! 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
|
487 %! 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
|
488 %! 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
|
489 %! 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
|
490 %! 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
|
491 %! assert (kurtosis (x), -6/5, 0.0094); |
6437 | 492 %! endif |
5730 | 493 */ |
494 | |
17258
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
495 /* |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
496 %!# 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
|
497 %!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
|
498 %! 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
|
499 %! 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
|
500 %!endfunction |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
501 %! |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
502 %!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
|
503 %!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
|
504 %!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
|
505 %!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
|
506 */ |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
507 |
4307 | 508 static std::string current_distribution = octave_rand::distribution (); |
509 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
510 DEFUN (randn, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
511 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
512 @deftypefn {} {} randn (@var{n})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
513 @deftypefnx {} {} randn (@var{m}, @var{n}, @dots{})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
514 @deftypefnx {} {} randn ([@var{m} @var{n} @dots{}])\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
515 @deftypefnx {} {@var{v} =} randn (\"state\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
516 @deftypefnx {} {} randn (\"state\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
517 @deftypefnx {} {} randn (\"state\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
518 @deftypefnx {} {@var{v} =} randn (\"seed\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
519 @deftypefnx {} {} randn (\"seed\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
520 @deftypefnx {} {} randn (\"seed\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
521 @deftypefnx {} {} randn (@dots{}, \"single\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
522 @deftypefnx {} {} randn (@dots{}, \"double\")\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
523 Return a matrix with normally distributed random elements having zero mean\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
524 and variance one.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
525 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
526 The arguments are handled the same as the arguments for @code{rand}.\n\ |
3369 | 527 \n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
528 By default, @code{randn} uses the @nospell{Marsaglia and Tsang}\n\ |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
529 ``Ziggurat technique'' to transform from a uniform to a normal distribution.\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
530 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17258
diff
changeset
|
531 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
|
532 @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
|
533 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
|
534 \n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
535 Reference: @nospell{G. Marsaglia and W.W. Tsang},\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
536 @cite{Ziggurat Method for Generating Random Variables},\n\ |
5730 | 537 J. Statistical Software, vol 5, 2000,\n\ |
18679
a142f35f3cb6
doc: Fix unbalanced parentheses in documentation.
Rik <rik@octave.org>
parents:
18574
diff
changeset
|
538 @url{http://www.jstatsoft.org/v05/i08/}\n\ |
2928 | 539 \n\ |
6547 | 540 @seealso{rand, rande, randg, randp}\n\ |
3369 | 541 @end deftypefn") |
2928 | 542 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
543 return do_rand (args, args.length (), "randn", "normal"); |
2928 | 544 } |
545 | |
546 /* | |
5730 | 547 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
548 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
549 %! randn ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
550 %! assert (randn (1, 6), [-2.666521678978671 -0.7381719971724564 1.507903992673601 0.6019427189162239 -0.450661261143348 -0.7054431351574116], 1e-6); |
6437 | 551 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
552 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
553 %! randn ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
554 %! assert (randn (1, 6), [-1.039402365684509 -1.25938892364502 0.1968704611063004 0.3874166905879974 -0.5976632833480835 -0.6615074276924133], 1e-6); |
5730 | 555 %!test |
6437 | 556 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
557 %! ## 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
|
558 %! randn ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
559 %! x = randn (100000, 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
560 %! 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
|
561 %! 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
|
562 %! 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
|
563 %! assert (kurtosis (x), 0, 0.04); |
6437 | 564 %! endif |
565 %!test | |
566 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
567 %! ## 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
|
568 %! randn ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
569 %! x = randn (100000, 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
570 %! 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
|
571 %! 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
|
572 %! 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
|
573 %! assert (kurtosis (x), 0, 0.04); |
6437 | 574 %! endif |
5730 | 575 */ |
576 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
577 DEFUN (rande, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
578 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
579 @deftypefn {} {} rande (@var{n})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
580 @deftypefnx {} {} rande (@var{m}, @var{n}, @dots{})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
581 @deftypefnx {} {} rande ([@var{m} @var{n} @dots{}])\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
582 @deftypefnx {} {@var{v} =} rande (\"state\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
583 @deftypefnx {} {} rande (\"state\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
584 @deftypefnx {} {} rande (\"state\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
585 @deftypefnx {} {@var{v} =} rande (\"seed\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
586 @deftypefnx {} {} rande (\"seed\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
587 @deftypefnx {} {} rande (\"seed\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
588 @deftypefnx {} {} rande (@dots{}, \"single\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
589 @deftypefnx {} {} rande (@dots{}, \"double\")\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
590 Return a matrix with exponentially distributed random elements.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
591 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
592 The arguments are handled the same as the arguments for @code{rand}.\n\ |
5730 | 593 \n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
594 By default, @code{randn} uses the @nospell{Marsaglia and Tsang}\n\ |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
595 ``Ziggurat technique'' to transform from a uniform to a normal distribution.\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
596 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17258
diff
changeset
|
597 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
|
598 @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
|
599 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
|
600 \n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
601 Reference: @nospell{G. Marsaglia and W.W. Tsang},\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
602 @cite{Ziggurat Method for Generating Random Variables},\n\ |
5730 | 603 J. Statistical Software, vol 5, 2000,\n\ |
18679
a142f35f3cb6
doc: Fix unbalanced parentheses in documentation.
Rik <rik@octave.org>
parents:
18574
diff
changeset
|
604 @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
|
605 \n\ |
6547 | 606 @seealso{rand, randn, randg, randp}\n\ |
5730 | 607 @end deftypefn") |
608 { | |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
609 return do_rand (args, args.length (), "rande", "exponential"); |
5730 | 610 } |
611 | |
612 /* | |
613 %!test | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
614 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
615 %! rande ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
616 %! assert (rande (1, 6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291], 1e-6); |
6437 | 617 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
618 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
619 %! rande ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
620 %! assert (rande (1, 6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087], 1e-6); |
5730 | 621 %!test |
6437 | 622 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
623 %! ## 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
|
624 %! rande ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
625 %! x = rande (100000, 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
626 %! 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
|
627 %! 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
|
628 %! 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
|
629 %! 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
|
630 %! assert (kurtosis (x), 6, 0.7); |
6437 | 631 %! endif |
632 %!test | |
633 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
634 %! ## 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
|
635 %! rande ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
636 %! x = rande (100000, 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
637 %! 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
|
638 %! 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
|
639 %! 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
|
640 %! 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
|
641 %! assert (kurtosis (x), 6, 0.7); |
6437 | 642 %! endif |
5730 | 643 */ |
644 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
645 DEFUN (randg, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
646 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
647 @deftypefn {} {} randg (@var{n})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
648 @deftypefnx {} {} randg (@var{m}, @var{n}, @dots{})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
649 @deftypefnx {} {} randg ([@var{m} @var{n} @dots{}])\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
650 @deftypefnx {} {@var{v} =} randg (\"state\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
651 @deftypefnx {} {} randg (\"state\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
652 @deftypefnx {} {} randg (\"state\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
653 @deftypefnx {} {@var{v} =} randg (\"seed\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
654 @deftypefnx {} {} randg (\"seed\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
655 @deftypefnx {} {} randg (\"seed\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
656 @deftypefnx {} {} randg (@dots{}, \"single\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
657 @deftypefnx {} {} 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
|
658 Return a matrix with @code{gamma (@var{a},1)} distributed random elements.\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
659 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
660 The arguments are handled the same as the arguments for @code{rand}, except\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
661 for the argument @var{a}.\n\ |
5730 | 662 \n\ |
663 This can be used to generate many distributions:\n\ | |
664 \n\ | |
665 @table @asis\n\ | |
6547 | 666 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\ |
10840 | 667 \n\ |
5730 | 668 @example\n\ |
6547 | 669 r = b * randg (a)\n\ |
5730 | 670 @end example\n\ |
10840 | 671 \n\ |
6547 | 672 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\ |
10840 | 673 \n\ |
5730 | 674 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
675 @group\n\ |
6547 | 676 r1 = randg (a, 1)\n\ |
677 r = r1 / (r1 + randg (b, 1))\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
678 @end group\n\ |
5730 | 679 @end example\n\ |
10840 | 680 \n\ |
6547 | 681 @item @code{Erlang (a, n)}\n\ |
10840 | 682 \n\ |
5730 | 683 @example\n\ |
6547 | 684 r = a * randg (n)\n\ |
5730 | 685 @end example\n\ |
10840 | 686 \n\ |
6547 | 687 @item @code{chisq (df)} for @code{df > 0}\n\ |
10840 | 688 \n\ |
5730 | 689 @example\n\ |
6547 | 690 r = 2 * randg (df / 2)\n\ |
5730 | 691 @end example\n\ |
10840 | 692 \n\ |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
693 @item @code{t (df)} for @code{0 < df < inf} (use randn if df is infinite)\n\ |
10840 | 694 \n\ |
5730 | 695 @example\n\ |
6547 | 696 r = randn () / sqrt (2 * randg (df / 2) / df)\n\ |
5730 | 697 @end example\n\ |
10840 | 698 \n\ |
6547 | 699 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\ |
10840 | 700 \n\ |
5730 | 701 @example\n\ |
7096 | 702 @group\n\ |
703 ## r1 equals 1 if n1 is infinite\n\ | |
704 r1 = 2 * randg (n1 / 2) / n1\n\ | |
705 ## r2 equals 1 if n2 is infinite\n\ | |
706 r2 = 2 * randg (n2 / 2) / n2\n\ | |
5730 | 707 r = r1 / r2\n\n\ |
7096 | 708 @end group\n\ |
5730 | 709 @end example\n\ |
10840 | 710 \n\ |
5730 | 711 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\ |
10840 | 712 \n\ |
5730 | 713 @example\n\ |
6547 | 714 r = randp ((1 - p) / p * randg (n))\n\ |
5730 | 715 @end example\n\ |
10840 | 716 \n\ |
6547 | 717 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\ |
5730 | 718 (use chisq if @code{L = 0})\n\ |
10840 | 719 \n\ |
5730 | 720 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
721 @group\n\ |
6547 | 722 r = randp (L / 2)\n\ |
723 r(r > 0) = 2 * randg (r(r > 0))\n\ | |
724 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
|
725 @end group\n\ |
5730 | 726 @end example\n\ |
10840 | 727 \n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
728 @item @code{Dirichlet (a1, @dots{} ak)}\n\ |
10840 | 729 \n\ |
5730 | 730 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
731 @group\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
732 r = (randg (a1), @dots{}, randg (ak))\n\ |
6547 | 733 r = r / sum (r)\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
734 @end group\n\ |
5730 | 735 @end example\n\ |
10840 | 736 \n\ |
5730 | 737 @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
|
738 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17258
diff
changeset
|
739 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
|
740 @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
|
741 classes.\n\ |
6547 | 742 @seealso{rand, randn, rande, randp}\n\ |
5730 | 743 @end deftypefn") |
744 { | |
745 int nargin = args.length (); | |
746 | |
747 if (nargin < 1) | |
748 error ("randg: insufficient arguments"); | |
749 | |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
750 return do_rand (args, nargin, "randg", "gamma", true); |
5730 | 751 } |
752 | |
753 /* | |
754 %!test | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
755 %! randg ("state", 12) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
756 %! assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]); # *** Please report |
6437 | 757 |
758 %!test | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
759 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
760 %! randg ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
761 %! assert (randg (0.1, 1, 6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12], 1e-6); |
6437 | 762 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
763 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
764 %! randg ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
765 %! assert (randg (0.95, 1, 6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939], 1e-6); |
6437 | 766 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
767 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
768 %! randg ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
769 %! assert (randg (1, 1, 6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533], 1e-6); |
6437 | 770 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
771 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
772 %! randg ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
773 %! assert (randg (10, 1, 6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059], 1e-5); |
6437 | 774 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
775 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
776 %! randg ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
777 %! assert (randg (100, 1, 6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118], 1e-4); |
6437 | 778 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
779 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
780 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
781 %! assert (randg (0.1, 1, 6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12], 1e-6); |
6437 | 782 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
783 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
784 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
785 %! assert (randg (0.95, 1, 6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648], 1e-6); |
6437 | 786 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
787 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
788 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
789 %! assert (randg (1, 1, 6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303], 1e-6); |
6437 | 790 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
791 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
792 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
793 %! assert (randg (10, 1, 6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186], 1e-5); |
6437 | 794 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
795 %! ## Test fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
796 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
797 %! 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
|
798 %!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
|
799 %! ## 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
|
800 %! 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
|
801 %! 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
|
802 %! 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
|
803 |
6437 | 804 %!test |
805 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
806 %! ## 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
|
807 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
808 %! a = 0.1; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
809 %! 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
|
810 %! 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
|
811 %! 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
|
812 %! 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
|
813 %! assert (kurtosis (x), 6/a, 50); |
6437 | 814 %! endif |
815 %!test | |
816 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
817 %! ## 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
|
818 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
819 %! a = 0.95; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
820 %! 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
|
821 %! 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
|
822 %! 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
|
823 %! 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
|
824 %! 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
|
825 %! endif |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
826 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
827 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
828 %! ## 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
|
829 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
830 %! a = 1; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
831 %! 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
|
832 %! 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
|
833 %! 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
|
834 %! 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
|
835 %! assert (kurtosis (x), 6/a, 2); |
6437 | 836 %! endif |
837 %!test | |
838 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
839 %! ## 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
|
840 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
841 %! a = 10; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
842 %! 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
|
843 %! 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
|
844 %! 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
|
845 %! 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
|
846 %! assert (kurtosis (x), 6/a, 0.5); |
6437 | 847 %! endif |
848 %!test | |
849 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
850 %! ## 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
|
851 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
852 %! a = 100; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
853 %! 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
|
854 %! 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
|
855 %! 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
|
856 %! 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
|
857 %! 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
|
858 %! endif |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
859 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
860 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
861 %!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
|
862 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
863 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
864 %! ## 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
|
865 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
866 %! a = 0.1; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
867 %! 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
|
868 %! 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
|
869 %! 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
|
870 %! 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
|
871 %! assert (kurtosis (x), 6/a, 50); |
6437 | 872 %! endif |
873 %!test | |
874 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
875 %! ## 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
|
876 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
877 %! a = 0.95; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
878 %! 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
|
879 %! 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
|
880 %! 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
|
881 %! 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
|
882 %! assert (kurtosis (x), 6/a, 2); |
6437 | 883 %! endif |
884 %!test | |
885 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
886 %! ## 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
|
887 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
888 %! a = 1; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
889 %! 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
|
890 %! 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
|
891 %! 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
|
892 %! 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
|
893 %! assert (kurtosis (x), 6/a, 2); |
6437 | 894 %! endif |
5730 | 895 %!test |
6437 | 896 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
897 %! ## 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
|
898 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
899 %! a = 10; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
900 %! 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
|
901 %! 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
|
902 %! 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
|
903 %! 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
|
904 %! assert (kurtosis (x), 6/a, 0.5); |
6437 | 905 %! endif |
5730 | 906 %!test |
6437 | 907 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
908 %! ## 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
|
909 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
910 %! a = 100; |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
911 %! 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
|
912 %! 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
|
913 %! 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
|
914 %! 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
|
915 %! assert (kurtosis (x), 6/a, 0.2); |
6437 | 916 %! endif |
5730 | 917 */ |
918 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
919 DEFUN (randp, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
920 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
921 @deftypefn {} {} randp (@var{l}, @var{n})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
922 @deftypefnx {} {} randp (@var{l}, @var{m}, @var{n}, @dots{})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
923 @deftypefnx {} {} randp (@var{l}, [@var{m} @var{n} @dots{}])\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
924 @deftypefnx {} {@var{v} =} randp (\"state\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
925 @deftypefnx {} {} randp (\"state\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
926 @deftypefnx {} {} randp (\"state\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
927 @deftypefnx {} {@var{v} =} randp (\"seed\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
928 @deftypefnx {} {} randp (\"seed\", @var{v})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
929 @deftypefnx {} {} randp (\"seed\", \"reset\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
930 @deftypefnx {} {} randp (@dots{}, \"single\")\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
931 @deftypefnx {} {} randp (@dots{}, \"double\")\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
932 Return a matrix with Poisson distributed random elements with mean value\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
933 parameter given by the first argument, @var{l}.\n\ |
5730 | 934 \n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
935 The arguments are handled the same as the arguments for @code{rand}, except\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
936 for the argument @var{l}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
937 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
938 Five different algorithms are used depending on the range of @var{l} and\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
939 whether or not @var{l} is a scalar or a matrix.\n\ |
5730 | 940 \n\ |
941 @table @asis\n\ | |
10840 | 942 @item For scalar @var{l} @leq{} 12, use direct method.\n\ |
943 W.H. Press, et al., @cite{Numerical Recipes in C},\n\ | |
944 Cambridge University Press, 1992.\n\ | |
945 \n\ | |
5730 | 946 @item For scalar @var{l} > 12, use rejection method.[1]\n\ |
10840 | 947 W.H. Press, et al., @cite{Numerical Recipes in C},\n\ |
948 Cambridge University Press, 1992.\n\ | |
949 \n\ | |
950 @item For matrix @var{l} @leq{} 10, use inversion method.[2]\n\ | |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
951 @nospell{E. Stadlober, et al., WinRand source code}, available via FTP.\n\ |
10840 | 952 \n\ |
5730 | 953 @item For matrix @var{l} > 10, use patchwork rejection method.\n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
954 @nospell{E. Stadlober, et al., WinRand source code}, available via FTP, or\n\ |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
955 @nospell{H. Zechner}, @cite{Efficient sampling from continuous and discrete\n\ |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10787
diff
changeset
|
956 unimodal distributions}, Doctoral Dissertation, 156pp., Technical\n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
957 University @nospell{Graz}, Austria, 1994.\n\ |
10840 | 958 \n\ |
5730 | 959 @item For @var{l} > 1e8, use normal approximation.\n\ |
19040
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
960 @nospell{L. Montanet}, et al., @cite{Review of Particle Properties},\n\ |
0850b5212619
doc: Add @nospell macro around proper names in documentation.
Rik <rik@octave.org>
parents:
18679
diff
changeset
|
961 Physical Review D 50 p1284, 1994.\n\ |
5730 | 962 @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
|
963 \n\ |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17258
diff
changeset
|
964 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
|
965 @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
|
966 classes.\n\ |
6547 | 967 @seealso{rand, randn, rande, randg}\n\ |
5730 | 968 @end deftypefn") |
969 { | |
970 int nargin = args.length (); | |
971 | |
972 if (nargin < 1) | |
973 error ("randp: insufficient arguments"); | |
974 | |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
975 return do_rand (args, nargin, "randp", "poisson", true); |
5730 | 976 } |
977 | |
978 /* | |
979 %!test | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
980 %! randp ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
981 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report |
6437 | 982 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
983 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
984 %! randp ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
985 %! assert (randp (5, 1, 6), [5 5 3 7 7 3]) |
6437 | 986 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
987 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
988 %! randp ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
989 %! assert (randp (15, 1, 6), [13 15 8 18 18 15]) |
6437 | 990 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
991 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
992 %! randp ("state", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
993 %! assert (randp (1e9, 1, 6), [999915677 999976657 1000047684 1000019035 999985749 999977692], -1e-6) |
6437 | 994 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
995 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
996 %! randp ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
997 %! %%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
|
998 %! assert (randp (5, 1, 5), [8 2 3 6 6]) |
6437 | 999 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1000 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1001 %! randp ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1002 %! assert (randp (15, 1, 6), [15 16 12 10 10 12]) |
6437 | 1003 %!test |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1004 %! ## Test fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1005 %! randp ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1006 %! assert (randp (1e9, 1, 6), [1000006208 1000012224 999981120 999963520 999963072 999981440], -1e-6) |
6437 | 1007 %!test |
1008 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1009 %! ## 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
|
1010 %! randp ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1011 %! 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
|
1012 %! 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
|
1013 %! 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
|
1014 %! 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
|
1015 %! 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
|
1016 %! 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
|
1017 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6437 | 1018 %! endfor |
1019 %! endif | |
1020 %!test | |
1021 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1022 %! ## 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
|
1023 %! randp ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1024 %! 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
|
1025 %! 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
|
1026 %! 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
|
1027 %! 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
|
1028 %! 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
|
1029 %! 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
|
1030 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6437 | 1031 %! endfor |
1032 %! endif | |
1033 %!test | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1034 %! randp ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1035 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report |
5730 | 1036 %!test |
6449 | 1037 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1038 %! ## 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
|
1039 %! randp ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1040 %! 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
|
1041 %! 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
|
1042 %! 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
|
1043 %! 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
|
1044 %! 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
|
1045 %! 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
|
1046 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6449 | 1047 %! endfor |
1048 %! endif | |
5730 | 1049 %!test |
6449 | 1050 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1051 %! ## 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
|
1052 %! randp ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1053 %! 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
|
1054 %! 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
|
1055 %! 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
|
1056 %! 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
|
1057 %! 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
|
1058 %! 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
|
1059 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6449 | 1060 %! endfor |
1061 %! endif | |
5730 | 1062 */ |
1063 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
1064 DEFUN (randperm, args, , |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
1065 "-*- texinfo -*-\n\ |
20853
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
1066 @deftypefn {} {} randperm (@var{n})\n\ |
1142cf6abc0d
2015 Code Sprint: remove class of function from docstring for all C++ files.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
1067 @deftypefnx {} {} randperm (@var{n}, @var{m})\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1068 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1069 \n\ |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1070 If @var{m} is supplied, return @var{m} unique entries, sampled without\n\ |
20172
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1071 replacement from @code{1:@var{n}}.\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1072 \n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1073 The complexity is O(@var{n}) in memory and O(@var{m}) in time, unless\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1074 @var{m} < @var{n}/5, in which case O(@var{m}) memory is used as well. The\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1075 randomization is performed using rand(). All permutations are equally\n\ |
4f45eaf83908
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
1076 likely.\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1077 @seealso{perms}\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1078 @end deftypefn") |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1079 { |
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
|
1080 #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
|
1081 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
|
1082 #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
|
1083 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
|
1084 #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
|
1085 |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1086 int nargin = args.length (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1087 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1088 if (nargin < 1 || nargin > 2) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1089 print_usage (); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1090 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1091 octave_idx_type n = args(0).idx_type_value (true); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1092 octave_idx_type m = (nargin == 2) ? args(1).idx_type_value (true) : n; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
1093 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1094 if (m < 0 || n < 0) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1095 error ("randperm: M and N must be non-negative"); |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1096 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1097 if (m > n) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1098 error ("randperm: M must be less than or equal to N"); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1099 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1100 // Quick and dirty heuristic to decide if we allocate or not the |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1101 // whole vector for tracking the truncated shuffle. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1102 bool short_shuffle = m < n/5; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1103 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1104 // Generate random numbers. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1105 NDArray r = octave_rand::nd_array (dim_vector (1, m)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1106 double *rvec = r.fortran_vec (); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1107 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1108 octave_idx_type idx_len = short_shuffle ? m : n; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1109 Array<octave_idx_type> idx; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1110 try |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1111 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1112 idx = Array<octave_idx_type> (dim_vector (1, idx_len)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1113 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1114 catch (const std::bad_alloc&) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1115 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1116 // Looks like n is too big and short_shuffle is false. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1117 // Let's try again, but this time with the alternative. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1118 idx_len = m; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1119 short_shuffle = true; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1120 idx = Array<octave_idx_type> (dim_vector (1, idx_len)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1121 } |
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
|
1122 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1123 octave_idx_type *ivec = idx.fortran_vec (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1124 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1125 for (octave_idx_type i = 0; i < idx_len; i++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1126 ivec[i] = i; |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1127 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1128 if (short_shuffle) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1129 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1130 unordered_map<octave_idx_type, octave_idx_type> map (m); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1131 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1132 // Perform the Knuth shuffle only keeping track of moved |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1133 // entries in the map |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1134 for (octave_idx_type i = 0; i < m; i++) |
20678
4b00afb5e9c3
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
1135 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1136 octave_idx_type k = i + |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1137 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
|
1138 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1139 // For shuffling first m entries, no need to use extra |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1140 // storage |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1141 if (k < m) |
20678
4b00afb5e9c3
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
1142 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1143 std::swap (ivec[i], ivec[k]); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1144 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1145 else |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1146 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1147 if (map.find (k) == map.end ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1148 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
|
1149 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1150 std::swap (ivec[i], map[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
|
1151 } |
20678
4b00afb5e9c3
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20535
diff
changeset
|
1152 } |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1153 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1154 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1155 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1156 // Perform the Knuth shuffle of the first m entries |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1157 for (octave_idx_type i = 0; i < m; i++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1158 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1159 octave_idx_type k = i + |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1160 gnulib::floor (rvec[i] * (n - i)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1161 std::swap (ivec[i], ivec[k]); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1162 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1163 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1164 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1165 // Convert to doubles, reusing r. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1166 for (octave_idx_type i = 0; i < m; i++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1167 rvec[i] = ivec[i] + 1; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1168 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1169 if (m < n) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1170 idx.resize (dim_vector (1, m)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1171 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1172 // Now create an array object with a cached idx_vector. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1173 return octave_value (new octave_matrix (r, idx_vector (idx))); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1174 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1175 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1176 /* |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1177 %!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
|
1178 %!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
|
1179 |
17662
0b7f5c56f853
Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
17336
diff
changeset
|
1180 ## Test biggish N (bug #39378) |
17812
fffd0c0ca2dc
maint: A few Octave coding convention tweaks
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
1181 %!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
|
1182 |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1183 %!test |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1184 %! rand ("seed", 0); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1185 %! 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
|
1186 %! 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
|
1187 %! 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
|
1188 %! endfor |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1189 */ |