Thread: How to execute a command every specific,randomly time?

  1. #1
    DeSeis
    Guest

    How to execute a command every specific,randomly time?

    Lets say I want within my program to execute a statement/function every couple of random times?
    like executing some function between 3-5 seconds elpased within runtime of the program (with cycling too, not only once).
    Im using borland turbo C++ in a graphics.h (bgi) project and need to put a BLOCK of rectangles every couple of seconds to block the moving "pacman" . Thats the last thing remained for me and I donno how to do it. I can make the Block appear and go away by the putimage(.........,COPY_PUT) and then putimage(.....,XOR_PUT).
    What I do not know is how I can execute them randomly or constantly every couple of secs.
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    You just need to set up an interrupt to go off every second, and every time the interrupt goes off, you handle the random stuff.

    Setting up an interrupt's obviously the difficult part, but it does give you a word to look for in your compiler's documentation, since it isn't a standard function.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    you could also do it this way

    a simpler way to do it (but possibly harder to code for some people) would be to have a loop like this

    PHP Code:
    while(gameon == true)
    {
           if(
    rand()%10 == 3//this will occur about 1/10th of the time
           
    {
                  
    do_rect_stuff();  //stuff to be done rarely
           
    }
           
    do_other_stuff(); //stuff to be done each turn

    of course if you wanted a different percent you could change the %10 after rand to give you better (or worse) odds

    hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM