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?!

5 Years
Discussion Span
Last Post by FlamingClaw

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

Edited by pyTony: n/a


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 numbers...so it is need realy. :-D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.