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
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 个评论
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
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:
- you will not want equal weighting of the choices;
- 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;
- the above has some problems with scoping of names;
- the above will execute the terms without ever displaying them
0 个评论
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
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
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!