Randomize Timer function not working 
Author Message
 Randomize Timer function not working

Hello.  I am writing a game in VB5.  Part of the game is rolling dice.
You play against the computer.  However, I have a bug.  When the
rolling takes place, the computer and the player always have either
two or three dice in common.

Why?

I'm running Win98, if that means anything.

Here is the code for rolling dice, the results of the dice are stored
in the caption of labels, not in a global variable:

    ' Randomize your dice
    For i = 1 To 5
        Randomize Timer
        die(i).Caption = Int((6 * Rnd) + 1)
    Next i

    Randomize Timer

    ' Randomize their dice
    For i = 1 To 5
        Randomize Timer
        compDie(i).Caption = Int((6 * Rnd) + 1)
    Next i



Sun, 18 Mar 2001 03:00:00 GMT  
 Randomize Timer function not working

Quote:

> Hello.  I am writing a game in VB5.  Part of the game is rolling dice.
> You play against the computer.  However, I have a bug.  When the
> rolling takes place, the computer and the player always have either
> two or three dice in common.

> Why?

> I'm running Win98, if that means anything.

> Here is the code for rolling dice, the results of the dice are stored
> in the caption of labels, not in a global variable:

>     ' Randomize your dice
>     For i = 1 To 5
>         'Randomize Timer
>         die(i).Caption = Int((6 * Rnd) + 1)
>     Next i

>     Randomize Timer

>     ' Randomize their dice
>     For i = 1 To 5
>         'Randomize Timer
>         compDie(i).Caption = Int((6 * Rnd) + 1)
>     Next i

Remove the Randomize Timer command from inside your loops.
In fact remove them all and place 1 in the form's initialize event.

LFS



Sun, 18 Mar 2001 03:00:00 GMT  
 Randomize Timer function not working



Quote:

> > Why?

> Remove the Randomize Timer command from inside your loops.
> In fact remove them all and place 1 in the form's initialize event.

Thought you might want some insight into what's happening.  The Randomize
Timer command uses the current system time to create a seed for the random
number generator (Rnd).  Every time that you call Rnd, it will create a new
random number.  You need to seed it only once so that the sequence of
"random" numbers that you get isn't the same each time that you run the
application.

In your case, it is quite possible that your code is running fast enough
that the Randomize Timer call is occurring multiple times in a single clock
tick.  This results in the random number generator being re-seeded with the
same original seed, thereby causing the next call to Rnd to produce the
same random number.

You need only seed the random number generator once.  I recommend that you
place your Randomize function inside the Form_Load event and forget about
it after that!

Jason White
Software Engineer
National Instruments

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                                       X
X Check out ComponentWorks, a collection of ActiveX controls from       X
X National Instruments for PC Measurement and Automation.  Information  X
X and free demo at http://www.natinst.com/cworks/                       X
X                                                                       X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



Sun, 18 Mar 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Randomize Timer function not working

2. RaiseEvent Does Not Work with MSComm But Works with Timer

3. Randomize Timer

4. Randomize Timer?

5. DBGrid Delete Key function not working on some DBGrids while it works on others

6. Timer property not working in VB.Net standard

7. multimedia timer does not work

8. Timer not working at all

9. Timer Not WOrking

10. Timer Function is not precise enough

11. Randomize and Rnd dont work?

12. Rnd() and Randomize() Functions

 

 
Powered by phpBB® Forum Software