# Random number generator within sub routine

Page 1 of 1

## 4 Replies - 4194 Views - Last Post: 23 April 2010 - 01:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=169636&amp;s=79db8377d01f8b060fb38241a6f9ff87&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 reimer_31

Reputation: 0
• 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_

• Like a lollipop

Reputation: 469
• Posts: 4,280
• 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'
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

### #3 code_junkie83

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

## Re: Random number generator within sub routine

Posted 22 April 2010 - 12:20 PM

```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

### #4 reimer_31

Reputation: 0
• 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

### #5 raziel_

• Like a lollipop

Reputation: 469
• Posts: 4,280
• 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.