Link to home
Start Free TrialLog in
Avatar of pigmentarts
pigmentartsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Making the time lapsed from Thread.sleep method random

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

Avatar of RomanRega
RomanRega

try:

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

int rand=new java.util.Random(System.currentTimeMillis()).nextInt(3000)+3000;
t.sleep( rand ) ;
Avatar of pigmentarts

ASKER

how do i use

>int rand=new
i've put it under the main class but it causes errors
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);
       }

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();
          }
       }
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();
instead of the    Thread.sleep(3000);
ASKER CERTIFIED SOLUTION
Avatar of RomanRega
RomanRega

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial