Thread: Need Help! Random in MFC

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stephencalderon View Post
    Greetings!
    I'm a beginner, still in school. I'm doing a small project in MFC. It's a basic animation of a ball bouncing off the four wall in a square. I don't exactly understand what this does randomly : srand((unsigned)time (NULL)). Also how would I use this to make the ball bounce randomly on the walls? Sorry if this is simple. Right now it bounce SE to SW to NW to NE. Once it hits the corners the ball then bounces across from corner to corner stuck that way.
    Thanks!
    You could use rand() as a means to vary the angle of rebount, making sure the ball doesn't find some static angle and resolve itself to bouncing continuously off the same points of the square.

    srand() is a seeder for the random number generator. It creates a new sequence of pseudo random numbers for the rand() function to use. It's weakness is that if you use the same number every time (as previously explained) you will get the same sequence every time.

    time() is the number of seconds since 00:00hrs Jan 1, 1970. Using it as a seed in srand pretty much guarantees you won't get the same sequence twice.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I would use a timer to do this.

    OnInit()
    Create a Compatible DC and bitmap for the background.
    Load or create the ball bitmap.
    Create a timer event (sends a OnTimer() message every time-period ie 1/sec)
    call srand(time)

    OnTimer()
    Calc new ball position
    FillRect() on background DC (to erase balls current position)
    Draw ball on background DC
    Call for a paint msg (OnPaint()) with

    InvalidateRect() //generate paint msg
    UpdateWindow() //Bypass OS msg queue and send the paint directly to the app

    OnPaint()
    BitBlt the background DC to the screen using the DC and CRect in the OnPaint() params

    OnClose()
    Clean up the background DC and ball image.
    Kill timer

    EDIT: rand() is a just a very big list of numbers.
    srand() sets the starting position within the list
    rand() gets the next number from the list (and moves the position one forward, so the next call gets the next number, etc).
    Last edited by novacain; 09-18-2010 at 09:16 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  2. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM
  3. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  4. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM