Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Random Method

Holly Leery
Ranch Hand
Posts: 97
I was wondering if someone could explain the Random Method to me. I want to use it where in a java applet a number between 0 to 3 is randomly picked to make a dot on a grid go north south east west. I will put it in a loop that runs 100 times to make this dot move around the grid. With this I will use a switch statement, but I am some what lost. Any help?

somkiat puisungnoen
Ranch Hand
Posts: 1312
Originally posted by Holly Leery:
I was wondering if someone could explain the Random Method to me. I want to use it where in a java applet a number between 0 to 3 is randomly picked to make a dot on a grid go north south east west. I will put it in a loop that runs 100 times to make this dot move around the grid. With this I will use a switch statement, but I am some what lost. Any help?

Example for java.util.Random class :

somkiat puisungnoen
Ranch Hand
Posts: 1312
Example to use Math.random() method

Francis Siu
Ranch Hand
Posts: 867
hi Holly Leery
Random method is used to generate a number greater than or equal to 0.0 and less than 1.0

The above example is generated between 0 to 3
For the following example is 1 to 3

and also with Math.round()

As I can not post the linkage of Math.random() and Math.round() because Mapraputa's
Hope this help

[ September 30, 2004: Message edited by: siu chung man ]

Holly Leery
Ranch Hand
Posts: 97
so is that what I would put into the code? I am completely confused on this random method stuff. how do i incorporate my switch statemetns in to that?

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
I assume 0 1 2 3 represent up down right left in some scheme. So you might have something like:

I'll leave it to you to get that into real Java syntax. Are you familiar with the convention of constant variables in all caps? I'm implying there is a line somehwere something like:

private static final int UP = 0;

Did your assignment say anything about the size of your play area? What if you move off the edge of the screen? There's some interesting math at play - google for Drunkard's Walk for details - that always makes this thing go off the screen. (Or is it just the quality of Random?) You'd expect the average location to hang round the middle but don't count on it.

Holly Leery
Ranch Hand
Posts: 97
Yeah the dot is not supposed to go off of the grid it has to stay with in the grid. x and y cannot go below 10 or above 310. i forgot about that part. any ideas of how to keep this from happening?

Layne Lund
Ranch Hand
Posts: 3061
It sounds like you should set up a few if statements to keep the dot from wandering outside of the grid. I'll let you think about it for a while. Perhaps you can post here an explanation of what you think you can do to solve this part of the problem. You can use English or pseudocode similar to what Stan gave above.

Layne

Holly Leery
Ranch Hand
Posts: 97
Here is my code so far, I am sure I mistakes in there. I am new at this so I don't catch things very fast, but this is what I have so far. What do you think? Thanks for your help!!

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Looks like you're going the right direction. Have you gotten this into a form that compiles yet? Where you said "location x" you probably just mean your variable "x".

Every time you change position I think you'll want something to "erase" the old ant and draw a new one. Otherwise it will be hard to see where he's going. If you want to leave old ants around to see the path taken, you might redraw the current position in black then draw the new position in red.

What do you want to happen when the ant hits an edge? Say it's in the right-most column and random gives you a right. Maybe just stay put and hope the next time through the loop gets a different random direction? Should you count that in your 100 moves? Maybe it could wrap off the right side into the leftmost column. Any other cool ideas?

Holly Leery
Ranch Hand
Posts: 97
Yeah does anyone have any ideas for what to do when the ant hits the edge? x and y have to stay below 310 and above 10. how would i do that? so my code looks ok? i seem to be getting errors saying ; expected on each of the cases and identifer expected on the distance formuland the message dialog box...any ideas there either?

Holly Leery
Ranch Hand
Posts: 97
I'd like the dot to come back into the grid, not go outside, so yes that can be part of the 100 times. where might i look to figure out how to start this part?

Nigel Browne
Ranch Hand
Posts: 703
To keep the ant within the grid you must check the position of the ant before any movement occurs, so if the ant is in the top left hand corner of the grid, both north and west are disallowed moves, if the ant is on the left hand edge of the grid movement west is disallowed and so on.
Basically it will mean that if you have randomly been assigned an illegal move you will need to generate new random directions until a legal move is generated.

Layne Lund
Ranch Hand
Posts: 3061
To fix your compiler errors, you first need to change "location x" and "location y" to "x" and "y", respectively. Other than that, I'm not sure. Why don't you make this fix and post any other compiler errors you get (along with the modified code, of course)? You should also somehow mark the lines that cause the errors. I'm not about to count lines to find an error on line 25, let alone line 100-something!

Layne