Successive random number generation problem

# Thread: Successive random number generation problem

1. Junior Member
Join Date
Dec 1969
Posts
2

## Successive random number generation problem

The problem I am having is this. I am trying to create a subroutine that calls a function. The function it calls is called GetRandom and as you can guess, it gives me a random number seeded off the clock. Everything works fine with that. When I submit and the subroutine is run, the function works just fine and gives me a random number. The problem is that when I loop inside the subroutine and it calls the function each time it loops it gives me the same number over and over again. So.. if I call GetRandom(1, 10) 40 times, it gives me 40 1&#039;s or 40 6&#039;s. There is no variation each time I call the function.<BR><BR>Every page I have visited on the internet about random numbers tells me how to get a random number. Trust me, that part is working in the code. What I don&#039;t understand is why every time I call the GetRandom function it returns the same number. I thought that when a function executes it creates variables insde the function that live until the function returns a value. At that point the variables created during the execution of the function run "out of scope". However, this doesnt seem to be the way it works for asp.net using vb.net.<BR><BR>What am I doing wrong?<BR><BR>Dave

2. Senior Member
Join Date
Dec 1969
Posts
2,885

## RE: Successive random number generation problem

Post relevant code please.

3. Junior Member
Join Date
Dec 1969
Posts
2

## RE: Successive random number generation problem

This code is not completed obviously. Please ignore the part about armor, the only loop that im trying to focus on or test is the one involving inf.<BR><BR>Sub Attack(inf as Integer, armor as Integer)<BR> Dim cat as Integer<BR> Dim atnum as Integer<BR> <BR> For cat = 1 To inf<BR> atnum = GetRandom(1, 10)<BR> If atnum &#060;= 3 Then<BR> AtHits = (AtHits + 1)<BR> End If<BR> &#039;inf = (inf - 1)<BR> Next Cat<BR> <BR> For cat = 1 To armor<BR> If GetRandom(1, 10) &#060;= 5 Then<BR> AtHits = (AtHits + 1)<BR> End If<BR> armor = (armor - 1)<BR> Next cat<BR>End Sub<BR><BR>Private Function GetRandom(intLowerBound As Integer, intUpperBound As Integer) as integer<BR> Dim RandomGenerator As Random<BR> <BR> Dim intRandomNumber As Integer<BR> <BR> RandomGenerator = New Random(DateTime.Now.Millisecond)<BR> intRandomNumber = RandomGenerator.Next(intLowerBound, intUpperBound + 1)<BR> GetRandom = intRandomNumber<BR> RandomGenerator = Nothing<BR>End Function<BR>

4. Senior Member
Join Date
Dec 1969
Posts
2,885

## RE: Successive random number generation problem

I have used your code and seen what you described. I do not know why it is behaving as such. Question - Why the getRandom function? It seems to just wrap Random.Next. Couldn&#039;t you just:<BR><BR> Sub Attack(ByVal inf As Integer, ByVal armor As Integer)<BR> Dim i As Integer = 0<BR> Dim myRand As New System.Random(DateTime.Now.Millisecond)<BR> For i = 0 To 9<BR> Response.Write(myRand.Next(inf, armor).ToString() + "&#060;br/&#062;")<BR> Next<BR> End Sub<BR>

5. Senior Member
Join Date
Dec 1969
Posts
4,697

## RE: Normal behavior :: Successive random number ge

Public Sub New( _<BR> ByVal Seed As Integer _<BR>)<BR><BR>if your application runs on a fast computer the system clock might not have time to change between invocations of this constructor<BR><BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemrandomclassctortopic2.asp

6. Senior Member
Join Date
Dec 1969
Posts
2,885

## RE: Normal behavior :: Successive random number ge

Ahhhh! Got&#039;cha ;)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•