pigmentarts
asked on
Making the time lapsed from Thread.sleep method random
I want a random time gap between 3-5 seconds is this possible?
ehm, i forgot those are milliseconds, so change it in:
int rand=new java.util.Random(System.cu rrentTimeM illis()).n extInt(300 0)+3000;
t.sleep( rand ) ;
int rand=new java.util.Random(System.cu
t.sleep( rand ) ;
ASKER
how do i use
>int rand=new
>int rand=new
ASKER
i've put it under the main class but it causes errors
Random random=new java.util.Random(System.cu rrentTimeM illis());
int time=random.nextInt(2000)+ 3000;
Thread.sleep(time);
to see how it works:
Random r=new java.util.Random(System.cu rrentTimeM illis());
for (int i=0; i< 100; i++){
int time=r.nextInt(2000)+3000;
System.out.println(time);
}
int time=random.nextInt(2000)+
Thread.sleep(time);
to see how it works:
Random r=new java.util.Random(System.cu
for (int i=0; i< 100; i++){
int time=r.nextInt(2000)+3000;
System.out.println(time);
}
or better, always to see how it works:
Random r=new java.util.Random(System.cu rrentTimeM illis());
for (int i=0; i< 100; i++){
int time=r.nextInt(2000)+3000;
System.out.println(time);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Random r=new java.util.Random(System.cu
for (int i=0; i< 100; i++){
int time=r.nextInt(2000)+3000;
System.out.println(time);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ASKER
sorry, could you please show me how to put that in here
while (index < array_of_clips.length) {
array_of_clips[index].play ();
try {
Thread.sleep(3000); // is one second enough??
} catch (InterruptedException ex) {
return; // I should be interrupted if the user want to stop the clips
}
array_of_clips[index].stop ();
while (index < array_of_clips.length) {
array_of_clips[index].play
try {
Thread.sleep(3000); // is one second enough??
} catch (InterruptedException ex) {
return; // I should be interrupted if the user want to stop the clips
}
array_of_clips[index].stop
ASKER
instead of the Thread.sleep(3000);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
int rand=new java.util.Random(System.cu
t.sleep( rand ) ;