# HG changeset patch # User Jaroslav Hajek # Date 1278925799 -7200 # Node ID d1f920d1ce0c75b2d69e0edb159ed716680dcf05 # Parent f7584d0ba5d3d2bc491c96743ff0f7e0472bb5b8 simplify code in rand.cc diff -r f7584d0ba5d3 -r d1f920d1ce0c src/ChangeLog --- a/src/ChangeLog Mon Jul 12 10:52:15 2010 +0200 +++ b/src/ChangeLog Mon Jul 12 11:09:59 2010 +0200 @@ -1,3 +1,9 @@ +2010-07-12 Jaroslav Hajek + + * DLD-FUNCTIONS/rand.cc (do_rand): Pass the distribution name as an + argument. Ensure restoration using unwind_protect. + (Frand, Frandn, Frandg, Frandp, Frande): Update. + 2010-07-12 Jaroslav Hajek * unwind-prot.h (unwind_protect::fcn_crefarg_elem): New class. diff -r f7584d0ba5d3 -r d1f920d1ce0c src/DLD-FUNCTIONS/rand.cc --- a/src/DLD-FUNCTIONS/rand.cc Mon Jul 12 10:52:15 2010 +0200 +++ b/src/DLD-FUNCTIONS/rand.cc Mon Jul 12 11:09:59 2010 +0200 @@ -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; }