Prevent randomized picture box from leaving

Cheetaiean

Member
Joined
Jan 20, 2014
Messages
13
Programming Experience
Beginner
I have a bottle of gatorade randomly appearing and then moving.

The problem is that after a while it 'randomly' moves up and left until it leaves the form.
Any help would be appreciated to prevent it from leaving and maybe being more random.

My code for the bottle:
VB.NET:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] 
[/SIZE][/FONT] 
[/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] imgHealth.Visible = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
            Randomize()

            imgHealth.Top = imgHealth.Top + Int((200 * Rnd() - 200))

            imgHealth.Left = imgHealth.Left + Int((200 * Rnd() - 200))      
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
 
First of all, don't use Rnd to generate random numbers. Use the Random class. If you must use Rnd though, because this is homework, don't put the Randomize statement where you have it. You execute ONE AND ONLY ONE Randomize statement.

As for the question, you know exactly how much room there is between the edge of your control and the edge of the parent container so write your code such that the number generated won't be any greater than that. Instead of generating a change in the position, generate the actual position within an appropriate range.

Also, don't set the Top and Left separately. If you want to change both then set the Location property.
 
Okay that was a lot of complicated stuff for me, a beginner high school student.
First, on my rubric, it only says I need to use 'Randomize', so whatever you're proposing seems to be fine. Do you mean like Dim word as Random or Public Class Random? Forgive my ignorance :D.

Also, for the Location thing, the microsoft example was:
VB.NET:
[COLOR=#000000][FONT=Consolas] textBox1.Location = [/FONT][/COLOR][COLOR=blue][FONT=Consolas]New[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Point(15, 15)[/FONT][/COLOR]   groupBox1.Controls.Add(textBox1)
That seems to make a specific point, not a range within which the thing can go. If I make the .Location random, it still won't be confined to the form. Is there a way to like make random integers within a certain range?
 
If you need to use Randomize then you need to use Rnd. It's a complete disgrace that anyone still teaches that but such is life. Use Randomize once and once only and then use Rnd each time you need a random number. It's up to you to specify what range you want the random number to be taken so specify the range you want. Make sure that the minimum is not less than zero and the maximum also within an appropriate range. It's simple arithmetic. Think through the logic first. That's what most people do wrong: they try to write code without a clear idea of what it's supposed to do and then wonder why it doesn't do what they want.
 
Yeah my teacher is very outdated (despite her youth) and also does not know what she is doing, just copies malfunctioning code from sketchy sites.

Anyways, I will do as you're saying, however I am not sure how to do a range.
For example, should I do:
VB.NET:
I[COLOR=#333333][FONT=Consolas][SIZE=2][COLOR=#0000ff]f[/COLOR][/SIZE][/FONT][/COLOR][COLOR=#333333][FONT=Consolas][SIZE=2] imgHealth.Visible = [/SIZE][/FONT][/COLOR][COLOR=#333333][FONT=Consolas][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][/FONT][/COLOR][COLOR=#333333][FONT=Consolas][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/FONT][/COLOR][FONT=Consolas][SIZE=2]
            Randomize()

            imgHealth.Location = (10, 15)
             [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT]


How do I make the the co-ordinates random?

Edit: I want the range to be the size of my form (width: 736 height: 55). SO maybe I should find a "random integer between 1 and 736" command or something?
 
Last edited:
Like I said, think through the logic first. What is the logic of what you want to happen, e.g.
I want the control to move a random amount in the horizontal direction up to 100 pixels while ensuring that the left edge of the control does not go beyond the left edge of its parent and the right edge of the control doesn't go beyond the right edge of its parent.
That is an example of clear and precise logic. That is something that you can write code to implement and check afterwards with 100% certainty whether the code does in fact implement the logic. If you don't know what the logic is then you can never know whether the code implements it.
 
Okay, to be direct and logical.

I want the imgHealth.Left to equal a number between 5 and 700. What would I do?
So:
VB.NET:
Randomize()
imgHealth.Left = CInt (Int((something * Rnd))
(Sorry for the code inaccuracy).

So my question now is basically how do I receive a random number between 5 and 700?
 
Okay so I found this code on stack overflow and it works like a charm:

imgHealth.Left = CInt(Math.Ceiling(Rnd() * 700))

Where 700 is the maximum place it can go. No idea how this works but I am satisfied nonetheless :D

Thank you for the advice on just changing position (=) instead of making it move to another spot.
 
So, you don't understand but it never occurred to you to read the documentation for the Rnd function? That's why things have documentation: to explain how they work.

Rnd Function (Visual Basic)
 
well that example had for example math.Floor instead of Ceiling and confusing things like that

Again, did you read the documentation for the Math.Floor and Math.Ceiling methods? They're not confusing; they're just unknown. If you read the relevant documentation then they will no longer be unknown. They are simply implementations of very common mathematical functions. Is it really confusing to round a number up or down to the nearest integer?
 
Back
Top