Results 1 to 17 of 17

Thread: How can you get a timer to execute randomly between 2-4 seconds?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    How can you get a timer to execute randomly between 2-4 seconds?

    is there any way to set a timer to execute randomly every 2-4 seconds (intervals every 10th of a second, ie 2.0, 2,1, 2,2 etc) instead of a static value?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    use a random statement, ie timeout = int(rnd*2000) + 2000 between 2000 and 4000 milliseconds, 2 - 4 seconds.

    make sure to use randomize timer to seed the number generator so that you don't get the same sequece each time.

    i also saw this code to wait an interval. you need to pass the value of timeout to the routine.

    Code:
    Public Sub WaitLoop(timeout As Long, Optional myvar As Boolean)
    
    Dim timenow As Long
    timenow = GetTickCount
    Do
        DoEvents
        If myvar = True Then
            Exit Do
        End If
    Loop While (GetTickCount - timenow) < timeout 
    End Sub

    http://www.vbforums.com/search.php?s...ost&sortorder=
    Last edited by dglienna; Aug 14th, 2004 at 01:35 AM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    is there any way to do it without using a loop that will execute billions of times and put the CPU usage to 100?

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Can you please clarify, as the following is contradictory!
    execute randomly every 2-4 seconds (intervals every 10th of a second



    Bruce.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I think he means the interval should be 2.0, 2.1, 2.2, ... 3.9, 4.0; rather than 2.425 etc.

    To create that interval, use a slightly modifed version of dglienna's code:
    timeout = (int(rnd*21)*100) + 2000
    (this will give values from 2000ms to 4000ms in steps of 100ms)


    One of these would do the job:

    a) set the interval to the random amount at the end of the Timer event. due to the way the timer works, this isn't going to be accurate (the interval is the maximum time until it will fire - it may fire almost immediately).

    b) use a version of what dglienna posted, but use Sleep in addition to DoEvents.

    c) set the timer to fire every 100ms (0.1s), then at the start of the timer event check if the interval time has been reached. eg:
    VB Code:
    1. '(in the timer event)
    2. Static Inverval_Time as Long
    3. If Inverval_Time > GetTickCount Then Exit Sub
    4.  
    5. '(your timer code here)
    6.  
    7. Inverval_Time = GetTickCount  + (int(rnd*21)*100) + 2000
    I haven't tested this, but it should do the job.

    d) use an API timer (I think using SetTimer).

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390
    out of curosity what is the point of the myvar in dglienna's code? - it looks pointless to me cause it will always abort b4 doing nething

  7. #7
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Errrr...
    You lot have just messed with my head
    VB Code:
    1. Private Sub Timer1_Timer()
    2.    Timer1.Interval = Clng(((Rnd * 2) + 2) * 1000)
    3. End Sub
    The timer fires randomly betweeb 2 and 4 seconds everytime.

    Wopka

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Not necesarily true, see point a) in my post above.

    Also, you code doesnt allow for x.x seconds.

  9. #9

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by si_the_geek
    Not necesarily true, see point a) in my post above.

    Also, you code doesnt allow for x.x seconds.
    the x.x seconds issue aside... .what's wrong with resetting the interval length inside the timer event?

    As an additional note. the first thing the event should do is disable the timer, then do what it needs to do, then reset the interval time, and lastly, re-enable the timer.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    The problem is that the timer control does always not fire at intervalms after the interval has been set (& the control enabled).

    It is possible that the event will fire much earlier than intervalms, I just ran a quick test and didn't get the problem, but I have seen it before (and there have been many threads on this forum about this issue).

    The event can also happen much later if the system is too busy to process it, and a timer does not tick quite as often if the application it is in doesn't have the focus.

  12. #12
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586
    Wow I never seen or had that problem with the timer.. its always been right on time for me. But if the original person felt this was an issue and wanted another way to do it without putting his program in a loop, you could set up a DLL with the loop in and just reference / call the DLL, of course you would need to raise an event when the time is reached and handle the event in the primary program. This would cause the loop to run in its own process and not limit the functionality of the primary program by getting bogged down in a loop.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    If you disregard that VB is single threaded and that a Timer event might not be raised because your app is doing some other lengthy calculation or something, this code should work:
    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Timer1.Enabled = False
    3.     Timer1.Interval = (Int(Rnd * 20) + 20) * 100
    4.     Timer1.Enabled = True
    5.     '...
    6.     'the rest of the code to execute in the Timer event
    7. End Sub
    Cheers,

  14. #14
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390
    the timer doesn;t actually miss getting fired when u;re app is busy - it just doesn't count up when it does - so whenever u;re app is executing code the timer pauses

  15. #15
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    You can skip the problem like this:

    - set timer interval to 500 ms or some other value smaller than you actually need, smaller gives better accuracy but in the other hand you increase processor usage...

    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Static WaitUntil As Long
    3.     If WaitUntil > GetTickCount Then Exit Sub
    4.     WaitUntil = GetTickCount + 2000 + Int(Rnd * 2000)
    5.     'do stuff here
    6. End Sub
    7.  
    8. 'in loops that take very long
    9. Do
    10.     'do some stuff here
    11.     Timer1_Timer
    12. Loop

    Yeah, bubblegum code, but does the work

  16. #16
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by i00
    the timer doesn;t actually miss getting fired when u;re app is busy - it just doesn't count up when it does - so whenever u;re app is executing code the timer pauses
    That is somewhat true, except that it isn't the timer that pauses. Whenever the Timer event should be fired Windows sends a WM_TIMER message to your applications message queue. Since VB isn't free threaded it can only handle one message at the time, or run one procedure at the time. If you would use a DoEvents call VB will pause the execution of the current procedure and take care of the messages currently in the cueue.

    On another note; After reading this thread I really can't understand why people started talking about this subject at all. VaxoP wanted to set a Timer to a random number between 2 and 4 seconds, so even it the execution of the timer event might be delayed a few milliseconds (even counting 100 milliseconds) wouldn't make any difference at all.

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    i coded the random time code for between 2 and 4 seconds.
    THEN i posted code that WAS AN EXAMPLE of how to use the timer. I did not expect anyone to actually *use* the code.
    Sorry for the extra variable!



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