This will be original. I promise.
I cannot get the random number generator to work.
I tried seeding with Date.Now.Millis econds, it still results in the same values.
What I have are arrays of values.
I get a random index value for each array so I can pull the data from them.
Except the data is similar on every run.
I know its a timing issue because when I step through code, it gives me randoms.
However, is there anything faster than milliseconds that I can seed at?
Here's my code. Please let me know if you need me to explain anything. Basically, i'm creating a random math expression:
I cannot get the random number generator to work.
I tried seeding with Date.Now.Millis econds, it still results in the same values.
What I have are arrays of values.
I get a random index value for each array so I can pull the data from them.
Except the data is similar on every run.
I know its a timing issue because when I step through code, it gives me randoms.
However, is there anything faster than milliseconds that I can seed at?
Here's my code. Please let me know if you need me to explain anything. Basically, i'm creating a random math expression:
Code:
#Region "Random Number Generator does not work"
Private Function RandomExpressionCreateOperand() As String
'Array of constants
Dim szConstants() As String = {"pi", "e"}
'Array of functions
Dim szFunctions() As String = {"Acos", "Asin", "Atan", "Ceiling", "Cos", "Cosh", "Floor", "Log", "Sin", "Sqrt", "Tan"}
'Array of parameters (proprietary)
Dim szParameters() As String = {"Parameter1", "Param.eu"}
'Array of tokens (corresponding to Constant, Fucntion, Parameter or raw value, respectively)
Dim iTokens() As Integer = {1, 2, 3, 4}
'Seed random number generator
Dim rc As New Random(Date.Now.Millisecond)
'Random number related to token (Constant, Function, Parameter or Value)
Dim iToken As Integer = rc.Next(1, UBound(iTokens))
'Return string
Dim szRet As String = ""
'Index of value in the array
Dim iIndex As Integer = 0
'Raw value but also parameter of a function token
Dim iParam As Integer = 0
'Max value of parameter or raw value:
Dim ParamMax As Integer = 999999
Select Case iToken
Case 1 'constants
iIndex = rc.Next(1, UBound(szConstants))
Debug.Print(iIndex)
szRet = "{" & szConstants(iIndex) & "}"
Case 2 'functions
iIndex = rc.Next(1, UBound(szFunctions))
Debug.Print(iIndex)
szRet = szFunctions(iIndex) & "("
iParam = rc.Next(1, ParamMax)
Debug.Print(iParam)
szRet = szRet & iParam & ")"
Case 3 'parameters
iIndex = rc.Next(1, UBound(szParameters))
Debug.Print(iIndex)
szRet = "[" & szParameters(iIndex) & "]"
Case 4 'values
szRet = rc.Next(1, ParamMax)
Debug.Print(szRet)
End Select
Return szRet
End Function
Private Sub RandomExpressionCreate(ByVal DoNotIncludeBadData As Boolean)
'Random object
Dim rc As New Random(Date.Now.Millisecond)
'array of operators
Dim szOperators() As String = {"*", "+", "/", "-", "^"}
'random number for up to 5 operations
Dim iOperatorCount As Integer = rc.Next(1, 5)
'String parts of random math expression
Dim szOp1 As String
Dim szOp2 As String
Dim szOP As String
Dim sbExp As New StringBuilder
Dim iOp As Integer
For x As Integer = 1 To iOperatorCount
'Operand 1:
szOp1 = RandomExpressionCreateOperand()
'Operator:
iOp = rc.Next(1, UBound(szOperators))
szOP = szOperators(iOp)
'Operand 2:
szOp2 = RandomExpressionCreateOperand()
'Write expression
sbExp.Append(szOp1 & " " & szOP & " " & szOp2)
'Add another operator as long as were not at the end
If x <> iOperatorCount Then
iOp = rc.Next(1, UBound(szOperators))
sbExp.Append(" " & szOperators(iOp) & " ")
End If
Next x
'Me.txtExpression.Text = sbExp.ToString
Debug.Print(sbExp.ToString)
End Sub
#End Region
Comment