The question is as follows:

How long – number of steps -- does it take a drunken ant to fall off the edge of a square table if it starts in the middle? Perform a simulation to find out.

Imagine an ant in the middle of a table with, say, about 100 direct steps to any edge.
Each step the Ant takes is a random change of –1, 0, +1 in the X direction and of -1, 0, +1 in the Y direction, so the Ant can move to any one of nine positions relative to where it is (including falling down in its current position when the change in X is 0 and the change in Y is also 0).

How does one go about this?!

Recommended Answers

All 3 Replies

1) Decide algorithm
2) Try it on paper
3) Make test cases
4) Write code incrementally and run tests
5) Run and get results, when tests pass

Try to make something like that

procedure RandomMove;
MoveX : array[0..2] of integer = (-1, 0, 1);
MoveY : array[0..2] of integer = (-1. 0, 1);
Randomize; //you need to use this procedure before you the Random() one
Ant.MoveToXY(Random(MoveX), Random(MoveY)); //the value in Random() must be the value that will be randomized.

The Randomize procedure task is to set the RandSeed constant to generate always random it is need realy. :-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.