-
November 16th, 2008, 12:22 PM
#1
Random mathmatical operator
Thanks all
Last edited by jordieg03; December 10th, 2008 at 04:40 AM.
-
November 16th, 2008, 12:27 PM
#2
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).
-
November 16th, 2008, 12:38 PM
#3
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.
-
November 16th, 2008, 12:47 PM
#4
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.
-
November 16th, 2008, 01:02 PM
#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;
}
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 16th, 2008, 01:36 PM
#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.
-
November 16th, 2008, 02:14 PM
#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;
}
Read about rand(), srand(), and the switch statement.
Regards,
Zachm
-
November 16th, 2008, 02:17 PM
#8
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.
-
November 16th, 2008, 04:56 PM
#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".
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
November 17th, 2008, 02:29 AM
#10
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".
-
November 17th, 2008, 04:21 AM
#11
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?
-
November 17th, 2008, 12:24 PM
#12
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()%4)
{
case 0:
return a+b;
case 1:
return a-b;
case 2:
return a*b;
case 3:
return a/b; // Div by zero?
}
}
Title: Subject: Read subject of title.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|