Thread: Random generation of a picturebox control....

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Random generation of a picturebox control....

    I am not sure if this is in the correct form but if it’s not please let me know so that I can notify a mod.....

    I have a form with a picturebox control on it. At this point the picturebox is simply moving from left to right 10 pixels per second. I also have a menu with a New Game option on it. My goal is to when clicked the New Game menu will reset the picturebox to a new location. I would like for this new location to be randomly generated each time. So this is what I have......

    Code:
    	//This Event Handler handles the momvment of an object(ship)
    	//on the form
    	private: System::Void gameTimer_Tick(System::Object *  sender, System::EventArgs *  e)
    		{
    			int newx = ship->Location.X + 10; 
    			int newy = ship->Location.Y; 
    			Point newloc = Point(newx,newy); 
    			ship->Location = newloc; 
    		}
    
    	private: System::Void newGameMenu(System::Object *  sender, System::EventArgs *  e)
    		{
    			//?
    		}
    I am guessing I am going to have to build another function and have my newGameMenu Event Handler call that function. That I can do but what goes in the function is confusing me. Espically the random location aspect of it. Any advice would be great....

    FYI - This is being done in Visual C++ 2003

    Thanks

    Chad
    Last edited by chadsxe; 08-10-2005 at 01:52 PM.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    For the sake of simplicity and to get you started, I will say use the rand() function. You will need to initialize a seed with srand().

    rand() will return an integer in the range 0 to RAND_MAX. To narrow the range, you could mod it with your range for example if you want a random number from 0 to 9 (10 numbers) you could use rand()%10, some will tell you that there are problems with doing this because it will not have a uniform distribution, but again for simplicity's sake this works reasonbly well.

    srand(unsigned int) will seed the random number function rand(), if you want the same sequence of random numbers you can use the same seed. If you want a "random" seed you can use the time for a seed: srand(time(0)), which again, under certain circumanstances has problems but for general purposes works reasonbly well. It should only be called once, best somewhere near the beginning of your program.

    finally, if you are unsure of the range I would use ScreenWidth-PictureBoxWidth for X and ScreenHeight-PictureBoxHeight for Y
    Last edited by Darryl; 08-10-2005 at 02:30 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Wow ummm I am not going to lie, I did not understand most of your post.

    Quote Originally Posted by Darryl
    For the sake of simplicity and to get you started, I will say use the rand() function. You will need to initialize a seed with srand().
    How do you call rand() & srand() functions and what class are they apart of? What do you mean by "seed"?

    If its not asking two much can you put this in the most simplistic terms possible or better yet lead me to an example.

    Thanks

    Chad

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Well I did a little research and I found out that the rand() function is in the stdib.h, also that....

    "The srand function sets the starting value, seed, used by the pseudo-random number generator in the rand function. The random number generator produces the same sequence of pseudo-random numbers for any given value of seed. "

    I also read that this is a very basic random number generator and that there much better ones out there. Which is fine for what I want to accomplish but it makes me aware of something else. I should figure out and learn how to reset my "ship" to its orginal place before I try to "random generate" it.

    With that said how should I go about doing this. This may sound stupid but can you call an Event Handler from a function? The only way I can see how to reset the ship is by building a function with the same body gameTimer_Tick Event handler that I have posted above. Then call that function from the newGameMenu Event Handler.

    Any thoughts...

    Thanks

    CHad

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I would say in your class ship, you create a const Point named Origin as a member
    Code:
    class ship
    {
    ...
    const Point origin;
    
    }
    ship::ship():origin(Point(0,0))
    {
         //constructor
    }
    ...
    private: System::Void newGameMenu(System::Object *  sender, System::EventArgs *  e)
    		{
    			ship->Location = ship->origin;
                            
    
    		}
    Last edited by Darryl; 08-11-2005 at 09:56 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    You know it is amazing how much time I spend looking things over and trying to figure them out mean while the answer already in front of my face.

    So anyways I figure out how to reset the "ship" to its orginal location and have it continue to move across the screen from left to right. This is what I did
    Code:
    private: System::Void newGame_Menu(System::Object *  sender, System::EventArgs *  e)
    		{
    			int newx = ship->Location.X + 10; 
    			int newy = ship->Location.Y; 
    			Point newloc = Point(16,408); 
    			ship->Location = newloc; 
    		}
    I just had to make shure I had set newloc to the position that the ship was orginally in (In this case 16,408).

    Now I am going to try and figure out how to implement the rand() function. I think for now I only want to implement it with my horizontal point.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    As you are using Managed C++ ( you are aware that you will need to ship the .NET Framework with your game, right ? ), you can also use the System::Random class.

    System::Random* m_Random = new System::Random();

    m_Random->Next(); will give you a random number.

    There is no need to initialize the Random class, it will automatically be initialized with the current time. Create an instance of the random class only once, not every time you need a random number.

    If you need something very secure and better than normal Random, you can use System::Security::Cryptography::RandomNumberGenera tor.
    But that might be a bit much for a game.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Does this look weird...
    Code:
    	private: System::Void newGame_Menu(System::Object *  sender, System::EventArgs *  e)
    		{
    System::Random* m_Random = new System::Random();
    
    			int newx = ship->Location.X + 10; 
    			int newy = ship->Location.Y - 10; 
    			Point newloc = Point(m_Random->Next(),408); 
    			ship->Location = newloc; 
    		}
    It compiles but when the event handler is called the "ship" disapears? Do I have to set a range so that the ship will actually fall on to a point that is in range of the form?

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you have limits for your random values, you should use the overload of the Next method that accepts limits as parameters:

    http://msdn.microsoft.com/library/de...nexttopic3.asp
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I decided to mess around with rand() because I some what understand it. At ended up with this....
    Code:
    	private: System::Void newGame_Menu(System::Object *  sender, System::EventArgs *  e)
    		{
    			srand(time(0));
    			int newx = ship->Location.X + 10; 
    			int newy = ship->Location.Y; 
    			Point newloc = Point(rand()%456,408); 
    			ship->Location = newloc; 
    		}
    This works but not in a very efficient way. Every time I call this function it generates a new position but that new position is not a great variant from the previous.
    In truth it only is about 1 pixel of a diffrence. How do I generate a greater variance between new generations?

    EDIT: Hmm weird I just remove the srand(time(0)) from my code and now it seems to be doing what I want. I must be confused as to why everyone is telling me to seed it. Does anyone care to explain this to me....

    Thanks

    Chad
    Last edited by chadsxe; 08-14-2005 at 12:35 PM.

  11. #11
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Random number generators on finite state machines (i.e. computers) aren't random at all. Essentially your C compiler includes or builds at runtime a "book" of pseudo-random numbers. Whenever you ask for a random number, the computer refers to this book at a specific position, reads a number and then increments the position. The "specific position" this process starts from is what is set by srand(), literally "seeding the random number generator".

    The statement:-
    Code:
    //correct form
    srand((unsigned int)time(NULL));
    Randomizes this position using the current system time, to give a further illusion of randomness.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I just wanted to say thanks again for the help, I think I have figured it out. I ditched the rand() function and went with the System::Random class. Hear is what I came up with.....
    Code:
    private: System::Void newGame_Menu(System::Object *  sender, System::EventArgs *  e)
    		{	int Next( int maxValue);
    			System::Random* m_Random = new System::Random();
    			int newx = ship->Location.X; 
    			int newy = ship->Location.Y; 
    			Point newloc = Point(m_Random->Next(456),408); 
    			ship->Location = newloc; 
    		}
    At first I could not figure out how to overload the next method to set a range but then it clicked.

    Thanks

    Chad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  4. fast random number generation
    By Heraclitus in forum C Programming
    Replies: 4
    Last Post: 02-09-2003, 07:48 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM