Bookmark Topic Watch Topic
  • New Topic

Slowing Down a program  RSS feed

 
Martin Bezener
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Report post to moderator
Is there any way to delibaretly slow down a program so that a new random number can be generated using ths system clock?
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Report post to moderator
call Thread.sleep( millisecond ) method
 
Martin Bezener
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Report post to moderator
This was my original post. How would I be able to incorporate the Thread.sleep method into it so that the system clock would change before a new random number is generated?
*******************
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;
}
}
 
Dirk Schreckmann
Sheriff
Posts: 7023
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!