I want to generate random (integers only between -10 and 25) mathematical expressions and also evaluate them. Can someone help with the code?

조회 수: 1 (최근 30일)
I want to generate random (integers only between -10 and 25) mathematical expressions and also evaluate them. For example -1 + (-3) -(10) - (-3)(5)/3 + 7 - (-6)(3) + 12. Maximum number of terms can be 10. Should have the option to select the operators like ^2 (squaring).

채택된 답변

Guillaume
Guillaume 2017년 10월 10일
Something like this ?
numberrange = [-10, 25];
maxoperators = 10;
numops = randi(maxoperators); %number of operations to generate
operators = ["+", "-", "/", "*", "^"]; %requires R2017a or later
numbers = randi(numberrange, 1, numops+1);
%convert to string, but negative numbers need to be enclosed in ()
numstrings = string(numbers); %requires R2016b or later
numstrings(numbers < 0) = compose("(%d)", numbers(numbers < 0));
%replace some numbers with bracketed sum or subtraction
numreplace = randi([0, numops]);
toreplace = randperm(numops, numreplace);
numstrings(toreplace) = compose("(%d%+d)", randi(numberrange, [numreplace, 1]), randi(numberrange, [numreplace, 1]));
%create expression
operations = operators(randi(numel(operators), 1, numops));
result = strjoin([numstrings; operations, ""], "")
And to evaluate the result:
eval(result)
Note: There are no provisions in the above to avoid division by 0
  댓글 수: 5
Seetha Rama Raju Sanapala
Seetha Rama Raju Sanapala 2017년 10월 11일
@Guillaume, I have checked repeatedly and the program is working well. Only I request the following modifications as per my new observations. Powers should be restricted to between -2 to 2 - otherwise the final answers are becoming to big. The other numbers can be between -10 and 25. And also do not want more than two ^s in the expression.
Guillaume
Guillaume 2017년 10월 11일
do not want more than two ^s in the expression.
Well, then I suggest that you roll first for the operations, and if more than two ^ are chosen, you reroll, something like:
powerindex = 5; %index of ^ in the operators cell array
opidx = randi(numel(operators), 1, numops);
while nnz(opidx == powerindex) > 2
opidx = randi(numel(operators), 1, numops)
end
operations = operators(opidx);
Powers should be restricted to between -2 to 2
Probably the easiest is to override numstrings after they've been computed. Something like:
powerrange = [-2, 2];
numstrings([false, opidx == powerindex]) = sprintfc('%d, randi(powerrange, [nnz(opidx == powerindex), 1]));

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2017년 10월 10일
Roughly,
unary_operators = {@(x) x, @(x) -x, @(x) x.^2, @(x) 1./x, @(x) log(x), @(x) exp(x), @(x) gamma(x+1)}
binary_operators = {@(x,y) x+y; @(x,y) x-y, @(x,y) x.*y, @(x,y) x./y, @(x,y) x.^y};
nu = length(unary_operators);
nb = length(binary_operators);
random_unary_operator = @(t) unary_operators{randperm(nu,1)}(t);
random_binary_operator = @(t1, t2) binary_operators{randperm(nb,1)}(t1, t2);
random_unary_term = @() random_unary_operator( random_term() );
random_binary_term = @() random_binary_operator( random_term(), random_term() );
function t = random_term()
r = rand;
if r < 0.123
t = random_constant();
elseif r < 0.876
t = random_unary_term();
else
t = random_binary_term();
end
end
But not exactly the above, because:
  1. you will not want equal weighting of the choices;
  2. the above has no inherent limit on the number of constants generated, and if you use equal random weighting of the choices then the number produced will often be too large;
  3. the above has some problems with scoping of names;
  4. the above will execute the terms without ever displaying them

Image Analyst
Image Analyst 2017년 10월 10일
To generate the random numbers, do this:
% Generate 50 numbers between -10 and 25, inclusive.
r = randi(36, 1, 50) - 11;
As far as doing 10 operations on them, you'll have to explain that more and better.
  댓글 수: 3
Guillaume
Guillaume 2017년 10월 10일
... this has 6 operators ... this has 10 operators
Why aren't the operators in (-3-4) (2 operators), (3-4) and (2+3) counted?
Walter Roberson
Walter Roberson 2017년 10월 10일
Is the exponent restricted to integers? To positive integers? Could you confirm that something like (-10)^25/(((5-(-7)^9)^3)/11) would be considered valid?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by