GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rand.cc
Go to the documentation of this file.
1 
2 /*
3 
4 Copyright (C) 1996-2015 John W. Eaton
5 Copyright (C) 2009 VZLU Prague
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
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
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
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <ctime>
30 #if defined (HAVE_UNORDERED_MAP)
31 #include <unordered_map>
32 #elif defined (HAVE_TR1_UNORDERED_MAP)
33 #include <tr1/unordered_map>
34 #endif
35 #include <string>
36 
37 #include "f77-fcn.h"
38 #include "lo-mappers.h"
39 #include "oct-rand.h"
40 #include "quit.h"
41 
42 #include "defun.h"
43 #include "error.h"
44 #include "gripes.h"
45 #include "oct-obj.h"
46 #include "unwind-prot.h"
47 #include "utils.h"
48 #include "ov-re-mat.h"
49 
50 /*
51 %!shared __random_statistical_tests__
52 %! ## Flag whether the statistical tests should be run in "make check" or not
53 %! __random_statistical_tests__ = 0;
54 */
55 
56 static octave_value
57 do_rand (const octave_value_list& args, int nargin, const char *fcn,
58  const std::string& distribution, bool additional_arg = false)
59 {
60  octave_value retval;
61  NDArray a;
62  int idx = 0;
63  dim_vector dims;
64  bool is_single = false;
65 
66  unwind_protect frame;
67  // Restore current distribution on any exit.
70 
71  octave_rand::distribution (distribution);
72 
73  if (nargin > 0 && args(nargin-1).is_string ())
74  {
75  std::string s_arg = args(nargin-1).string_value ();
76 
77  if (s_arg == "single")
78  {
79  is_single = true;
80  nargin--;
81  }
82  else if (s_arg == "double")
83  nargin--;
84  }
85 
86  if (additional_arg)
87  {
88  if (nargin == 0)
89  {
90  error ("%s: expecting at least one argument", fcn);
91  goto done;
92  }
93  else if (args(0).is_string ())
94  additional_arg = false;
95  else
96  {
97  a = args(0).array_value ();
98  if (error_state)
99  {
100  error ("%s: expecting scalar or matrix arguments", fcn);
101  goto done;
102  }
103  idx++;
104  nargin--;
105  }
106  }
107 
108  switch (nargin)
109  {
110  case 0:
111  {
112  if (additional_arg)
113  dims = a.dims ();
114  else
115  {
116  dims.resize (2);
117 
118  dims(0) = 1;
119  dims(1) = 1;
120  }
121  goto gen_matrix;
122  }
123  break;
124 
125  case 1:
126  {
127  octave_value tmp = args(idx);
128 
129  if (tmp.is_string ())
130  {
131  std::string s_arg = tmp.string_value ();
132 
133  if (s_arg == "dist")
134  {
135  retval = octave_rand::distribution ();
136  }
137  else if (s_arg == "seed")
138  {
139  retval = octave_rand::seed ();
140  }
141  else if (s_arg == "state" || s_arg == "twister")
142  {
143  retval = octave_rand::state (fcn);
144  }
145  else if (s_arg == "uniform")
146  {
148  }
149  else if (s_arg == "normal")
150  {
152  }
153  else if (s_arg == "exponential")
154  {
156  }
157  else if (s_arg == "poisson")
158  {
160  }
161  else if (s_arg == "gamma")
162  {
164  }
165  else
166  error ("%s: unrecognized string argument", fcn);
167  }
168  else if (tmp.is_scalar_type ())
169  {
170  double dval = tmp.double_value ();
171 
172  if (xisnan (dval))
173  {
174  error ("%s: NaN is invalid matrix dimension", fcn);
175  }
176  else
177  {
178  dims.resize (2);
179 
180  dims(0) = NINTbig (tmp.double_value ());
181  dims(1) = NINTbig (tmp.double_value ());
182 
183  if (! error_state)
184  goto gen_matrix;
185  }
186  }
187  else if (tmp.is_range ())
188  {
189  Range r = tmp.range_value ();
190 
191  if (r.all_elements_are_ints ())
192  {
193  octave_idx_type n = r.nelem ();
194 
195  dims.resize (n);
196 
197  octave_idx_type base = NINTbig (r.base ());
198  octave_idx_type incr = NINTbig (r.inc ());
199 
200  for (octave_idx_type i = 0; i < n; i++)
201  {
202  //Negative dimensions are treated as zero for Matlab
203  //compatibility
204  dims(i) = base >= 0 ? base : 0;
205  base += incr;
206  }
207 
208  goto gen_matrix;
209 
210  }
211  else
212  error ("%s: all elements of range must be integers",
213  fcn);
214  }
215  else if (tmp.is_matrix_type ())
216  {
217  Array<int> iv = tmp.int_vector_value (true);
218 
219  if (! error_state)
220  {
221  octave_idx_type len = iv.length ();
222 
223  dims.resize (len);
224 
225  for (octave_idx_type i = 0; i < len; i++)
226  {
227  //Negative dimensions are treated as zero for Matlab
228  //compatibility
229  octave_idx_type elt = iv(i);
230  dims(i) = elt >=0 ? elt : 0;
231  }
232 
233  goto gen_matrix;
234  }
235  else
236  error ("%s: expecting integer vector", fcn);
237  }
238  else
239  {
240  gripe_wrong_type_arg ("rand", tmp);
241  return retval;
242  }
243  }
244  break;
245 
246  default:
247  {
248  octave_value tmp = args(idx);
249 
250  if (nargin == 2 && tmp.is_string ())
251  {
252  std::string ts = tmp.string_value ();
253 
254  if (ts == "seed")
255  {
256  if (args(idx+1).is_real_scalar ())
257  {
258  double d = args(idx+1).double_value ();
259 
260  if (! error_state)
261  octave_rand::seed (d);
262  }
263  else if (args(idx+1).is_string ()
264  && args(idx+1).string_value () == "reset")
266  else
267  error ("%s: seed must be a real scalar", fcn);
268  }
269  else if (ts == "state" || ts == "twister")
270  {
271  if (args(idx+1).is_string ()
272  && args(idx+1).string_value () == "reset")
273  octave_rand::reset (fcn);
274  else
275  {
276  ColumnVector s =
277  ColumnVector (args(idx+1).vector_value(false, true));
278 
279  if (! error_state)
280  octave_rand::state (s, fcn);
281  }
282  }
283  else
284  error ("%s: unrecognized string argument", fcn);
285  }
286  else
287  {
288  dims.resize (nargin);
289 
290  for (int i = 0; i < nargin; i++)
291  {
292  octave_idx_type elt = args(idx+i).int_value ();
293  if (error_state)
294  {
295  error ("%s: expecting integer arguments", fcn);
296  goto done;
297  }
298  //Negative is zero for Matlab compatibility
299  dims(i) = elt >= 0 ? elt : 0;
300  }
301 
302  goto gen_matrix;
303  }
304  }
305  break;
306  }
307 
308 done:
309 
310  return retval;
311 
312 gen_matrix:
313 
314  dims.chop_trailing_singletons ();
315 
316  if (is_single)
317  {
318  if (additional_arg)
319  {
320  if (a.length () == 1)
321  return octave_rand::float_nd_array (dims, a(0));
322  else
323  {
324  if (a.dims () != dims)
325  {
326  error ("%s: mismatch in argument size", fcn);
327  return retval;
328  }
329  octave_idx_type len = a.length ();
330  FloatNDArray m (dims);
331  float *v = m.fortran_vec ();
332  for (octave_idx_type i = 0; i < len; i++)
333  v[i] = octave_rand::float_scalar (a(i));
334  return m;
335  }
336  }
337  else
338  return octave_rand::float_nd_array (dims);
339  }
340  else
341  {
342  if (additional_arg)
343  {
344  if (a.length () == 1)
345  return octave_rand::nd_array (dims, a(0));
346  else
347  {
348  if (a.dims () != dims)
349  {
350  error ("%s: mismatch in argument size", fcn);
351  return retval;
352  }
353  octave_idx_type len = a.length ();
354  NDArray m (dims);
355  double *v = m.fortran_vec ();
356  for (octave_idx_type i = 0; i < len; i++)
357  v[i] = octave_rand::scalar (a(i));
358  return m;
359  }
360  }
361  else
362  return octave_rand::nd_array (dims);
363  }
364 }
365 
366 DEFUN (rand, args, ,
367  "-*- texinfo -*-\n\
368 @deftypefn {Built-in Function} {} rand (@var{n})\n\
369 @deftypefnx {Built-in Function} {} rand (@var{m}, @var{n}, @dots{})\n\
370 @deftypefnx {Built-in Function} {} rand ([@var{m} @var{n} @dots{}])\n\
371 @deftypefnx {Built-in Function} {@var{v} =} rand (\"state\")\n\
372 @deftypefnx {Built-in Function} {} rand (\"state\", @var{v})\n\
373 @deftypefnx {Built-in Function} {} rand (\"state\", \"reset\")\n\
374 @deftypefnx {Built-in Function} {@var{v} =} rand (\"seed\")\n\
375 @deftypefnx {Built-in Function} {} rand (\"seed\", @var{v})\n\
376 @deftypefnx {Built-in Function} {} rand (\"seed\", \"reset\")\n\
377 @deftypefnx {Built-in Function} {} rand (@dots{}, \"single\")\n\
378 @deftypefnx {Built-in Function} {} rand (@dots{}, \"double\")\n\
379 Return a matrix with random elements uniformly distributed on the\n\
380 interval (0, 1).\n\
381 \n\
382 The arguments are handled the same as the arguments for @code{eye}.\n\
383 \n\
384 You can query the state of the random number generator using the form\n\
385 \n\
386 @example\n\
387 v = rand (\"state\")\n\
388 @end example\n\
389 \n\
390 This returns a column vector @var{v} of length 625. Later, you can restore\n\
391 the random number generator to the state @var{v} using the form\n\
392 \n\
393 @example\n\
394 rand (\"state\", v)\n\
395 @end example\n\
396 \n\
397 @noindent\n\
398 You may also initialize the state vector from an arbitrary vector of length\n\
399 @leq{} 625 for @var{v}. This new state will be a hash based on the value of\n\
400 @var{v}, not @var{v} itself.\n\
401 \n\
402 By default, the generator is initialized from @code{/dev/urandom} if it is\n\
403 available, otherwise from CPU time, wall clock time, and the current\n\
404 fraction of a second. Note that this differs from @sc{matlab}, which\n\
405 always initializes the state to the same state at startup. To obtain\n\
406 behavior comparable to @sc{matlab}, initialize with a deterministic state\n\
407 vector in Octave's startup files (@pxref{Startup Files}).\n\
408 \n\
409 To compute the pseudo-random sequence, @code{rand} uses the Mersenne\n\
410 Twister with a period of @math{2^{19937}-1}\n\
411 (See @nospell{M. Matsumoto and T. Nishimura},\n\
412 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform\n\
413 pseudorandom number generator},\n\
414 ACM Trans. on Modeling and Computer Simulation Vol. 8, No. 1, pp. 3--30,\n\
415 January 1998,\n\
416 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\
417 Do @strong{not} use for cryptography without securely hashing several\n\
418 returned values together, otherwise the generator state can be learned after\n\
419 reading 624 consecutive values.\n\
420 \n\
421 Older versions of Octave used a different random number generator.\n\
422 The new generator is used by default as it is significantly faster than the\n\
423 old generator, and produces random numbers with a significantly longer cycle\n\
424 time. However, in some circumstances it might be desirable to obtain the\n\
425 same random sequences as produced by the old generators. To do this the\n\
426 keyword @qcode{\"seed\"} is used to specify that the old generators should\n\
427 be used, as in\n\
428 \n\
429 @example\n\
430 rand (\"seed\", val)\n\
431 @end example\n\
432 \n\
433 @noindent\n\
434 which sets the seed of the generator to @var{val}. The seed of the\n\
435 generator can be queried with\n\
436 \n\
437 @example\n\
438 s = rand (\"seed\")\n\
439 @end example\n\
440 \n\
441 However, it should be noted that querying the seed will not cause\n\
442 @code{rand} to use the old generators, only setting the seed will. To cause\n\
443 @code{rand} to once again use the new generators, the keyword\n\
444 @qcode{\"state\"} should be used to reset the state of the @code{rand}.\n\
445 \n\
446 The state or seed of the generator can be reset to a new random value using\n\
447 the @qcode{\"reset\"} keyword.\n\
448 \n\
449 The class of the value returned can be controlled by a trailing\n\
450 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
451 classes.\n\
452 @seealso{randn, rande, randg, randp}\n\
453 @end deftypefn")
454 {
455  octave_value retval;
456 
457  int nargin = args.length ();
458 
459  retval = do_rand (args, nargin, "rand", "uniform");
460 
461  return retval;
462 }
463 
464 // FIXME: The old generator (selected when "seed" is set) will not
465 // work properly if compiled to use 64-bit integers.
466 
467 /*
468 %!test # "state" can be a scalar
469 %! rand ("state", 12); x = rand (1,4);
470 %! rand ("state", 12); y = rand (1,4);
471 %! assert (x, y);
472 %!test # "state" can be a vector
473 %! rand ("state", [12,13]); x = rand (1,4);
474 %! rand ("state", [12;13]); y = rand (1,4);
475 %! assert (x, y);
476 %!test # querying "state" doesn't disturb sequence
477 %! rand ("state", 12); rand (1,2); x = rand (1,2);
478 %! rand ("state", 12); rand (1,2);
479 %! s = rand ("state"); y = rand (1,2);
480 %! assert (x, y);
481 %! rand ("state", s); z = rand (1,2);
482 %! assert (x, z);
483 %!test # "seed" must be a scalar
484 %! rand ("seed", 12); x = rand (1,4);
485 %! rand ("seed", 12); y = rand (1,4);
486 %! assert (x, y);
487 %!error <seed must be a real scalar> rand ("seed", [12,13])
488 %!test # querying "seed" returns a value which can be used later
489 %! s = rand ("seed"); x = rand (1,2);
490 %! rand ("seed", s); y = rand (1,2);
491 %! assert (x, y);
492 %!test # querying "seed" doesn't disturb sequence
493 %! rand ("seed", 12); rand (1,2); x = rand (1,2);
494 %! rand ("seed", 12); rand (1,2);
495 %! s = rand ("seed"); y = rand (1,2);
496 %! assert (x, y);
497 %! rand ("seed", s); z = rand (1,2);
498 %! assert (x, z);
499 */
500 
501 /*
502 %!test
503 %! ## Test fixed state
504 %! rand ("state", 1);
505 %! assert (rand (1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382], 1e-6);
506 %!test
507 %! ## Test fixed seed
508 %! rand ("seed", 1);
509 %! assert (rand (1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759], 1e-6);
510 %!test
511 %! if (__random_statistical_tests__)
512 %! ## statistical tests may fail occasionally.
513 %! rand ("state", 12);
514 %! x = rand (100000, 1);
515 %! assert (max (x) < 1); #*** Please report this!!! ***
516 %! assert (min (x) > 0); #*** Please report this!!! ***
517 %! assert (mean (x), 0.5, 0.0024);
518 %! assert (var (x), 1/48, 0.0632);
519 %! assert (skewness (x), 0, 0.012);
520 %! assert (kurtosis (x), -6/5, 0.0094);
521 %! endif
522 %!test
523 %! if (__random_statistical_tests__)
524 %! ## statistical tests may fail occasionally.
525 %! rand ("seed", 12);
526 %! x = rand (100000, 1);
527 %! assert (max (x) < 1); #*** Please report this!!! ***
528 %! assert (min (x) > 0); #*** Please report this!!! ***
529 %! assert (mean (x), 0.5, 0.0024);
530 %! assert (var (x), 1/48, 0.0632);
531 %! assert (skewness (x), 0, 0.012);
532 %! assert (kurtosis (x), -6/5, 0.0094);
533 %! endif
534 */
535 
536 /*
537 %!# Test out-of-range values as rand() seeds. See oct-rand.cc: double2uint32().
538 %!function v = __rand_sample__ (initval)
539 %! rand ("state", initval);
540 %! v = rand (1, 6);
541 %!endfunction
542 %!
543 %!assert (__rand_sample__ (0), __rand_sample__ (2^32))
544 %!assert (__rand_sample__ (-2), __rand_sample__ (2^32-2))
545 %!assert (__rand_sample__ (Inf), __rand_sample__ (NaN))
546 %!assert (! isequal (__rand_sample__ (-1), __rand_sample__ (-2)))
547 */
548 
550 
551 DEFUN (randn, args, ,
552  "-*- texinfo -*-\n\
553 @deftypefn {Built-in Function} {} randn (@var{n})\n\
554 @deftypefnx {Built-in Function} {} randn (@var{m}, @var{n}, @dots{})\n\
555 @deftypefnx {Built-in Function} {} randn ([@var{m} @var{n} @dots{}])\n\
556 @deftypefnx {Built-in Function} {@var{v} =} randn (\"state\")\n\
557 @deftypefnx {Built-in Function} {} randn (\"state\", @var{v})\n\
558 @deftypefnx {Built-in Function} {} randn (\"state\", \"reset\")\n\
559 @deftypefnx {Built-in Function} {@var{v} =} randn (\"seed\")\n\
560 @deftypefnx {Built-in Function} {} randn (\"seed\", @var{v})\n\
561 @deftypefnx {Built-in Function} {} randn (\"seed\", \"reset\")\n\
562 @deftypefnx {Built-in Function} {} randn (@dots{}, \"single\")\n\
563 @deftypefnx {Built-in Function} {} randn (@dots{}, \"double\")\n\
564 Return a matrix with normally distributed random elements having zero mean\n\
565 and variance one.\n\
566 \n\
567 The arguments are handled the same as the arguments for @code{rand}.\n\
568 \n\
569 By default, @code{randn} uses the @nospell{Marsaglia and Tsang}\n\
570 ``Ziggurat technique'' to transform from a uniform to a normal distribution.\n\
571 \n\
572 The class of the value returned can be controlled by a trailing\n\
573 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
574 classes.\n\
575 \n\
576 Reference: @nospell{G. Marsaglia and W.W. Tsang},\n\
577 @cite{Ziggurat Method for Generating Random Variables},\n\
578 J. Statistical Software, vol 5, 2000,\n\
579 @url{http://www.jstatsoft.org/v05/i08/}\n\
580 \n\
581 @seealso{rand, rande, randg, randp}\n\
582 @end deftypefn")
583 {
584  octave_value retval;
585 
586  int nargin = args.length ();
587 
588  retval = do_rand (args, nargin, "randn", "normal");
589 
590  return retval;
591 }
592 
593 /*
594 %!test
595 %! ## Test fixed state
596 %! randn ("state", 1);
597 %! assert (randn (1, 6), [-2.666521678978671 -0.7381719971724564 1.507903992673601 0.6019427189162239 -0.450661261143348 -0.7054431351574116], 1e-6);
598 %!test
599 %! ## Test fixed seed
600 %! randn ("seed", 1);
601 %! assert (randn (1, 6), [-1.039402365684509 -1.25938892364502 0.1968704611063004 0.3874166905879974 -0.5976632833480835 -0.6615074276924133], 1e-6);
602 %!test
603 %! if (__random_statistical_tests__)
604 %! ## statistical tests may fail occasionally.
605 %! randn ("state", 12);
606 %! x = randn (100000, 1);
607 %! assert (mean (x), 0, 0.01);
608 %! assert (var (x), 1, 0.02);
609 %! assert (skewness (x), 0, 0.02);
610 %! assert (kurtosis (x), 0, 0.04);
611 %! endif
612 %!test
613 %! if (__random_statistical_tests__)
614 %! ## statistical tests may fail occasionally.
615 %! randn ("seed", 12);
616 %! x = randn (100000, 1);
617 %! assert (mean (x), 0, 0.01);
618 %! assert (var (x), 1, 0.02);
619 %! assert (skewness (x), 0, 0.02);
620 %! assert (kurtosis (x), 0, 0.04);
621 %! endif
622 */
623 
624 DEFUN (rande, args, ,
625  "-*- texinfo -*-\n\
626 @deftypefn {Built-in Function} {} rande (@var{n})\n\
627 @deftypefnx {Built-in Function} {} rande (@var{m}, @var{n}, @dots{})\n\
628 @deftypefnx {Built-in Function} {} rande ([@var{m} @var{n} @dots{}])\n\
629 @deftypefnx {Built-in Function} {@var{v} =} rande (\"state\")\n\
630 @deftypefnx {Built-in Function} {} rande (\"state\", @var{v})\n\
631 @deftypefnx {Built-in Function} {} rande (\"state\", \"reset\")\n\
632 @deftypefnx {Built-in Function} {@var{v} =} rande (\"seed\")\n\
633 @deftypefnx {Built-in Function} {} rande (\"seed\", @var{v})\n\
634 @deftypefnx {Built-in Function} {} rande (\"seed\", \"reset\")\n\
635 @deftypefnx {Built-in Function} {} rande (@dots{}, \"single\")\n\
636 @deftypefnx {Built-in Function} {} rande (@dots{}, \"double\")\n\
637 Return a matrix with exponentially distributed random elements.\n\
638 \n\
639 The arguments are handled the same as the arguments for @code{rand}.\n\
640 \n\
641 By default, @code{randn} uses the @nospell{Marsaglia and Tsang}\n\
642 ``Ziggurat technique'' to transform from a uniform to a normal distribution.\n\
643 \n\
644 The class of the value returned can be controlled by a trailing\n\
645 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
646 classes.\n\
647 \n\
648 Reference: @nospell{G. Marsaglia and W.W. Tsang},\n\
649 @cite{Ziggurat Method for Generating Random Variables},\n\
650 J. Statistical Software, vol 5, 2000,\n\
651 @url{http://www.jstatsoft.org/v05/i08/}\n\
652 \n\
653 @seealso{rand, randn, randg, randp}\n\
654 @end deftypefn")
655 {
656  octave_value retval;
657 
658  int nargin = args.length ();
659 
660  retval = do_rand (args, nargin, "rande", "exponential");
661 
662  return retval;
663 }
664 
665 /*
666 %!test
667 %! ## Test fixed state
668 %! rande ("state", 1);
669 %! assert (rande (1, 6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291], 1e-6);
670 %!test
671 %! ## Test fixed seed
672 %! rande ("seed", 1);
673 %! assert (rande (1, 6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087], 1e-6);
674 %!test
675 %! if (__random_statistical_tests__)
676 %! ## statistical tests may fail occasionally
677 %! rande ("state", 1);
678 %! x = rande (100000, 1);
679 %! assert (min (x) > 0); # *** Please report this!!! ***
680 %! assert (mean (x), 1, 0.01);
681 %! assert (var (x), 1, 0.03);
682 %! assert (skewness (x), 2, 0.06);
683 %! assert (kurtosis (x), 6, 0.7);
684 %! endif
685 %!test
686 %! if (__random_statistical_tests__)
687 %! ## statistical tests may fail occasionally
688 %! rande ("seed", 1);
689 %! x = rande (100000, 1);
690 %! assert (min (x)>0); # *** Please report this!!! ***
691 %! assert (mean (x), 1, 0.01);
692 %! assert (var (x), 1, 0.03);
693 %! assert (skewness (x), 2, 0.06);
694 %! assert (kurtosis (x), 6, 0.7);
695 %! endif
696 */
697 
698 DEFUN (randg, args, ,
699  "-*- texinfo -*-\n\
700 @deftypefn {Built-in Function} {} randg (@var{n})\n\
701 @deftypefnx {Built-in Function} {} randg (@var{m}, @var{n}, @dots{})\n\
702 @deftypefnx {Built-in Function} {} randg ([@var{m} @var{n} @dots{}])\n\
703 @deftypefnx {Built-in Function} {@var{v} =} randg (\"state\")\n\
704 @deftypefnx {Built-in Function} {} randg (\"state\", @var{v})\n\
705 @deftypefnx {Built-in Function} {} randg (\"state\", \"reset\")\n\
706 @deftypefnx {Built-in Function} {@var{v} =} randg (\"seed\")\n\
707 @deftypefnx {Built-in Function} {} randg (\"seed\", @var{v})\n\
708 @deftypefnx {Built-in Function} {} randg (\"seed\", \"reset\")\n\
709 @deftypefnx {Built-in Function} {} randg (@dots{}, \"single\")\n\
710 @deftypefnx {Built-in Function} {} randg (@dots{}, \"double\")\n\
711 Return a matrix with @code{gamma (@var{a},1)} distributed random elements.\n\
712 \n\
713 The arguments are handled the same as the arguments for @code{rand}, except\n\
714 for the argument @var{a}.\n\
715 \n\
716 This can be used to generate many distributions:\n\
717 \n\
718 @table @asis\n\
719 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\
720 \n\
721 @example\n\
722 r = b * randg (a)\n\
723 @end example\n\
724 \n\
725 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\
726 \n\
727 @example\n\
728 @group\n\
729 r1 = randg (a, 1)\n\
730 r = r1 / (r1 + randg (b, 1))\n\
731 @end group\n\
732 @end example\n\
733 \n\
734 @item @code{Erlang (a, n)}\n\
735 \n\
736 @example\n\
737 r = a * randg (n)\n\
738 @end example\n\
739 \n\
740 @item @code{chisq (df)} for @code{df > 0}\n\
741 \n\
742 @example\n\
743 r = 2 * randg (df / 2)\n\
744 @end example\n\
745 \n\
746 @item @code{t (df)} for @code{0 < df < inf} (use randn if df is infinite)\n\
747 \n\
748 @example\n\
749 r = randn () / sqrt (2 * randg (df / 2) / df)\n\
750 @end example\n\
751 \n\
752 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\
753 \n\
754 @example\n\
755 @group\n\
756 ## r1 equals 1 if n1 is infinite\n\
757 r1 = 2 * randg (n1 / 2) / n1\n\
758 ## r2 equals 1 if n2 is infinite\n\
759 r2 = 2 * randg (n2 / 2) / n2\n\
760 r = r1 / r2\n\n\
761 @end group\n\
762 @end example\n\
763 \n\
764 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\
765 \n\
766 @example\n\
767 r = randp ((1 - p) / p * randg (n))\n\
768 @end example\n\
769 \n\
770 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\
771 (use chisq if @code{L = 0})\n\
772 \n\
773 @example\n\
774 @group\n\
775 r = randp (L / 2)\n\
776 r(r > 0) = 2 * randg (r(r > 0))\n\
777 r(df > 0) += 2 * randg (df(df > 0)/2)\n\
778 @end group\n\
779 @end example\n\
780 \n\
781 @item @code{Dirichlet (a1, @dots{} ak)}\n\
782 \n\
783 @example\n\
784 @group\n\
785 r = (randg (a1), @dots{}, randg (ak))\n\
786 r = r / sum (r)\n\
787 @end group\n\
788 @end example\n\
789 \n\
790 @end table\n\
791 \n\
792 The class of the value returned can be controlled by a trailing\n\
793 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
794 classes.\n\
795 @seealso{rand, randn, rande, randp}\n\
796 @end deftypefn")
797 {
798  octave_value retval;
799 
800  int nargin = args.length ();
801 
802  if (nargin < 1)
803  error ("randg: insufficient arguments");
804  else
805  retval = do_rand (args, nargin, "randg", "gamma", true);
806 
807  return retval;
808 }
809 
810 /*
811 %!test
812 %! randg ("state", 12)
813 %! assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]); # *** Please report
814 
815 %!test
816 %! ## Test fixed state
817 %! randg ("state", 1);
818 %! assert (randg (0.1, 1, 6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12], 1e-6);
819 %!test
820 %! ## Test fixed state
821 %! randg ("state", 1);
822 %! assert (randg (0.95, 1, 6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939], 1e-6);
823 %!test
824 %! ## Test fixed state
825 %! randg ("state", 1);
826 %! assert (randg (1, 1, 6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533], 1e-6);
827 %!test
828 %! ## Test fixed state
829 %! randg ("state", 1);
830 %! assert (randg (10, 1, 6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059], 1e-5);
831 %!test
832 %! ## Test fixed state
833 %! randg ("state", 1);
834 %! assert (randg (100, 1, 6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118], 1e-4);
835 %!test
836 %! ## Test fixed seed
837 %! randg ("seed", 1);
838 %! assert (randg (0.1, 1, 6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12], 1e-6);
839 %!test
840 %! ## Test fixed seed
841 %! randg ("seed", 1);
842 %! assert (randg (0.95, 1, 6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648], 1e-6);
843 %!test
844 %! ## Test fixed seed
845 %! randg ("seed", 1);
846 %! assert (randg (1, 1, 6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303], 1e-6);
847 %!test
848 %! ## Test fixed seed
849 %! randg ("seed", 1);
850 %! assert (randg (10, 1, 6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186], 1e-5);
851 %!test
852 %! ## Test fixed seed
853 %! randg ("seed", 1);
854 %! assert (randg (100, 1, 6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625], 1e-4);
855 %!test
856 %! ## Test out-of-bounds values produce NaN w/old-style generators & floats
857 %! randg ("seed", 1);
858 %! result = randg ([-2 Inf], "single");
859 %! assert (result, single ([NaN NaN]));
860 
861 %!test
862 %! if (__random_statistical_tests__)
863 %! ## statistical tests may fail occasionally.
864 %! randg ("state", 12);
865 %! a = 0.1;
866 %! x = randg (a, 100000, 1);
867 %! assert (mean (x), a, 0.01);
868 %! assert (var (x), a, 0.01);
869 %! assert (skewness (x), 2/sqrt (a), 1);
870 %! assert (kurtosis (x), 6/a, 50);
871 %! endif
872 %!test
873 %! if (__random_statistical_tests__)
874 %! ## statistical tests may fail occasionally.
875 %! randg ("state", 12);
876 %! a = 0.95;
877 %! x = randg (a, 100000, 1);
878 %! assert (mean (x), a, 0.01);
879 %! assert (var (x), a, 0.04);
880 %! assert (skewness (x), 2/sqrt (a), 0.2);
881 %! assert (kurtosis (x), 6/a, 2);
882 %! endif
883 %!test
884 %! if (__random_statistical_tests__)
885 %! ## statistical tests may fail occasionally.
886 %! randg ("state", 12);
887 %! a = 1;
888 %! x = randg (a, 100000, 1);
889 %! assert (mean (x), a, 0.01);
890 %! assert (var (x), a, 0.04);
891 %! assert (skewness (x), 2/sqrt (a), 0.2);
892 %! assert (kurtosis (x), 6/a, 2);
893 %! endif
894 %!test
895 %! if (__random_statistical_tests__)
896 %! ## statistical tests may fail occasionally.
897 %! randg ("state", 12);
898 %! a = 10;
899 %! x = randg (a, 100000, 1);
900 %! assert (mean (x), a, 0.1);
901 %! assert (var (x), a, 0.5);
902 %! assert (skewness (x), 2/sqrt (a), 0.1);
903 %! assert (kurtosis (x), 6/a, 0.5);
904 %! endif
905 %!test
906 %! if (__random_statistical_tests__)
907 %! ## statistical tests may fail occasionally.
908 %! randg ("state", 12);
909 %! a = 100;
910 %! x = randg (a, 100000, 1);
911 %! assert (mean (x), a, 0.2);
912 %! assert (var (x), a, 2);
913 %! assert (skewness (x), 2/sqrt (a), 0.05);
914 %! assert (kurtosis (x), 6/a, 0.2);
915 %! endif
916 %!test
917 %! randg ("seed", 12);
918 %!assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]) # *** Please report
919 %!test
920 %! if (__random_statistical_tests__)
921 %! ## statistical tests may fail occasionally.
922 %! randg ("seed", 12);
923 %! a = 0.1;
924 %! x = randg (a, 100000, 1);
925 %! assert (mean (x), a, 0.01);
926 %! assert (var (x), a, 0.01);
927 %! assert (skewness (x), 2/sqrt (a), 1);
928 %! assert (kurtosis (x), 6/a, 50);
929 %! endif
930 %!test
931 %! if (__random_statistical_tests__)
932 %! ## statistical tests may fail occasionally.
933 %! randg ("seed", 12);
934 %! a = 0.95;
935 %! x = randg (a, 100000, 1);
936 %! assert (mean (x), a, 0.01);
937 %! assert (var (x), a, 0.04);
938 %! assert (skewness (x), 2/sqrt (a), 0.2);
939 %! assert (kurtosis (x), 6/a, 2);
940 %! endif
941 %!test
942 %! if (__random_statistical_tests__)
943 %! ## statistical tests may fail occasionally.
944 %! randg ("seed", 12);
945 %! a = 1;
946 %! x = randg (a, 100000, 1);
947 %! assert (mean (x), a, 0.01);
948 %! assert (var (x), a, 0.04);
949 %! assert (skewness (x), 2/sqrt (a), 0.2);
950 %! assert (kurtosis (x), 6/a, 2);
951 %! endif
952 %!test
953 %! if (__random_statistical_tests__)
954 %! ## statistical tests may fail occasionally.
955 %! randg ("seed", 12);
956 %! a = 10;
957 %! x = randg (a, 100000, 1);
958 %! assert (mean (x), a, 0.1);
959 %! assert (var (x), a, 0.5);
960 %! assert (skewness (x), 2/sqrt (a), 0.1);
961 %! assert (kurtosis (x), 6/a, 0.5);
962 %! endif
963 %!test
964 %! if (__random_statistical_tests__)
965 %! ## statistical tests may fail occasionally.
966 %! randg ("seed", 12);
967 %! a = 100;
968 %! x = randg (a, 100000, 1);
969 %! assert (mean (x), a, 0.2);
970 %! assert (var (x), a, 2);
971 %! assert (skewness (x), 2/sqrt (a), 0.05);
972 %! assert (kurtosis (x), 6/a, 0.2);
973 %! endif
974 */
975 
976 DEFUN (randp, args, ,
977  "-*- texinfo -*-\n\
978 @deftypefn {Built-in Function} {} randp (@var{l}, @var{n})\n\
979 @deftypefnx {Built-in Function} {} randp (@var{l}, @var{m}, @var{n}, @dots{})\n\
980 @deftypefnx {Built-in Function} {} randp (@var{l}, [@var{m} @var{n} @dots{}])\n\
981 @deftypefnx {Built-in Function} {@var{v} =} randp (\"state\")\n\
982 @deftypefnx {Built-in Function} {} randp (\"state\", @var{v})\n\
983 @deftypefnx {Built-in Function} {} randp (\"state\", \"reset\")\n\
984 @deftypefnx {Built-in Function} {@var{v} =} randp (\"seed\")\n\
985 @deftypefnx {Built-in Function} {} randp (\"seed\", @var{v})\n\
986 @deftypefnx {Built-in Function} {} randp (\"seed\", \"reset\")\n\
987 @deftypefnx {Built-in Function} {} randp (@dots{}, \"single\")\n\
988 @deftypefnx {Built-in Function} {} randp (@dots{}, \"double\")\n\
989 Return a matrix with Poisson distributed random elements with mean value\n\
990 parameter given by the first argument, @var{l}.\n\
991 \n\
992 The arguments are handled the same as the arguments for @code{rand}, except\n\
993 for the argument @var{l}.\n\
994 \n\
995 Five different algorithms are used depending on the range of @var{l} and\n\
996 whether or not @var{l} is a scalar or a matrix.\n\
997 \n\
998 @table @asis\n\
999 @item For scalar @var{l} @leq{} 12, use direct method.\n\
1000 W.H. Press, et al., @cite{Numerical Recipes in C},\n\
1001 Cambridge University Press, 1992.\n\
1002 \n\
1003 @item For scalar @var{l} > 12, use rejection method.[1]\n\
1004 W.H. Press, et al., @cite{Numerical Recipes in C},\n\
1005 Cambridge University Press, 1992.\n\
1006 \n\
1007 @item For matrix @var{l} @leq{} 10, use inversion method.[2]\n\
1008 @nospell{E. Stadlober, et al., WinRand source code}, available via FTP.\n\
1009 \n\
1010 @item For matrix @var{l} > 10, use patchwork rejection method.\n\
1011 @nospell{E. Stadlober, et al., WinRand source code}, available via FTP, or\n\
1012 @nospell{H. Zechner}, @cite{Efficient sampling from continuous and discrete\n\
1013 unimodal distributions}, Doctoral Dissertation, 156pp., Technical\n\
1014 University @nospell{Graz}, Austria, 1994.\n\
1015 \n\
1016 @item For @var{l} > 1e8, use normal approximation.\n\
1017 @nospell{L. Montanet}, et al., @cite{Review of Particle Properties},\n\
1018 Physical Review D 50 p1284, 1994.\n\
1019 @end table\n\
1020 \n\
1021 The class of the value returned can be controlled by a trailing\n\
1022 @qcode{\"double\"} or @qcode{\"single\"} argument. These are the only valid\n\
1023 classes.\n\
1024 @seealso{rand, randn, rande, randg}\n\
1025 @end deftypefn")
1026 {
1027  octave_value retval;
1028 
1029  int nargin = args.length ();
1030 
1031  if (nargin < 1)
1032  error ("randp: insufficient arguments");
1033  else
1034  retval = do_rand (args, nargin, "randp", "poisson", true);
1035 
1036  return retval;
1037 }
1038 
1039 /*
1040 %!test
1041 %! randp ("state", 12);
1042 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report
1043 %!test
1044 %! ## Test fixed state
1045 %! randp ("state", 1);
1046 %! assert (randp (5, 1, 6), [5 5 3 7 7 3])
1047 %!test
1048 %! ## Test fixed state
1049 %! randp ("state", 1);
1050 %! assert (randp (15, 1, 6), [13 15 8 18 18 15])
1051 %!test
1052 %! ## Test fixed state
1053 %! randp ("state", 1);
1054 %! assert (randp (1e9, 1, 6), [999915677 999976657 1000047684 1000019035 999985749 999977692], -1e-6)
1055 %!test
1056 %! ## Test fixed state
1057 %! randp ("seed", 1);
1058 %! %%assert (randp (5, 1, 6), [8 2 3 6 6 8])
1059 %! assert (randp (5, 1, 5), [8 2 3 6 6])
1060 %!test
1061 %! ## Test fixed state
1062 %! randp ("seed", 1);
1063 %! assert (randp (15, 1, 6), [15 16 12 10 10 12])
1064 %!test
1065 %! ## Test fixed state
1066 %! randp ("seed", 1);
1067 %! assert (randp (1e9, 1, 6), [1000006208 1000012224 999981120 999963520 999963072 999981440], -1e-6)
1068 %!test
1069 %! if (__random_statistical_tests__)
1070 %! ## statistical tests may fail occasionally.
1071 %! randp ("state", 12);
1072 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
1073 %! x = randp (a (1), 100000, 1);
1074 %! assert (min (x) >= 0); # *** Please report this!!! ***
1075 %! assert (mean (x), a(1), a(2));
1076 %! assert (var (x), a(1), 0.02*a(1));
1077 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
1078 %! assert (kurtosis (x), 1/a(1), 3*a(3));
1079 %! endfor
1080 %! endif
1081 %!test
1082 %! if (__random_statistical_tests__)
1083 %! ## statistical tests may fail occasionally.
1084 %! randp ("state", 12);
1085 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
1086 %! x = randp (a(1)*ones (100000, 1), 100000, 1);
1087 %! assert (min (x) >= 0); # *** Please report this!!! ***
1088 %! assert (mean (x), a(1), a(2));
1089 %! assert (var (x), a(1), 0.02*a(1));
1090 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
1091 %! assert (kurtosis (x), 1/a(1), 3*a(3));
1092 %! endfor
1093 %! endif
1094 %!test
1095 %! randp ("seed", 12);
1096 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report
1097 %!test
1098 %! if (__random_statistical_tests__)
1099 %! ## statistical tests may fail occasionally.
1100 %! randp ("seed", 12);
1101 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
1102 %! x = randp (a(1), 100000, 1);
1103 %! assert (min (x) >= 0); # *** Please report this!!! ***
1104 %! assert (mean (x), a(1), a(2));
1105 %! assert (var (x), a(1), 0.02*a(1));
1106 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
1107 %! assert (kurtosis (x), 1/a(1), 3*a(3));
1108 %! endfor
1109 %! endif
1110 %!test
1111 %! if (__random_statistical_tests__)
1112 %! ## statistical tests may fail occasionally.
1113 %! randp ("seed", 12);
1114 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03]
1115 %! x = randp (a(1)*ones (100000, 1), 100000, 1);
1116 %! assert (min (x) >= 0); # *** Please report this!!! ***
1117 %! assert (mean (x), a(1), a(2));
1118 %! assert (var (x), a(1), 0.02*a(1));
1119 %! assert (skewness (x), 1/sqrt (a(1)), a(3));
1120 %! assert (kurtosis (x), 1/a(1), 3*a(3));
1121 %! endfor
1122 %! endif
1123 */
1124 
1125 DEFUN (randperm, args, ,
1126  "-*- texinfo -*-\n\
1127 @deftypefn {Built-in Function} {} randperm (@var{n})\n\
1128 @deftypefnx {Built-in Function} {} randperm (@var{n}, @var{m})\n\
1129 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\
1130 \n\
1131 If @var{m} is supplied, return @var{m} unique entries, sampled without\n\
1132 replacement from @code{1:@var{n}}.\n\
1133 \n\
1134 The complexity is O(@var{n}) in memory and O(@var{m}) in time, unless\n\
1135 @var{m} < @var{n}/5, in which case O(@var{m}) memory is used as well. The\n\
1136 randomization is performed using rand(). All permutations are equally\n\
1137 likely.\n\
1138 @seealso{perms}\n\
1139 @end deftypefn")
1140 {
1141 
1142 #ifdef USE_UNORDERED_MAP_WITH_TR1
1143 using std::tr1::unordered_map;
1144 #else
1145 using std::unordered_map;
1146 #endif
1147 
1148  int nargin = args.length ();
1149  octave_value retval;
1150 
1151  if (nargin == 1 || nargin == 2)
1152  {
1153  octave_idx_type n, m;
1154 
1155  n = args(0).idx_type_value (true);
1156 
1157  if (nargin == 2)
1158  m = args(1).idx_type_value (true);
1159  else
1160  m = n;
1161 
1162  if (m < 0 || n < 0)
1163  error ("randperm: M and N must be non-negative");
1164 
1165  if (m > n)
1166  error ("randperm: M must be less than or equal to N");
1167 
1168  // Quick and dirty heuristic to decide if we allocate or not the
1169  // whole vector for tracking the truncated shuffle.
1170  bool short_shuffle = m < n/5;
1171 
1172  if (! error_state)
1173  {
1174  // Generate random numbers.
1176  double *rvec = r.fortran_vec ();
1177 
1178  octave_idx_type idx_len = short_shuffle ? m : n;
1180  try
1181  {
1182  idx = Array<octave_idx_type> (dim_vector (1, idx_len));
1183  }
1184  catch (std::bad_alloc)
1185  {
1186  // Looks like n is too big and short_shuffle is false.
1187  // Let's try again, but this time with the alternative.
1188  idx_len = m;
1189  short_shuffle = true;
1190  idx = Array<octave_idx_type> (dim_vector (1, idx_len));
1191  }
1192 
1193  octave_idx_type *ivec = idx.fortran_vec ();
1194 
1195  for (octave_idx_type i = 0; i < idx_len; i++)
1196  ivec[i] = i;
1197 
1198  if (short_shuffle)
1199  {
1200  unordered_map<octave_idx_type, octave_idx_type> map (m);
1201 
1202  // Perform the Knuth shuffle only keeping track of moved
1203  // entries in the map
1204  for (octave_idx_type i = 0; i < m; i++)
1205  {
1206  octave_idx_type k = i +
1207  gnulib::floor (rvec[i] * (n - i));
1208 
1209  //For shuffling first m entries, no need to use extra
1210  //storage
1211  if (k < m)
1212  {
1213  std::swap (ivec[i], ivec[k]);
1214  }
1215  else
1216  {
1217  if (map.find (k) == map.end ())
1218  map[k] = k;
1219 
1220  std::swap (ivec[i], map[k]);
1221  }
1222  }
1223  }
1224  else
1225  {
1226 
1227  // Perform the Knuth shuffle of the first m entries
1228  for (octave_idx_type i = 0; i < m; i++)
1229  {
1230  octave_idx_type k = i +
1231  gnulib::floor (rvec[i] * (n - i));
1232  std::swap (ivec[i], ivec[k]);
1233  }
1234  }
1235 
1236  // Convert to doubles, reusing r.
1237  for (octave_idx_type i = 0; i < m; i++)
1238  rvec[i] = ivec[i] + 1;
1239 
1240  if (m < n)
1241  idx.resize (dim_vector (1, m));
1242 
1243  // Now create an array object with a cached idx_vector.
1244  retval = new octave_matrix (r, idx_vector (idx));
1245  }
1246  }
1247  else
1248  print_usage ();
1249 
1250  return retval;
1251 }
1252 
1253 /*
1254 %!assert (sort (randperm (20)), 1:20)
1255 %!assert (length (randperm (20,10)), 10)
1256 
1257 ## Test biggish N (bug #39378)
1258 %!assert (length (randperm (30000^2, 100000)), 100000)
1259 
1260 %!test
1261 %! rand ("seed", 0);
1262 %! for i = 1:100
1263 %! p = randperm (305, 30);
1264 %! assert (length (unique (p)), 30);
1265 %! endfor
1266 */
bool is_range(void) const
Definition: ov.h:571
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
Definition: gripes.cc:135
static void exponential_distribution(void)
Definition: oct-rand.h:115
bool xisnan(double x)
Definition: lo-mappers.cc:144
static octave_value do_rand(const octave_value_list &args, int nargin, const char *fcn, const std::string &distribution, bool additional_arg=false)
Definition: rand.cc:57
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
octave_idx_type nelem(void) const
Definition: Range.h:64
bool is_scalar_type(void) const
Definition: ov.h:657
void resize(int n, int fill_value=0)
Definition: dim-vector.h:287
Definition: Range.h:31
static void reset(void)
Definition: oct-rand.h:62
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
void error(const char *fmt,...)
Definition: error.cc:476
static float float_scalar(float a=1.0)
Definition: oct-rand.h:140
F77_RET_T const double const double double * d
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:337
static std::string current_distribution
Definition: rand.cc:549
void add_fcn(void(*fcn)(void))
std::string string_value(bool force=false) const
Definition: ov.h:897
static double seed(void)
Definition: oct-rand.h:49
static void gamma_distribution(void)
Definition: oct-rand.h:127
Range range_value(void) const
Definition: ov.h:903
bool is_matrix_type(void) const
Definition: ov.h:660
bool is_string(void) const
Definition: ov.h:562
double inc(void) const
Definition: Range.h:63
int error_state
Definition: error.cc:101
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
static void poisson_distribution(void)
Definition: oct-rand.h:121
octave_idx_type length(void) const
Definition: ov.cc:1525
static ColumnVector state(const std::string &d=std::string())
Definition: oct-rand.h:69
static NDArray nd_array(const dim_vector &dims, double a=1.0)
Definition: oct-rand.h:159
Handles the reference counting for all the derived classes.
Definition: Array.h:45
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:267
static FloatNDArray float_nd_array(const dim_vector &dims, float a=1.0)
Definition: oct-rand.h:167
Array< octave_value > array_value(void) const
Definition: oct-obj.h:79
static void normal_distribution(void)
Definition: oct-rand.h:109
bool all_elements_are_ints(void) const
Definition: Range.cc:40
double base(void) const
Definition: Range.h:61
static std::string distribution(void)
Definition: oct-rand.h:90
Array< int > int_vector_value(bool req_int=false, bool frc_str_conv=false, bool frc_vec_conv=false) const
Definition: ov.cc:1717
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:282
const T * fortran_vec(void) const
Definition: Array.h:481
octave_idx_type NINTbig(double x)
Definition: lo-mappers.cc:635
static double scalar(double a=1.0)
Definition: oct-rand.h:134
double double_value(bool frc_str_conv=false) const
Definition: ov.h:759
void chop_trailing_singletons(void)
Definition: dim-vector.h:214
static void uniform_distribution(void)
Definition: oct-rand.h:103