Path: news.mathworks.com!not-for-mail
From: "Justin Abbott" <abbottjj@NOPSAM.saic.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Random for basic artihmetic operations
Date: Fri, 22 May 2009 01:57:01 +0000 (UTC)
Organization: SAIC
Lines: 30
Message-ID: <gv50pd$ib3$1@fred.mathworks.com>
References: <4a15cb07$0$31332$9b4e6d93@newsspool4.arcor-online.net>
Reply-To: "Justin Abbott" <abbottjj@NOPSAM.saic.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1242957421 18787 172.30.248.35 (22 May 2009 01:57:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 22 May 2009 01:57:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 489490
Xref: news.mathworks.com comp.soft-sys.matlab:541680


"Sven Schulz" <sven-schu@arcor.de> wrote in message <4a15cb07$0$31332$9b4e6d93@newsspool4.arcor-online.net>...
> Hi,
> 
> how can i design a random choice for basic arithmetic operations?
> 
> Example:
> 
> First random choice  A+B*C/E
> Second random choice A*B-C+E
> Third random choice  A-B+C+E
> an so on . . .
> 
> I have no idea to implement M-Code to solve this problem.
> 
> Regards
> 
> Sven

I am not really sure exactly what you want, but if you stick with the four possible operations you have in your example and assume that A,B,C,D are scalars
you could try something along the lines of:

% Cell array of function handles to the desired operations
op = {@plus @times @minus @rdivide};   
% Pick 3 operations with replacement
opDraw = ceil(4*rand(3,1));
val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));

After writing this down it is really ugly, so you might want to do it recursively.  Additionally it ignores any sort of operation precedent rules, but it is some sort of start.

-Justin