4 Replies - 3132 Views - Last Post: 23 April 2010 - 01:29 PM Rate Topic: -----

#1 reimer_31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-April 10

Random number generator within sub routine

Posted 22 April 2010 - 12:08 PM

I want t to equal a new random number everytime but it only gives a random number the first time and then uses that same number everytime.

Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        notreduced(Label17, Label14)
        notreduced(Label21, Label22)
        notreduced(Label23, Label24)
        notreduced(Label25, Label26)
    End Sub


Sub notreduced(ByRef a As Label, ByRef b As Label)
        lowestFraction(a, B)/>
        Dim ran As New Random()
        Dim t As Integer
        t = ran.Next(2, 9)
        a.Text = a.Text * t
        b.Text = b.Text * t
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Random number generator within sub routine

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Random number generator within sub routine

Posted 22 April 2010 - 12:18 PM

that is because random is not very random here if you use delay it will give you random numbers.
    Sub notreduced(ByRef a As Label, ByRef b As Label)
        lowestFraction(a, B)/>
        Dim ran As New Random()
        Dim t As Integer
        t = ran.Next(2, 9)
        'here sleep for 1 second'
        System.Threading.Thread.Sleep("1000")
        MessageBox.Show(t.ToString)
        a.Text = a.Text * t
        b.Text = b.Text * t
    End Sub


This post has been edited by NoBrain: 22 April 2010 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 code_junkie83  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 62
  • Joined: 17-June 09

Re: Random number generator within sub routine

Posted 22 April 2010 - 12:20 PM

Try this instead:

Dim ran as New Random(System.DateTime.Now.Millisecond)


I believe that will act as a reseed for the random number generator.

EDIT: Or what NoBrain said :)

This post has been edited by code_junkie83: 22 April 2010 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 reimer_31  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-April 10

Re: Random number generator within sub routine

Posted 23 April 2010 - 12:45 PM

Thank you nobrain and code_junkie
Both ways worked
Was This Post Helpful? 0
  • +
  • -

#5 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Random number generator within sub routine

Posted 23 April 2010 - 01:29 PM

i suggest you use code_junkie83 code it is the right way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1