comparison libinterp/corefcn/rand.cc @ 17258:213ee68b59da

Handle out-of-range values consistently when initializing random number generator. * oct-rand.cc: New function double2uint32(). It normalizes a finite double by taking it modulo 2^32, such that the result is in [0, 2^32). Nonfinites give zero as result. This prevents compiler-dependent casting of a double whose truncated value cannot be represented in a uint32_t. * rand.cc: Add %!test block to verify behavior.
author Philipp Kutin <philipp.kutin@gmail.com>
date Thu, 15 Aug 2013 20:14:30 +0200
parents 2fc554ffbc28
children bc924baa2c4e
comparison
equal deleted inserted replaced
17257:ce55a8575874 17258:213ee68b59da
531 %! assert (skewness (x), 0, 0.012); 531 %! assert (skewness (x), 0, 0.012);
532 %! assert (kurtosis (x), -6/5, 0.0094); 532 %! assert (kurtosis (x), -6/5, 0.0094);
533 %! endif 533 %! endif
534 */ 534 */
535 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
536 static std::string current_distribution = octave_rand::distribution (); 549 static std::string current_distribution = octave_rand::distribution ();
537 550
538 DEFUN (randn, args, , 551 DEFUN (randn, args, ,
539 "-*- texinfo -*-\n\ 552 "-*- texinfo -*-\n\
540 @deftypefn {Built-in Function} {} randn (@var{n})\n\ 553 @deftypefn {Built-in Function} {} randn (@var{n})\n\