File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVA RAndom Numbers

 
Martin Bezener
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program to write for a computer science class I am in involving random numbers in JAVA. It involves a drunk man on a bridge, who starts at the center, and has 5 steps to one end or the other, and can last for 100 steps before falling asleep. This is done 5 times. I used 2 clases and this is what the code looks like:
public class DrunkMan
{
int walk (int position, int stepCount)
{
while (stepCount < 100 && position > -5 && position < 5)
{
Random whichWay = new Random();
int way = whichWay.nextInt(2);

if (way == 0)
{
position --;
stepCount++;
}
if (way != 0)
{
position++;
stepCount++;
}

}
System.out.println(position);
return stepCount;
}
}

And then here is the MAIN class.
public class Main
{
public static void main (String[] Args)
{
int position = 0;
int stepCount = 0;
int x = 0;

while (x < 5)
{
DrunkMan walkAttempt = new DrunkMan();
walkAttempt.walk(stepCount, position);
System.out.println(stepCount);
x++;
}
}
}
Problem is that when I run it, the results look like this:
5 (position)
0 (stepCOunt)
5
0
5
0
5
0
5
0
What's wrong with this code?
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The no-argument constructor for java.util.Random() uses the system time as a seed. Given that your program is pretty small, the system time probably doesn't change during the course of the program. Given the same seed, Random will generate the same 'random' number sequence. Print out the value of way and I think you will get what I'm driving at.
 
Martin Bezener
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see exactly what you mean. Now are there really any ways to slow down a program (i.e. have loops that slow the program down)? Thanks for replying.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marty,
I'm not exactly sure what you are trying to do with some of this code.
Perhaps you're not aware that primitives in Java are passed to other methods by value. So, from the perspective of the main method in the Main class, position and stepCount never change - they are both always 0 - because they never change locally (inside of the main method).
The position and stepCount variables over in the walk method of the DrunkMan class are different variables than those in the main method of the Main class. So, incrementing either or both of them in the walk method has no influence on the value of the variables over in the main method.
Is this becoming clear yet?
 
Martin Bezener
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to completely change around my code so that it only conisists of one method and a main all inside of one class. I hope that this will fix the problem. Do you have any suggestions as to whether the variables will be affected in main?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any suggestions as to whether the variables will be affected in main?
I guess I'm not completely sure what you mean. If you've only one method, then changes to variables local to that method would be reflected in different parts of the same method.
 
vigor lee
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
up
 
Igor Ko
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should create random generator instance (new Random) only
one time ! Not a lot of times in cycle.
[ November 30, 2003: Message edited by: Igor Ko ]
 
Igor Ko
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second problem (as it was spoken before) counter, you pass
to function call step counter, then you enter into function you
begins to use the LOCAL of the function copy of the step counter,
so your changes of the local won't change extern variable...
One of solution - don't pass the variables as function arguments,
but use class instance, likes:
class Walker {
int stepCounter = 0;
int currentPosition = 0;
Random random = new Random();
void walk(){
if( random.nextInt(2) != 0 ){
stepCounter--; // HERE you change class instance variable.
} else {
......
}
}
------------------------------------------------
[ November 30, 2003: Message edited by: Igor Ko ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic