CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Random mathmatical operator

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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2008
    Posts
    2

    Re: Random mathmatical operator

    Quote Originally Posted by laserlight View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Random mathmatical operator

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  6. #6
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    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.

  7. #7
    Join Date
    Oct 2006
    Posts
    616

    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

  8. #8
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Random mathmatical operator

    Quote Originally Posted by jordieg03 View Post
    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.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Random mathmatical operator

    Quote Originally Posted by Zachm View Post
    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

  10. #10
    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".

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Random mathmatical operator

    Quote 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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    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?
    	}
    }
    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
  •  





Click Here to Expand Forum to Full Width

Featured