-
Nov 19th, 2020, 05:42 PM
#1
Thread Starter
Hyperactive Member
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.
-
Nov 19th, 2020, 06:01 PM
#2
Re: Random equation generator
That's not an equation, it's an expression.
-
Nov 19th, 2020, 06:52 PM
#3
Re: Random equation generator
Originally Posted by boops boops
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.
-
Nov 19th, 2020, 06:57 PM
#4
Re: Random equation generator
Originally Posted by VB.NET Developer
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.
Last edited by jmcilhinney; Nov 19th, 2020 at 07:11 PM.
-
Nov 19th, 2020, 07:07 PM
#5
Re: Random equation generator
Here are some methods that you might find useful:
vb.net Code:
Private rng As New Random
Private Function GetNumberText() As String
Dim number As Integer
Do
'Generate a number in the range -99 to 99 inclusive...
number = rng.Next(-99, 100)
Loop While number = 0 '...excluding 0.
'Wrap negative numbers in parentheses.
Return If(number < 0, $"({number})", number.ToString())
End Function
Private Function GetOperatorText() As String
Dim operators = "+-*/"
Return operators(rng.Next(operators.Length))
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.
-
Nov 19th, 2020, 07:22 PM
#6
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.
-
Nov 19th, 2020, 09:07 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|