Results 1 to 7 of 7

Thread: Random equation generator

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Random equation generator

    I want to make random equation generator such as this:
    96 - (-24) / (-24) * (-91) + 70 * 55 / (-4) * (-48) + (-10) * 61

    and I want to generate three equations in textbox multiline separated by newline and always select the second eq. Thats not problem, problem is how to make random equation generator. I am able to do that via generation a random long number, add random spaces between that, replace spaces with random symbols +-*/() (and power) but thats inefficient approach.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Random equation generator

    Quote Originally Posted by boops boops View Post
    That's not an equation, it's an expression.
    Indeed. An equation, by definition, equates things. If there's no equality operator then there's no equation.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Random equation generator

    Quote Originally Posted by VB.NET Developer View Post
    problem is how to make random equation generator
    Doing something random is .NET is always done pretty much the same way (unless for cryptographic purposes): use an instance of the Random class to generate a random number in an appropriate range and then use that number in whatever way is appropriate for your specific scenario. In your case, it looks like you might want ten numbers in the range -99 to 99 so that's exactly what you should create, i.e. call Random.Next ten times and specify that range. For the operators, you would put them in an appropriate list and then generate a random index into that list nine times. That's the random part done. The rest is simply formatting.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Random equation generator

    Here are some methods that you might find useful:
    vb.net Code:
    1. Private rng As New Random
    2.  
    3. Private Function GetNumberText() As String
    4.     Dim number As Integer
    5.  
    6.     Do
    7.         'Generate a number in the range -99 to 99 inclusive...
    8.         number = rng.Next(-99, 100)
    9.     Loop While number = 0 '...excluding 0.
    10.  
    11.     'Wrap negative numbers in parentheses.
    12.     Return If(number < 0, $"({number})", number.ToString())
    13. End Function
    14.  
    15. Private Function GetOperatorText() As String
    16.     Dim operators = "+-*/"
    17.  
    18.     Return operators(rng.Next(operators.Length))
    19. End Function
    Call the first one ten times and the second one nine times. I'll let you work out how to put the parts together. I can think of at least two ways to do that.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Random equation generator

    Take a look at this example:
    Code:
    Private _random = New Random()
    Private Function RandomOperand(min As Integer, max As Integer) As String
    	Dim includeParenthesis = DateTime.Now.Millisecond Mod 5 = 0
    	Dim operand = _random.Next(min, max + 1)
    
    	' string interpolation: Return $"{If(includeParenthesis, "(", String.Empty)}{operand}{If(includeParenthesis, ")", String.Empty)}
    	Return If(includeParenthesis, "(", String.Empty) & operand.ToString() & If(includeParenthesis, ")", String.Empty)
    End Function
    
    Private ReadOnly _operators As String = "/*-+"
    Private Function RandomOperator() As String
    	Return _operators(_random.Next(0, _operators.Length)).ToString()
    End Function
    
    Private Function RandomExpression(length As Integer) As String
    	If (length < 1) Then
    		Throw New ArgumentException("length cannot be less than 1")
    	End If
    	
    	' string interpolation: Dim leftHandExpression = $"{RandomOperand(1, 100)}{RandomOperator()}{RandomOperand(1, 100)}
    	Dim leftHandExpression = RandomOperand(1, 100) & RandomOperator() & RandomOperand(1, 100)
    	Dim index = 1
    	Do While index < length - 1
    		' string interpolation: leftHandExpression = $"{leftHandExpression}{RandomOperator()}{RandomOperand(1, 100)}
    		leftHandExpression = leftHandExpression & RandomOperator() & RandomOperand(1, 100)
    		index += 1
    	Loop
    
    	Return leftHandExpression
    End Function
    Fiddle: https://dotnetfiddle.net/YJk1Y0

    Note that I didn't include spaces in between the operands and operators, you can change that when concatenating the variables or by including them when returning the random operator.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Random equation generator

    One further observation is that your parentheses do nothing in that equation. It would evaluate exactly the same whether they were there or not.

    Many years back, I wrote an equation generator for a program that used a genetic algorithm to predict a pattern based on some set of variables. Basically, I was selecting a random set of variables from a pool of potential variables, building equations out of them, and seeing how well they predicted the pattern I was trying to match. Parentheses radically change the evaluation of equations, as long as they are meaningful, but they are also terribly problematic. If you are willing to ignore parentheses, or to make them meaningless, as you are showing, then your equations become MUCH easier to generate. If you want to evaluate them, you also have to make sure that you don't end up dividing by zero, but that's much easier without parentheses, too (you can avoid the case of N/(5-5)). What I did was made parentheses into transposons that could hop around the equation as a form of mutation. Such a mutation could radically change the meaning of the equation. For example, N/5-5 is valid, whereas N/(5-5) is a divide by 0 error. If you leave out parentheses, you....still have a problem, but you can avoid that by never allowing a 0.

    Considering the order of operations, I believe that it is impossible to write an expression that can't be evaluated if you don't include parentheses and don't allow numbers to be 0. Violate either of those, though, and you can have divide by 0 errors.
    My usual boring signature: Nothing

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