[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 410
  • Last Modified:

Making the time lapsed from Thread.sleep method random

I want a random time gap between 3-5 seconds is this possible?

0
pigmentarts
Asked:
pigmentarts
  • 5
  • 4
1 Solution
 
RomanRegaCommented:
try:

int rand=new java.util.Random(System.currentTimeMillis()).nextInt(3)+3;
t.sleep( rand ) ;
0
 
RomanRegaCommented:
ehm, i forgot those are milliseconds, so change it in:

int rand=new java.util.Random(System.currentTimeMillis()).nextInt(3000)+3000;
t.sleep( rand ) ;
0
 
pigmentartsAuthor Commented:
how do i use

>int rand=new
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pigmentartsAuthor Commented:
i've put it under the main class but it causes errors
0
 
RomanRegaCommented:
Random random=new java.util.Random(System.currentTimeMillis());
int time=random.nextInt(2000)+3000;
Thread.sleep(time);

to see how it works:

       Random r=new java.util.Random(System.currentTimeMillis());
       for (int i=0; i< 100; i++){
          int time=r.nextInt(2000)+3000;
          System.out.println(time);
       }

0
 
RomanRegaCommented:
or better, always to see how it works:

       Random r=new java.util.Random(System.currentTimeMillis());
       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();
          }
       }
0
 
pigmentartsAuthor Commented:
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();
0
 
pigmentartsAuthor Commented:
instead of the    Thread.sleep(3000);
0
 
RomanRegaCommented:
import java.util.*;
...

Random random=new Random(System.currentTimeMillis());
while (index < array_of_clips.length) {
                    array_of_clips[index].play();
                    int time=random.nextInt(2000)+3000;
                    try {
                        Thread.sleep(time); // 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();
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now