[RESOLVED] Random number generator stopped working
Hi, I'm writing a simple game in VB6 which selects one of three balls at random to drop towards a paddle underneath it. The user then presses a button to activate the correct paddle and catch the ball. This worked fine for a while and the balls dropped randomly but then it stopped working. This is the code I was using for the random number generator:
Code:
Private Function RandomNumber() As Integer
'generate a random number between 0 and 2
Randomize
randNum = Int(Rnd * 3)
'End Function
This is the only place the random number is used:
Code:
Dim RandNum As Integer
If dropping = False Then 'if no ball is dropping
RandomNumber 'generate a random number
dropping_ball = randNum
dropping = True 'drop a ball
End If
I tried outputing RandNum in a textbox and saw that it is always at zero. I also tried giving it a different value when it was initialised but it always reset to zero. This makes me think that the generator is working but it could just be picking the same number each time. I have no idea why it's doing this if so because this code is exactly the same as before when it was working. As it was working, I had no need to change it so I didn't.
Does anyone have any idea how to fix this?
Thanks,
Mooncinder
Re: Random number generator stopped working
Don't put Randomize in the RandomNumber function. Put Randomize at the load event of your application instead.
Re: Random number generator stopped working
Did you make sure that randNum is declared in the General and thus not a local variable?
Looks like it's not getting passed back from the sub. Try the same routine as a GoSub and I bet it works. ;)
Re: Random number generator stopped working
change:
Code:
Private Function RandomNumber() As Integer
RandomNumber = Int(Rnd * 3)
End Function
and
Code:
If dropping = False Then 'if no ball is dropping
'generate a random number
dropping_ball = RandomNumber
dropping = True 'drop a ball
End If
and put Randomize in Form_Load as already mentioned
Re: Random number generator stopped working
Quote:
Originally Posted by Mooncinder
...This worked fine for a while and the balls dropped randomly but then it stopped working.
Your code is working for me...
(Edit: even without Bushmobile's improvements)
Maybe I'll have to wait longer to see it pick zeros. How long / how many function calls is "for a while"?
Re: Random number generator stopped working
Thanks for replying, everyone. :)
I tried putting Randomize in form_load and it didn't make any difference. I also tried doing what bushmobile suggested but that didn't work either. I'm afraid I'm not familiar with GoSub but I'll look into that.
I'm not sure how long it worked for. It didn't stop working while the program was running. It was only after I'd been working on another part of the code then ran it again although I don't see how changing a completely unrelated piece of code that didn't use the random number could have affected it.
Re: Random number generator stopped working
The reason I mentioned GoSub is because then the variable can remain local. Actually your routine is so short that in-line programming would work also rather than use a function, unless you plan to call this in other controls.
Here's a routine I use for scrambling array values that never seems to fail:
Code:
ReDim Item(ArraySize)
Randomize
For I = 0 To ArraySize
Nx = Int(Rnd * ArraySize)
NumSlave = Item(Nx) : Item(Nx) = Item(I) : Item(I) = NumSlave
Next
Re: Random number generator stopped working
Well, I tried forgetting the function and just putting the random number code where I would normally call the function and it went really strange, with the number changing all over the place and not always dropping a ball. However, when I looked into this I realised I'd missed out a line of code in the code for the second and third ball and once I'd put this in, it worked!
Oh well, it's always something simple. Thanks for all your help. :D