programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# change coordinates

Greenhorn
Posts: 20
Hi,
This is a simple Java program. Given initial coordinates for a point (100,100), the point has to randomly move in any direction 50 times.
The distance in each move should be 10 points.
Eg- initial value (100,100)
next move( 110, 100)
next move(110, 90)
...... and so on for 50 times.
Can anyone please tell me how to make the point randomly move in any direction?
Thanks,
Amrita

Ranch Hand
Posts: 618
I'm hesitant to spell it out for you since some people post on here when looking for answers to school assignments. Anyway, this URL will talk about random numbers in Java:
http://leepoint.net/notes-java/47algorithms/12random-api.html
I found that on Google, just in case you ever need to search for something again.
If you're limited to a certain area in which this point can appear, then just remember that your upper and lower bounds for the point values will change depending on where the current point is, so you can change the bounds for the random number every time, or I suppose you could just keep generating random numbers until you got one that was valid.

Amrita Dole
Greenhorn
Posts: 20
Hi,
Thanks for the reply. I'm planning to take a higher level course in Java next term so wanted to use my holidays in practicising some Java problems so its not a school asignment.
My question was actually not how to generate a random number but how to move the point randomly in any direction on the plane given the following-
1. a starting location
2. the distance it can move at one time ie sqrt(x(power)2 + y(power)2) is
fixed
3. the point cannot move outside a max distance, ie can move in a square
area where x = some value and y = some value.
Any clues?

Ranch Hand
Posts: 102
Do you have any code you can post? How far along are you? I'm sure we all have some ideas about how to go from here, but where is *here* for you?

(instanceof Sidekick)
Ranch Hand
Posts: 8791
Without stepping into the "doing the homework" place, think about some of the things you have to figger out:
1) Should I move up, down, right or left? How can I use a random number to pick one of those?
2) How would I change the coordinate to reflect the move?
3) Can I move the direction I chose? Or will that put my X or Y coordinate outside the box? Hmmm, what is the max or min X and Y that fit in the box?
4) What do I do if I can't go that way? Try the opposite way? Try a random direction again until I get one that works?
See if any of those give you a starting point. Could those decisions turn into methods?
Is this called Drunkard's Walk in your class? Google on that term and read some interesting theory about where you might wind up. Have fun ... post again if you have specific code questions!