How to generate random number that goes in 45 in 45 until you reach 540?

0

Hello, if someone could help me generate a random number that goes in 45 in 45 to reach 540?

package edu.cibertec.hilos;

import java.util.Random;

import javax.swing.plaf.SliderUI;

import edu.cibertec.carrera.CarreraMain;

/**
 * Esta clase permite agregar el comportamiento de hilos a objetos a través de interfaz Runnable.
 * @author Marcelo Tataje
 *
 */
public class VegetaRunnable implements Runnable {

    /**
     * Velocidad asignada al hilo.
     */
    final int VELOCIDAD = 45;

    /**
     * Tiempo de descanso para el hilo.
     */
    final int DESCANSO_MS = 1000;

    /**
     * La distancia recorrida que se irá incrementando.
     */
    private int distanciaRecorrida = 0;

    public int hola;

    private int resultado = -1; // variable global

    @Override
    public void run() {
        System.err.println("Vegeta ha iniciado la carrera");
        if(resultado < 0) { // comprobamos
            Random r = new Random(); 
            resultado = (int) (Math.random() *(45));
        
        System.err.println(String.format("resultado: %d en la carrera", resultado));
        while (distanciaRecorrida < CarreraMain.DISTANCIA_TOTAL) {
            int hola =distanciaRecorrida += VELOCIDAD;
            if(hola == resultado) {
            	try {
					Thread.sleep(2000);
				      System.err.println(String.format("Vegeta se ha dormido"));
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
            System.err.println(String.format("Vegeta ha avanzado: %d en la carrera", hola));
            try {
                Thread.sleep(DESCANSO_MS);
            } catch (InterruptedException e) {
                // Ocurre cuando se intenta acceder a un hilo que no se encuentra en estado de ejecución 
                e.printStackTrace();
            }
        }

        System.err.println("->Vegeta ha terminado la carrera");
    }

    }
}
    
asked by Chris Alexis 19.09.2018 в 19:50
source

2 answers

1

I do not understand the statement very well, and I give you the following code that does not need to be executed in a separate thread.

import java.util.Random;

public class Recorrido {

private static final int DISTANCIA_MAXIMA = 540; 
/**
 * Velocidad asignada al hilo.
 */
private static final int RECORRIDO_MAXIMO_POR_VEX = 45;

/**
 * Tiempo de descanso para el hilo.
 */
private static final int DESCANSO = 1000;

 public static void main(String []args){

    int distanciaRecorridoTotal = 0;

    System.out.println("Iniciamos el recorrido");

    while(distanciaRecorridoTotal<DISTANCIA_MAXIMA){
        //ESTO TE TRAE UN NUMERO ALEATORIO DEL 0 AL 45
        int recorridoParcial = new Random().nextInt(RECORRIDO_MAXIMO_POR_VEX+1);
        System.out.println(String.format("Recorriendo %d kilometros", recorridoParcial));
        try {
                Thread.sleep(DESCANSO);
                System.out.println(String.format("Descansando"));
            } catch (InterruptedException e) {
                System.err.println("Se produjo un error al dormir al hilo ");
                e.printStackTrace();
            }

        distanciaRecorridoTotal+=recorridoParcial;
        System.out.println(String.format("Recorriendo acumulado %d kilometros", distanciaRecorridoTotal));
    }

     System.out.println(String.format("Se recorrieron %d kilometros",distanciaRecorridoTotal));
 }
}

In this example, a maximum of 45 km is traveled (by taking a unit of measurement), and the kilometers traveled are accumulated until it is equal to or greater than 540.

I wonder if it is the problem that you pose, but I hope it is useful to you.

    
answered by 20.09.2018 в 01:26
1

First the interval:

  

a number that goes in 45 in 45 until you reach 540

540/45 = 12

so the interval is

45*1 = 45
45*2 = 90
45*3 = 135
...
45*10 = 450
45*11 = 495
45*12 = 540
  

a random number!

We use the function of @AusCBloke to have a random number between 1 and 12

int randomWithRange(int min, int max)
{
   int range = Math.abs(max - min) + 1;     
   return (int)(Math.random() * range) + (min <= max ? min : max);
}

It is used like this:

randomWithRange(1, 12)
  

that goes in 45 in 45!

Well ...

randomWithRange(1, 12) *45

ref:

link

    
answered by 20.09.2018 в 03:21