Getting random, unique numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TTCEric
    New Member
    • Jul 2008
    • 26

    Getting random, unique numbers

    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:

    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
  • TTCEric
    New Member
    • Jul 2008
    • 26

    #2
    Getting random, unique numbers

    I posted this before but did not get a reply so i'm gonna try it again without diluting with detail.

    Real simply put: random numbers are not random even though I seed with time in milliseconds.

    Is there some other method to seed or some other concrete randomizer logic that works?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Yep, programs usually only generate pseudo random numbers. If you want real randomness then you'd have to observe some naturally random activities (like movement of atoms). In practice though pseudo random numbers suffice.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        If you feel that your original question has been overlooked, please just reply to the original thread. That will bump it to the top of the list again. Please do not make a new post. Please read the Posting Guidelines.

        I have merged your threads.

        MODERATOR

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          How fast you request "random" numbers also plays a part.
          I noticed that if I requested them too fast I would get duplicates.
          And I believe if you do NOT provide a seed, it defaults to using the time anyway.

          Comment

          • TTCEric
            New Member
            • Jul 2008
            • 26

            #6
            Originally posted by Plater
            How fast you request "random" numbers also plays a part.
            I noticed that if I requested them too fast I would get duplicates.
            And I believe if you do NOT provide a seed, it defaults to using the time anyway.
            Yes. I observed this as well. Stepping through the code, random worked. It was being seeded in milliseconds.

            What can I use instead of milliseconds? Evidently, my routines are happening too quickly- before the next millisecond.

            My temporary solution will be to store used random numbers but only in such a way as not to duplicate more than a few times. I hope this will delay the operation a bit.

            If theres a truer way to handle randoms, please let me know.
            I dont know what the other poster mean by using Atomic things. Sorry but thanks for the responses, community.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Use a Thread.Sleep() with a one milisecond wait time?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by TTCEric
                If theres a truer way to handle randoms, please let me know.
                I dont know what the other poster mean by using Atomic things. Sorry but thanks for the responses, community.
                <That was just the .NET village idiot being himself>
                It was just an example of how one could get true randomness. The intent was to show that true randomness is very difficult to get in programs and so pseudo random numbers are an acceptable substitute.
                </That was just the .NET village idiot being himself>

                Comment

                • TTCEric
                  New Member
                  • Jul 2008
                  • 26

                  #9
                  Originally posted by Plater
                  Use a Thread.Sleep() with a one milisecond wait time?
                  Good suggestion. I'll give it a try. Appreciate it

                  Comment

                  • IanWright
                    New Member
                    • Jan 2008
                    • 179

                    #10
                    Originally posted by TTCEric
                    Yes. I observed this as well. Stepping through the code, random worked. It was being seeded in milliseconds.

                    What can I use instead of milliseconds? Evidently, my routines are happening too quickly- before the next millisecond.

                    My temporary solution will be to store used random numbers but only in such a way as not to duplicate more than a few times. I hope this will delay the operation a bit.

                    If theres a truer way to handle randoms, please let me know.
                    I dont know what the other poster mean by using Atomic things. Sorry but thanks for the responses, community.
                    Can you not try the DateTime.Ticks property instead? This has a greater resolution that miliseconds...

                    Comment

                    • TTCEric
                      New Member
                      • Jul 2008
                      • 26

                      #11
                      Originally posted by IanWright
                      Can you not try the DateTime.Ticks property instead? This has a greater resolution that miliseconds...
                      I like that Ticks uses Nanoseconds (just read about it). I'll see if this works. Thanks for the suggestion.

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Originally posted by TTCEric
                        I like that Ticks uses Nanoseconds (just read about it). I'll see if this works. Thanks for the suggestion.
                        the Ticks count is the default seed if none is specified I believe

                        Comment

                        Working...