Mercurial > hg > octave-lyh
diff src/DLD-FUNCTIONS/rand.cc @ 10782:d1f920d1ce0c
simplify code in rand.cc
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 12 Jul 2010 11:09:59 +0200 |
parents | 92a85ed5b86e |
children | ac433932ce23 |
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/rand.cc +++ b/src/DLD-FUNCTIONS/rand.cc @@ -51,13 +51,20 @@ static octave_value do_rand (const octave_value_list& args, int nargin, const char *fcn, - bool additional_arg = false) + const std::string& distribution, bool additional_arg = false) { octave_value retval; NDArray a; int idx = 0; dim_vector dims; + unwind_protect frame; + // Restore current distribution on any exit. + frame.add_fcn (octave_rand::distribution, + octave_rand::distribution ()); + + octave_rand::distribution (distribution); + if (additional_arg) { if (nargin == 0) @@ -397,7 +404,7 @@ int nargin = args.length (); - retval = do_rand (args, nargin, "rand"); + retval = do_rand (args, nargin, "rand", "uniform"); return retval; } @@ -508,21 +515,7 @@ int nargin = args.length (); - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "normal"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "randn"); + retval = do_rand (args, nargin, "randn", "normal"); return retval; } @@ -581,21 +574,7 @@ int nargin = args.length (); - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "exponential"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "rande"); + retval = do_rand (args, nargin, "rande", "exponential"); return retval; } @@ -712,23 +691,7 @@ if (nargin < 1) error ("randg: insufficient arguments"); else - { - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "gamma"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "randg", true); - } + retval = do_rand (args, nargin, "randg", "gamma", true); return retval; } @@ -927,23 +890,7 @@ if (nargin < 1) error ("randp: insufficient arguments"); else - { - unwind_protect frame; - - // This relies on the fact that elements are popped from the unwind - // stack in the reverse of the order they are pushed - // (i.e. current_distribution will be reset before calling - // reset_rand_generator()). - - frame.add_fcn (reset_rand_generator); - frame.protect_var (current_distribution); - - current_distribution = "poisson"; - - octave_rand::distribution (current_distribution); - - retval = do_rand (args, nargin, "randp", true); - } + retval = do_rand (args, nargin, "randp", "poisson", true); return retval; }