user2255552
user2255552

Reputation: 33

True Random Number Generating

So basically, I have this function that is supposed to generate two random integers between a high and a low number, to make a point on the form. I know that Random can handle this, but Random has consistency, whereas I need the numbers to be completely random on the form.

For example, most of my generated points appear in a diagonal line. This is what I want to avoid. It should go all over the form between the high and low numbers.

Here is my current function:

Function GetNewLocation() As Point
    Randomize()
    Dim int1 As Integer = RandomNumber(6, 345)
    Randomize()
    Dim int2 As Integer = RandomNumber(35, 286)
    Return New Point(int1, int2)
End Function

Function RandomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
    Randomize()
    Return New Random().Next(low, high)
End Function

How can I get true random number generation where the point is not on a diagonal line?

Upvotes: 2

Views: 8632

Answers (5)

Nowrose Muhammad Ragib
Nowrose Muhammad Ragib

Reputation: 615

in java(assuming the limit is (limit-1)):

random  = System.currentTimeMillis()%limit;

You get the idea :)

Upvotes: 0

J.Kent
J.Kent

Reputation: 195

I literally just came up with this solution. Maybe it will help =)

I went from:

Dim rand As Random = New Random(DateTime.Now.Millisecond)

To

Dim rand As Random = New Random(DateTime.Now.Millisecond * DateTime.Now.Second * DateTime.Now.Minute * DateTime.Now.Hour)

And it seems to have worked really well. You can see the difference in the side by side.

Random Compare

Upvotes: 0

user5480760
user5480760

Reputation: 1

Thats also not the best slution. I prefer my own code:

 Dim old As Integer = 6572
Public Function Rand(ByVal min As Integer, ByVal max As Integer) As Integer
    Dim random As New Random(old + Date.Now.Millisecond)
    old = random.Next(min, max + CInt(IIf(Date.Now.Millisecond Mod 2 = 0, 1, 0)))
    Return old
End Function

Thats a little bit better

Upvotes: 0

Havenard
Havenard

Reputation: 27854

There is no true randomness in computer systems. There are many algorithm to generate "random" numbers but they are always based on a seed, like the time, process id, a mix of both, or on more advanced cases where randomness is taken seriously, on sounds being detected in a microphone, data from atmospheric sensors or cosmic microwave background, but it will always derivate from some existing source of information.

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117057

Each time you create a new instance of Random you are resetting the random number generator. Since the default constructor uses Environment.TickCount as the seed you are often returning precisely the same sequence of pseudo-random numbers. The system does not update TickCount that often. This is why it seems to you that you are getting non-random numbers.

Try changing your code like this:

Private _rnd As New Random()
Function RandomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
    Return _rnd.Next(low, high)
End Function

Upvotes: 5

Related Questions