Results 1 to 8 of 8

Thread: [RESOLVED] Random number generator stopped working

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Location
    Lincoln, UK
    Posts
    14

    Resolved [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

  2. #2
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Random number generator stopped working

    Don't put Randomize in the RandomNumber function. Put Randomize at the load event of your application instead.

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

  5. #5
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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"?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Location
    Lincoln, UK
    Posts
    14

    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.

  7. #7
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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
    Last edited by Code Doc; Mar 7th, 2007 at 12:53 PM.
    Doctor Ed

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2007
    Location
    Lincoln, UK
    Posts
    14

    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.

Posting Permissions

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



Click Here to Expand Forum to Full Width