help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

rand within dld function


From: Dirk Eddelbuettel
Subject: rand within dld function
Date: Tue, 8 Dec 1998 22:00:47 -0500 (EST)

Hi Heber,

  Heber>  A simple question: How do I generate one uniform random number from
  Heber> within a .oct file function?  For some reason I can't make it work
  Heber> out.

The following code is a variation on what JWE once recommended for calling
functions in .m files from code in .oct files. After all, the mechanism is
the same -- you just have tell Octave's parser what you want to get executed.

So the following will call Octave's "rand" function with whatever argument is
in args(0). See below for how calls with '1' and '3' give just what rand(1)
and rand(3) would have generated.

Cheers, Dirk

-----------------------------------------------------------------------------
#include "config.h"
#include "oct-obj.h"
#include "oct-map.h"
#include "defun-dld.h"
#include "toplev.h"

DEFUN_DLD (call_oct_from_oct, args, nargout,
  "Should uniform random variable of dimension args(0).")
{
  string func = "rand";

  // read arg 0
  double x = args(0).double_value();

  // and put it into a new list
  octave_value_list arg;
  arg(0) = x;

  // function to call
  octave_value_list retval = feval (func, arg, 1);

  return retval;
}
-----------------------------------------------------------------------------


address@hidden:~/octave/oct/source> octave
Octave, version 2.0.13.94 (i386-pc-linux-gnu).
Copyright (C) 1996, 1997, 1998 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

octave:1> call_oct_from_oct(1)
ans = 0.63730
octave:2> call_oct_from_oct(3)
ans =

  0.10647  0.49915  0.57270
  0.47568  0.75489  0.65061
  0.86098  0.28920  0.88621



-- 
Linux is not only free; it is, arguably, a better operating system, offering
a degree of stability and an ability to scale up that NT cannot match.
                                             -- The Economist, Oct 3, 1998



reply via email to

[Prev in Thread] Current Thread [Next in Thread]