I want to generate random (integers only between -10 and 25) mathematical expressions and also evaluate them. Can someone help with the code?
2 ビュー (過去 30 日間)
古いコメントを表示
Seetha Rama Raju Sanapala
2017 年 10 月 10 日
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!