wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes JAVA RAndom Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JAVA RAndom Numbers" Watch "JAVA RAndom Numbers" New topic
Author

JAVA RAndom Numbers

Martin Bezener
Greenhorn

Joined: Nov 26, 2003
Posts: 7
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

Joined: Oct 29, 2001
Posts: 8843
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Martin Bezener
Greenhorn

Joined: Nov 26, 2003
Posts: 7
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

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Martin Bezener
Greenhorn

Joined: Nov 26, 2003
Posts: 7
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Nov 01, 2003
Posts: 5
up
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
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

Joined: Jun 24, 2002
Posts: 90
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
 
subject: JAVA RAndom Numbers
 
Similar Threads
Minimum and maximum random number?
generating unique Random Numbers
HELP!!! Im running out of time.
Slowing Down a program