Amrita Dole

Greenhorn

Posts: 20

posted 13 years ago

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

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

Stephen Huey

Ranch Hand

Posts: 618

posted 13 years ago

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.

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

posted 13 years ago

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?

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?

David Crossett

Ranch Hand

Posts: 102

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 13 years ago

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!

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!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

It is sorta covered in the JavaRanch Style Guide. |