Problems Generating Random Numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a quick Question and I Hope some one can help or at least explain.

What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I run this code the random numbers that are displayed are all the same for abot 25 cycles and then randomize as normal.

Can some one tell me why this is or at least tell me what I am doing wrong.

Regards Glenn

Module Module1

Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine()
End Sub

Public Function RollPercent() As Integer
Dim tmpRandom As New Random(Environment.TickCount)
Return tmpRandom.Next(1, 101)
End Function

End Module
 
Environment.TickCount returns milliseconds, which aren't granular enough for the way you're going about this. So essentially you are creating multiple random numbers with the same seed, which of course means that they're not random at all.

You should change your tmpRandom to a private member of the class and initialize it only once. Then call tmpRandom.Next as you are doing now.
 
I beleive it has to do with the variable scope, if you create the variable
inside the function and initiate the value to environment.tick the SEED will
remain the same until the next tick, same seed causes same "random" number.
Try the same example with a module level random like this:

Module Module1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine
End Sub

Public Function RollPercent() As Integer
'Dim tmpRandom As New Random(Now.Millisecond)
Return tmpRandom.Next(1, 101)
End Function
End Module
 
Thanks will give it a go

Jared said:
I beleive it has to do with the variable scope, if you create the variable
inside the function and initiate the value to environment.tick the SEED will
remain the same until the next tick, same seed causes same "random" number.
Try the same example with a module level random like this:

Module Module1

Dim tmprandom As New System.Random(Now.Millisecond)
Sub Main()
Dim rLoop As Integer
For rLoop = 1 To 100
Console.WriteLine("Return Value {0} - {1}", rLoop, RollPercent)
Next
Console.ReadLine
End Sub

Public Function RollPercent() As Integer
'Dim tmpRandom As New Random(Now.Millisecond)
Return tmpRandom.Next(1, 101)
End Function
End Module
 
It's because the function calls are happening so fast that they're occurring
between tick counts. That's why you're getting duplicate values.



Glenn Wilson said:
I have a quick Question and I Hope some one can help or at least explain.

What is happening is that I am trying to use random numbers in an
application, as per the sample test code below. When I run this code the
random numbers that are displayed are all the same for abot 25 cycles and
then randomize as normal.
 
yEaH rIgHt,
Try the example for yourself. I realize what you are saying and I too
beleive this, but, I think it has to do with the scope of the variable. If
you instantiate the random generator inside the function the tick count may
be very well be the same for may cycles, resulting in duplicate numbers.
That is way I said if you instantiate the variable at a different level the
SEED has already been assigned resulting in a "non-duplicating" random
number. If I'm way off, someone please correct me.
Jared
 
Declare the Random as static and only create it once.

Public Function RollPercent() As Integer
Static tmpRandom As Random
'Random uses time based seed by default so no need to specify.
If tmpRandom Is Nothing Then tmpRandom = New Random()
Return tmpRandom
End Function
 
I don't think I am explaining this right. I am trying to say that when Glenn
was creating the variable inside the function the seed value was the same,
causing duplicate numbers. This was all relitave to the scope of the
variable in THIS example. I am not saying the only way to do this is make a
module level variable. I need to quit trying to help people, I am no expert,
and, instead of building on a post people seem to try to "one-up" or
"out-do" another poster; there are many ways to do any single task. My way
is not better, I was only explaining why I believed the value was
duplicating at the request in the original post. Everyone else is just
giving examples of how to correct the problem, which is fine, but if Glen
doesn't know what he is doing "wrong" how can he prevent this in the future?


"Mick Doherty"
 
Of course it was the way he was seeding the Random class that was causing
the problem. I wasn't disputing that. Take a couple of deep breaths. It's
going to be o.k.
 
I was not disagreeing with you. I just posted an alternative solution. I did
not need to explain the cause of the problem since you had already done
that.

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi100/alternate/home.html


Jared said:
I don't think I am explaining this right. I am trying to say that when Glenn
was creating the variable inside the function the seed value was the same,
causing duplicate numbers. This was all relitave to the scope of the
variable in THIS example. I am not saying the only way to do this is make a
module level variable. I need to quit trying to help people, I am no expert,
and, instead of building on a post people seem to try to "one-up" or
"out-do" another poster; there are many ways to do any single task. My way
is not better, I was only explaining why I believed the value was
duplicating at the request in the original post. Everyone else is just
giving examples of how to correct the problem, which is fine, but if Glen
doesn't know what he is doing "wrong" how can he prevent this in the
future?
 
Back
Top