By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

Getting random, unique numbers

TTCEric
P: 26
This will be original. I promise.

I cannot get the random number generator to work.

I tried seeding with Date.Now.Milliseconds, 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:

Expand|Select|Wrap|Line Numbers
  1. #Region "Random Number Generator does not work"
  2.     Private Function RandomExpressionCreateOperand() As String
  3.         'Array of constants 
  4.         Dim szConstants() As String = {"pi", "e"}
  5.         'Array of functions 
  6.         Dim szFunctions() As String = {"Acos", "Asin", "Atan", "Ceiling", "Cos", "Cosh", "Floor", "Log", "Sin", "Sqrt", "Tan"}
  7.         'Array of parameters (proprietary)
  8.         Dim szParameters() As String = {"Parameter1", "Param.eu"}
  9.         'Array of tokens (corresponding to Constant, Fucntion, Parameter or raw value, respectively)
  10.         Dim iTokens() As Integer = {1, 2, 3, 4}
  11.         'Seed random number generator
  12.         Dim rc As New Random(Date.Now.Millisecond)
  13.         'Random number related to token (Constant, Function, Parameter or Value)
  14.         Dim iToken As Integer = rc.Next(1, UBound(iTokens))
  15.         'Return string 
  16.         Dim szRet As String = ""
  17.         'Index of value in the array
  18.         Dim iIndex As Integer = 0
  19.         'Raw value but also parameter of a function token
  20.         Dim iParam As Integer = 0
  21.         'Max value of parameter or raw value: 
  22.         Dim ParamMax As Integer = 999999
  23.         Select Case iToken
  24.             Case 1 'constants
  25.                 iIndex = rc.Next(1, UBound(szConstants))
  26.                 Debug.Print(iIndex)
  27.                 szRet = "{" & szConstants(iIndex) & "}"
  28.             Case 2 'functions 
  29.                 iIndex = rc.Next(1, UBound(szFunctions))
  30.                 Debug.Print(iIndex)
  31.                 szRet = szFunctions(iIndex) & "("
  32.                 iParam = rc.Next(1, ParamMax)
  33.                 Debug.Print(iParam)
  34.                 szRet = szRet & iParam & ")"
  35.             Case 3 'parameters
  36.                 iIndex = rc.Next(1, UBound(szParameters))
  37.                 Debug.Print(iIndex)
  38.                 szRet = "[" & szParameters(iIndex) & "]"
  39.             Case 4 'values
  40.                 szRet = rc.Next(1, ParamMax)
  41.                 Debug.Print(szRet)
  42.         End Select
  43.         Return szRet
  44.     End Function
  45.     Private Sub RandomExpressionCreate(ByVal DoNotIncludeBadData As Boolean)
  46.         'Random object
  47.         Dim rc As New Random(Date.Now.Millisecond)
  48.  
  49.         'array of operators
  50.         Dim szOperators() As String = {"*", "+", "/", "-", "^"}
  51.  
  52.         'random number for up to 5 operations
  53.         Dim iOperatorCount As Integer = rc.Next(1, 5)
  54.  
  55.         'String parts of random math expression
  56.         Dim szOp1 As String
  57.         Dim szOp2 As String
  58.         Dim szOP As String
  59.         Dim sbExp As New StringBuilder
  60.         Dim iOp As Integer
  61.  
  62.         For x As Integer = 1 To iOperatorCount
  63.             'Operand 1: 
  64.             szOp1 = RandomExpressionCreateOperand()
  65.  
  66.             'Operator:  
  67.             iOp = rc.Next(1, UBound(szOperators))
  68.             szOP = szOperators(iOp)
  69.  
  70.             'Operand 2: 
  71.             szOp2 = RandomExpressionCreateOperand()
  72.  
  73.             'Write expression 
  74.             sbExp.Append(szOp1 & " " & szOP & " " & szOp2)
  75.  
  76.             'Add another operator as long as were not at the end
  77.             If x <> iOperatorCount Then
  78.                 iOp = rc.Next(1, UBound(szOperators))
  79.                 sbExp.Append(" " & szOperators(iOp) & " ")
  80.             End If
  81.         Next x
  82.  
  83.         'Me.txtExpression.Text = sbExp.ToString
  84.         Debug.Print(sbExp.ToString)
  85.     End Sub
  86. #End Region
  87.  
Jul 25 '08 #1
Share this Question
Share on Google+
11 Replies


TTCEric
P: 26
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?
Jul 28 '08 #2

10K+
P: 13,264
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.
Jul 28 '08 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
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
Jul 28 '08 #4

Plater
Expert 5K+
P: 7,872
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.
Jul 28 '08 #5

TTCEric
P: 26
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.
Jul 28 '08 #6

Plater
Expert 5K+
P: 7,872
Use a Thread.Sleep() with a one milisecond wait time?
Jul 28 '08 #7

10K+
P: 13,264
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>
Jul 29 '08 #8

TTCEric
P: 26
Use a Thread.Sleep() with a one milisecond wait time?
Good suggestion. I'll give it a try. Appreciate it
Jul 29 '08 #9

100+
P: 179
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...
Jul 29 '08 #10

TTCEric
P: 26
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.
Jul 29 '08 #11

Plater
Expert 5K+
P: 7,872
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
Jul 29 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.