0

I made a randomization function for my regen and it just makes all the things that respawn in a diagonal line across the form... the code is this:

Public Function RandomNumber(ByVal MaxNumber As Integer, _
Optional ByVal MinNumber As Integer = 0) As Integer

    'initialize random number generator
    Dim r As New Random(System.DateTime.Now.Millisecond)

    'if passed incorrect arguments, swap them
    'can also throw exception,return 0

    If MinNumber > MaxNumber Then
        Dim t As Integer = MinNumber
        MinNumber = MaxNumber
        MaxNumber = t
    End If

    Return r.Next(MinNumber, MaxNumber)

End Function

and the code for 1 regen is this:

'regen coins
        z = coin1

    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.Location = zloc
    z.Show()

I do not know why it just makes a diagonal line but help would be VERY appreciated!

2 Answers2

0

Your question is a bit vague, but I'm going to make a suggestion. Initialize your random number generator outside the RandomNumber function. Otherwise you create a new instance every time you make a call:

'initialize random number generator outside the function 
Public _r As New Random(System.DateTime.Now.Millisecond)

Public Function RandomNumber(ByVal MaxNumber As Integer, Optional ByVal MinNumber As Integer = 0) As Integer
    ' ensure min is less than max
    If MinNumber > MaxNumber Then
        Return _r.Next(MaxNumber, MinNumber)
    Else
        Return _r.Next(MinNumber, MaxNumber)
    End If
End Function
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
  • thank you..but now the things spawn in objects how do i stop that? – user2209912 Mar 26 '13 at 03:50
  • I'd suggest changing If zloc.Y > 595 Then to a loop rather than a single test. `Do While zloc.Y > 595 : zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height)) : Loop ` – Derek Tomes Mar 26 '13 at 03:56
  • would this work to stop it from being inside picturebox1, picturebox2, and picturebox3? z = coin1 z.Location = zloc z.Hide() zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height)) Do While z.Bounds.IntersectsWith(PictureBox1.Bounds, PictureBox2.Bounds, PictureBox3.Bounds) zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height)) Loop z.Location = zloc z.Show() – user2209912 Mar 26 '13 at 04:23
  • Rather than appending your next question to this one, I think you need to ask a new question. – Derek Tomes Mar 26 '13 at 22:46
0

Your Random object, r, has function-level scope, which means that you re-initialize it upon each entry to the RandomNumber function.

A lot of people make this mistake and get the same "random" value each time*. You do get a slightly different number each time because, instead of using the default constructor, you're re-initializing it with a semi-random seed (the current millisecond count from the clock). Either way, the call to r.Next() is still not doing what you want it to do.

To fix this, ensuring that it's the same Random object that is used on each call to the function, hoist the declaration of r up a scope level (e.g., to the containing class), or mark it Static at function-level.

That will probably help enough to make you happy. If not, you're in for more education than you were probably hoping. The topic of randomness is a big philosophical one. There is no better place to start than the Wikipedia article on the subject, or perhaps this famous Stack Overflow question.

* Which is, of course, entirely allowed within the definition of "random". It's just not what people are wanting.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574