random numbers every so often

4

Does anyone know how to generate random numbers every minute?

Right now I only have one for where it sends 3 data just for nothing, but it sends them instantly, and I need every minute to throw a random number at me.

for(int i =0; i<3; i++){      
int numero = (int) (Math.random() * 100) + 1;      
System.out.println(numero);     
} 

Result is:

56
32
49
BUILD SUCCESSFUL (total time: 0 seconds)
    
asked by VirusDetected 05.11.2018 в 07:02
source

1 answer

3

You can use a call to Thread.sleep() to enter a wait of n milliseconds before getting the next number.

Sleep receives the number of milliseconds that will sleep the thread from which it is invoked before continuing execution.

Example, adapting your code, it could be:

for(int i =0; i<3; i++){      
  int numero = (int) (Math.random() * 100) + 1;      
  System.out.println(numero);     
  Thread.sleep(60000); //60000 milisegundos son 1 minuto
} 

The output will be the same, but your program will deliver each number 1 minute after the previous one.

A defect of the code that I send you is that after delivering the last number, the program will wait a minute before finishing. This is not necessary, it is up to you to adapt it so that you do not wait, if you wish.

    
answered by 05.11.2018 / 07:18
source