# How to randomize operator?

**URL:** <https://forum.kirupa.com/t/how-to-randomize-operator/14688>\
**Category:** Uncategorized\
**Created:** [March 1, 2003, 11:55am UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688 "2003-03-01T11:55:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![cookie](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@cookie](https://forum.kirupa.com/u/cookie)\
**Post date:** [March 1, 2003, 11:55am UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/1 "2003-03-01T11:55:07Z")

</div>

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!

😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 2:25pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/2 "2003-03-01T14:25:52Z")

</div>

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=["+", “-”]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 3:29pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/3 "2003-03-01T15:29:16Z")

</div>

HELP HELP! ☹

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 3:41pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/4 "2003-03-01T15:41:37Z")

</div>

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

```auto

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 3:44pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/5 "2003-03-01T15:44:15Z")

</div>

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["/"]];

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 3:45pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/6 "2003-03-01T15:45:23Z")

</div>

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 :

```php

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 3:46pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/7 "2003-03-01T15:46:45Z")

</div>

Funny you answered 1min before my reply! =)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 3:59pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/8 "2003-03-01T15:59:16Z")

</div>

```auto

/ 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

🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 4:19pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/9 "2003-03-01T16:19:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 4:29pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/10 "2003-03-01T16:29:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 4:32pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/11 "2003-03-01T16:32:26Z")

</div>

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??

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 4:36pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/12 "2003-03-01T16:36:47Z")

</div>

then you can use something like this:

```auto

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);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 1, 2003, 4:44pm UTC](https://forum.kirupa.com/t/how-to-randomize-operator/14688/13 "2003-03-01T16:44:14Z")

</div>

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