This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Method

 
Holly Leery
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :



This will help you.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example to use Math.random() method

 
Francis Siu
Ranch Hand
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

You can also read more about Math.random()

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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic