How to randomize operator?

I’m trying to get flash to randomize an arithmetic operator from a set of operators, i.e.

operators = + - * / etc

and I want flash to randomly pick one from the list!
How do I do this? I looked at the random tut on kirupa but I don’t know how to make it work with anything other than numbers!

:stuck_out_tongue:

PLease help! Should I use an array with the diff. operators and then have it pick one randomly or what? How would I do this?

Someone!! I need this fast please! I can use an array and randomise an operator from a list but then it will be a string and I can’t use the random operator to execute calculations! I want to both display the operator in a dynamic textfield and use it in calculations!

Therefore, I can’t use this I think:

operators=["+", “-”]

HELP HELP! :frowning:

you really dont have much control over operators in Flash. What you can do is something like this though:


Math.sum = function(a,b){ return a + b; }
Math.subtract = function(a,b){ return a - b; }
Math.multiply = function(a,b){ return a * b; }
Math.divide = function(a,b){ return a / b; }

operators = [Math.sum, Math.subtract, Math.multiply, Math.divide];

// test 10 times on numbers 1 and 2
for (j=0; j< 10; j++){
	operation = operators[ random(operators.length) ];
	trace( operation(1, 2) );
}

/* ==possible output:==*\

2		<- multiply
0.5		<- divide
2		<- multiply
-1		<- subtract
2		<- multiply
-1		<- subtract
0.5		<- divide
0.5		<- divide
3		<- sum 
3		<- sum 

\*=====================*/ 

This sets up math functions which perform the operations on two values and returns the result. those functions can then be put in an array and be called randomly

btw, if you needed to reference them using strings you could use

Math["+"] = function(a,b){ return a + b; }
Math["-"] = function(a,b){ return a - b; }
Math["*"] = function(a,b){ return a * b; }
Math["/"] = function(a,b){ return a / b; }

operators = [Math["+"], Math["-"], Math["*"], Math["/"]];

thanks senocular! I knew that when I saw you enter the forum you would be the one to answer my question!

Thanks =)

My actionscript skills are limited, so do you think that you could give me a brief explanation of the following code, too confusing:P :


/ test 10 times on numbers 1 and 2
for (j=0; j< 10; j++){
	operation = operators[ random(operators.length) ];
	trace( operation(1, 2) );
}

Funny you answered 1min before my reply! =)


/ test 10 times on numbers 1 and 2
for (j=0; j< 10; j++){
	operation = operators[ random(operators.length) ];
	trace( operation(1, 2) );
}

for (_ ; _; _ ){…} is a method of looping through the same code many times over again. It uses numbers to count how many times to loop through the code (The code between the { and }, in this case
operation = operators[ random(operators.length) ];
trace( operation(1, 2) );
is what is being looped through). The first part of the for loop indicates the starting value of the number (j) controlling the loops. Here I started it at 0. The next part, after the semicolon, tells the for loop whether or not it should continue to loop. He I used j< 10 meaning, as long as the value in j is less than 10, keep repeating that code over and over again. The part after that is where the number is changed to determine when the loop stops. Here 1 is added to j every loop (using the ++ operator). So after the first loop, j is no longer 0 but 1. The following loop will make j 2, then 3 and 4 and so on… so on until j is no longer less than 10 at which point the loop stops.

operation = operators[ random(operators.length) ];

assigns a variable operation to something in the operators array. Each element of the operators array is a function, so operation itself will become a function. What element of the operators array it becomes is determined by

random(operators.length)

This choses a random number from 0 upto but not including the length of the array. Because the operators array length is 4, this returns a number from 0-3, which make up the 4 indexes of the array.

operators[0] == Math.sum
operators[1] == Math.subtract
operators[2] == Math.multiply
operators[3] == Math.divide

so operation will be one of those functions randomly. Following that is the actual function call of operation (since its assigned to be a function, it must be used as it is a function)

trace( operation(1, 2) );

This not only calls the function (with the arguments 1 and 2) but it traces the result in the output window. Because operation is a random operation of either sum, subtract, multiply or divide, the outcome of the trace will be either 1 + 2, 1 - 2, 1 * 2, or 1 / 2 since operation does any one of those things depending on what it was randomly assigned to from the previous line.

Because those two lines are within the { and } of the for loop, they are run through 10 times and the trace displays 10 different randomly selected operations of sum, subtract, multiply or divide performed on the numbers 1 and 2

:slight_smile:

Thanks for the breakdown senocular! That helped! I just have one problem. Ok so the array will be randomised so that the operation order is diff. but say that I want the operator itself displayed in a textfield and not the answer! What do I do?? I tried this:

operater.text=operations[1];

so the first operation in the array is display but I just want the operator displayed, i.e “+”!

Thannks

do you need to do the operation or just display the operator?

OK I want to randomise which operator is executed and use this operator on numbers “a” and “b”! I also want the random operator to be displayed in a textfield, i.e. “+”. So all the different operators don’t need to be applied to the numbers “a” and “b”, unless that is the way to do it??

then you can use something like this:


Math["+"] = function(a,b){ return a + b; }
Math["-"] = function(a,b){ return a - b; }
Math["*"] = function(a,b){ return a * b; }
Math["/"] = function(a,b){ return a / b; }

operators = ["+", "-", "*", "/"];

a = 1;
b = 2;
sign = operators[ random(operators.length) ];

operater.text = a + sign + b + "=" + Math[sign](a,b);

You’re the man senocular! You make it “look” easy! =)