mercredi 23 décembre 2015

Getting the same random number [JAVA]

I want to generate a random number to apply to some arrays in order to get different elements in each execution. The arrays contains names of sport products (product,size,price,etc).By doing this,I wanna make random products that would go into a string but in each execution of the program,I get the same product. Where is the problem?

Here is the code in the class generaProductos:

public void generaProductos() {
    int num;
    for (int i=0;i<3;i++){
        num = (int) Math.random() * 3;
        String cliente = tipoProducto[num] + " " + deporte[num] + " " +
                         destinatario[num] + " " + color[num] + " " + tallaRopaAdulto[num]
                         + " " +   preciosIVA[num];
        System.out.println(cliente);
     }
     return;
 }

And here is where I call generaProductos() method in main:

switch (opt){
    case 1:
        generaProductos alm = new generaProductos();
        alm.generaProductos();

When I execute my code,I always receive this:

Botas Futbol Hombre Marron S 16.99

Botas Futbol Hombre Marron S 16.99

Botas Futbol Hombre Marron S 16.99

(In english it would be Football boots men brown size S 16.99)

I am new here so sorry for any mistake I could have made.




Aucun commentaire:

Enregistrer un commentaire