Duplicate random numbers

Asked

Viewed 55 times

0

I created a class that contains a method that generates a random number from 0 to 61... in my Fragment I instituted within my event of button... My question is, how do I put the amount of times it will generate for me. For example, I have a spinner with the options 1,2,3 then I choose the 2, when clicking the button it has to generate for me:

1º = 25 2º= 50

all at the same time

How would I do it? Because if I create each method for the amount of times it becomes difficult, because imagine if the user wants it 100 times, there complicates. So how would I do that, I have no idea, someone could help me?

Code:

public void geraSix(){
    MegaSenaController numberRandom = new MegaSenaController();
    numberRandom.megaSena();
    String groupFirst = numberRandom.getPrimary();
    groupText.setText(groupFirst);
}

2 answers

1

It was not clear what the methods created by you do exactly, but following your logic you should store the values in a List and then show them. Ex:

MegaSenaController numberRandom = new MegaSenaController();
int qntNumeros = (int) spinner.getSelectedItem();
List<Integer> numeros = new ArrayList();

for (int aux=0 ; aux<=qntNumeros ; aux++) {
    numberRandom.megaSena();
    numeros.add(numberRandom.getPrimary());  
}

This causes the random numbers to be stored in numeros. Then you just show them.

  • I did some things, but I couldn’t do the multiplication...

  • Multiplication? You refer to the creation of random numbers, right? You have tried to print the return of methods megaSena() and getPrimary() to see if they are correct? Try posting their code in your question as well.

0

I believe a loop is a solution:

for (var i = 0, max = max/*neste max voce deve pegar o valor que o usuario quer*/; i < max; i++) {
    //gera um numero aleatorio
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.