Jump to content

[SOLVED] randomly add, subract or multiply


Dragen

Recommended Posts

okay, this is probably a stupid question, but I can't figure out an answer..

I'm trying to randomly add, subtract or multpily from a number. I create the number and also the second number which will be used for the equation.

I've also got the mathematic operators (I hope that's the right term..) stored in an array:

$matho = array('+', '-', '*');

 

How can I then get php to parse it all as a mathematics equation?

I mean this obviously doesn't work:

echo $number1 . $matho[rand(0, 1)] . $number2;

 

thanks

Link to comment
Share on other sites

I'd do it like this

 

$matho = array('+', '-', '*');
$math_var = $matho[rand(0, 2)];

switch ($math_var)
{
case '+':
	echo $number1 + $number2;
	break;
case '-':
	echo $number1 - $number2;
	break;
case '*':
	echo $number1 * $number2;
	break;
}

Link to comment
Share on other sites

You can't bring up an equation symbol from a variable without using eval.

 

The following code should work (or something similar using eval). I don't use eval as much anymore but I know it's something like this.

 

$matho = array('+', '-', '*');
eval("echo $number1 " . $matho[rand(0,2)] . " $number2;");

Link to comment
Share on other sites

I would shy away from using eval if you are letting users enter their own input.  It's too much of a security risk.

 

DylanBlit's method is the way I would do it.

 

The only exception is I would change $math_var to this for sake of making it just a little more dynamic and easier to read:

srand((float)microtime() * 1000000); // (PHP4 Only) make it more random.
shuffle($matho);
$math_var = $matho[0];

Link to comment
Share on other sites

thanks for the advice guys. eval is something I hadn't looked into before.

I'm really not wanting to have to write out a whole switch statement just for a simple maths equation. It means that each time I want to change an operator I have to change the whole thing.

I've gone with darklink's suggestion of eval as I don't need to worry about user input or anything and it's quicker to make changes.

Here's my eval solution:

$n = rand(1, 75);
$n2 = rand(1,12);
eval("\$this->check = $n" . $matho[$opt] . "$n2;");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.