Random Numbers are NOT random !!!

  • Thread starter Thread starter mark4asp
  • Start date Start date
M

mark4asp

The result of RandList() is a list of numbers which are all the SAME.

Changing the d() function to the one which is commented out has no
effect (of course).

How can I get the function [ d() ] to return a different random number
each time?

Function RandList()
Dim str As String = ""
Dim i As Integer
For i = 1 To 100
str += d(10).toString() & ", "
Next
Return str
End Function

Function d(ByVal high) As Integer
Return New System.Random().Next(1, high+1)
End Function

' Function d(num As Integer)
' Dim RanNum As New Random
' Return RanNum.Next(1, num+1)
' End Function
 
Hi,

Randomize
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmrandomize.asp


Ken
----------------------
The result of RandList() is a list of numbers which are all the SAME.

Changing the d() function to the one which is commented out has no
effect (of course).

How can I get the function [ d() ] to return a different random number
each time?

Function RandList()
Dim str As String = ""
Dim i As Integer
For i = 1 To 100
str += d(10).toString() & ", "
Next
Return str
End Function

Function d(ByVal high) As Integer
Return New System.Random().Next(1, high+1)
End Function

' Function d(num As Integer)
' Dim RanNum As New Random
' Return RanNum.Next(1, num+1)
' End Function
 
mark4asp said:
How can I get the function [ d() ] to return a different random number
each time?
Function d(ByVal high) As Integer
Return New System.Random().Next(1, high+1)
End Function


That random generator is just that, a generator. You need to
fire it up and pull out the random numbers. What you are doing
is starting up a whole lot of new generators. Since the generators
use the current time as their seed (when you provide no seed yourself)
and you are calling them all in a blink of an eye, they are all created
using the same seed.

What you need to do is to declare your generator at the module
level:

Private RandomGenerator As New Random

And use your routine to pull out the random numbers:
Function d(ByVal high) As Integer
Return RandomGenerator.Next(1, high+1)
End Function

See if that helps

LFS
 
You must call Randomize first in order to get a random seed value.

Rnd will always return the same numbers every time you run it unless you
provide it a different seed to start with each time. That is what Randomize
does. Just place it as the first line in your method before Rnd().

You can use Randomize(numbero of your choosing), or you can simply call
Randomize and it will pull the seed value from the system timer.

Have fun!
 
random numbers are never random, they just follow a pattern created by a
sead number.
 
Gerry O'Brien said:
You must call Randomize first in order to get a random seed value.

Rnd will always return the same numbers every time you run it unless you
provide it a different seed to start with

Correct, but he wasn't using the Rnd function:

He was using the Random data type, which uses a time-dependant
seed value on creation if no seed value is provided.

IOW, Randomize is not necessary when using this data type.

LFS
 
OOPS!

I read into what I wanted. My Bad!!

Sorry.

--
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)
 
mark4asp,
In addition to Larry's comments (about making Random module level) you can
simply make the Random variable Static to the procedure.

Something like:
Private Function RandList() As String
Dim sb As New System.Text.StringBuilder
Dim i As Integer
For i = 1 To 100
sb.Append(d(10))
sb.Append(", ")
Next
sb.Length -= 2
Return sb.ToString()
End Function

Private Function d(ByVal high As Integer) As Integer
Static numberGenerator As New System.Random
Return numberGenerator.Next(1, high + 1)
End Function

Making numberGenerator Static, is the same as making it module level,
however only d can use the numberGenerator variable.

Hope this helps
Jay
 
I'm talking about the generic functionality included in 3GL and 4GL
languages, not specialized functions like that
 
Brian Henry said:
I'm talking about the generic functionality included in 3GL and 4GL
languages, not specialized functions like that

But even 3GL and 4GL languages can approximate randomness by
measuring time differences between 'random' events. When the
unit of time measured is (very) small, then there is plenty of room
for a wide variance in seemingly regular events.

For example:

Private Declare Function QueryPerformanceCounter Lib "kernel32" ( _
lpPerformanceCount As LARGE_INTEGER) As Long

Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Private Sub Form_Load()
Dim itg1 As LARGE_INTEGER
Dim itg2 As LARGE_INTEGER

QueryPerformanceCounter itg1

' A random event
DoEvents

QueryPerformanceCounter itg2
Debug.Print Left(CStr(itg2.lowpart - itg1.lowpart), 4) - 1000
Unload Me

End Sub


You can see that repeated attempts do not produce the same
values. A program might also factor in events that hapen on a
larger scale, such as human input (mouse moves and key input)
which would yield an even wider variance.

While the above may not be statistically random, you can find
events that may be, like the time it takes to find a file after a
directory read. The seek time may be dependant on the speed
of the disk and the exact moment the request was made. Delaying
that call by some 'random' amount (like the routine above) may be
sufficiently long enough to create a few digits of randomness.

Close enough for Windows, anyway!

Good luck!
LFS


LFS
 
Larry Serflaten said:
For example:

Private Declare Function QueryPerformanceCounter Lib "kernel32" ( _
lpPerformanceCount As LARGE_INTEGER) As Long

Private Type LARGE_INTEGER


Bummer, I forgot where I was again....

The theory may be helpful even if the code is old school! <g>


Sorry!
LFS
 
Back
Top