 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member Join Date
Nov 2008
Posts
2

## Random mathmatical operator

Thanks all Last edited by jordieg03; December 10th, 2008 at 04:40 AM.  Reply With Quote

2. Elite Member Power Poster           Join Date
Jan 2006
Location
Singapore
Posts
6,768

## Re: Random mathmatical operator

You can generate a (pseudo)random number (possibly using std::srand() and std::rand() from <cstdlib>) and map it to one of the operators (the principle is the same whether you mean merely characters or actual operations).  Reply With Quote

3. Junior Member Join Date
Nov 2008
Posts
2

## Re: Random mathmatical operator Originally Posted by laserlight You can generate a (pseudo)random number (possibly using std::srand() and std::rand() from <cstdlib>) and map it to one of the operators (the principle is the same whether you mean merely characters or actual operations).
Ok so I would need to generate a random number (Was what I had initially thought), but then how would I map an operator to the number?

Sorry to sound like such a numptie. Would it be possible to give a quick example perhaps if it's not too much hassle?
Last edited by jordieg03; December 10th, 2008 at 04:40 AM.  Reply With Quote

4. Elite Member Power Poster           Join Date
Jan 2006
Location
Singapore
Posts
6,768

## Re: Random mathmatical operator Originally Posted by jordieg03
how would I map an operator to the number?
More like map a number to an operator. You could use an if-else chain, a switch, an array, a std::map, etc.  Reply With Quote

5. ## Re: Random mathmatical operator

When overloading the operators, the rule should always be "Do what the integers do".

For example the following code should cause someone to be shot:
Code:
```MyClass operator +(MyClass const &a, MyClass const &b)
{
return a.Value - b.Value;
}```  Reply With Quote

6. ## Re: Random mathmatical operator

TheCPUWizard has made a good point, the client should be able to clearly understand what the overloaded operators purpose is just by looking at the interface and not having to sift through the implementation to see how it really works.
If it seems unclear at what an operator does with a value, then it should be a function instead.  Reply With Quote

7. ## Re: Random mathmatical operator

I believe the OP didn't ask about operator overloading, rather how to implement some random logic of selection which arithmetic operator to use in some computation.

Here's a simple function that uses the rand() function to generate a number between low and high, inclusive:
Code:
```int randomNumber(int low, int high)
{
int range = high - low + 1;
int result = low + rand % range;

return result;
}```

Regards,
Zachm  Reply With Quote

8. _uj Banned   Join Date
Nov 2003
Posts
1,405

## Re: Random mathmatical operator Originally Posted by jordieg03 Part of a piece of code I'm writing I need to generate a random mathematical operator (+ - / *).
What do you mean by random operator?

Is it an operator that's "fuzzy" so that for example 1+1 will be 2 with high probability but also could be 0 or 3 with a lower probability?

Or do you just want to generate numbers in a range like throwing a die and get 1, 2, 3, 4, 5 or 6 with equal probability 1/6?
Last edited by _uj; November 16th, 2008 at 02:21 PM.  Reply With Quote

9. ## Re: Random mathmatical operator Originally Posted by Zachm I believe the OP didn't ask about operator overloading, rather how to implement some random logic of selection which arithmetic operator to use in some computation.
Quite possible. But the word "operator" has a specific meaning in C++. Section 5 (Expressions) of the ISO specification provides the definition of what qualifiers as an "operator".  Reply With Quote

10. Member Join Date
Nov 2008
Posts
90

## Re: Random mathmatical operator

I would have to agree with the Wiz. You are confusing the term "operator" with something else. Section 5 (Expressions) of the ISO specification strictly forbids operating on data types with "user-defined operators".  Reply With Quote

11. Elite Member Power Poster           Join Date
Jan 2006
Location
Singapore
Posts
6,768

## Re: Random mathmatical operator Originally Posted by Zachm
I believe the OP didn't ask about operator overloading, rather how to implement some random logic of selection which arithmetic operator to use in some computation.
Yes, that is how I interpreted the question (or if not an arithmetic operation, then one of the characters '+', '-', '/' or '*', chosen at random). In retrospect, _uj's question is worth repeating: what do you mean by random operator?  Reply With Quote

12. Member Join Date
Jul 2008
Posts
38

## Re: Random mathmatical operator

Try something like this (if i understood what you wanted correctly that is)

Code:
```double randOper(double a, double b)
{
switch(rand()&#37;4)
{
case 0:
return a+b;

case 1:
return a-b;

case 2:
return a*b;

case 3:
return a/b; // Div by zero?
}
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 