0

:) That's my function:

public int random(int amountOfLines)
{
    labelWithNumbers.setVisible(true);
    labelStatic.setVisible(true);
    randomButton.setEnabled(false);
    Random generator = new Random();
    int fromNumber = 1; 
    int toNumber = amountOfLines;
    int questionID = 0;
    for(int i=0; i<5; i++)
    {
        questionID = generator.nextInt(toNumber - fromNumber + 1) + fromNumber;
        labelWithNumbers.setText(Integer.toString(questionID));
        try {
            Thread.sleep(120);
        } catch (InterruptedException ex) {
            Logger.getLogger(LosowanieForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return questionID;
}

For example fromNumber = 1 and amountOfLines = 5. Function return 3 and now I want to delete this number from amountOfLines. In the next function call I want to random numbers: 1, 2, 4, 5 - it doen't include 3. And every time like that. I don't want to delete number and make list and .size(), because I want to delete this number from labelWithNumbers.setText() to don't show it in label. How can I solve this problem? Thanks for all answers!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Persantarus
  • 37
  • 1
  • 6
  • Please see the [many similar questions that can easily be found](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+random+no+repeat). Easiest solution: create an `ArrayList`, shuffle it via `Collections.shuffle(...)`, and then remove the 0th item from the list. – Hovercraft Full Of Eels Feb 11 '17 at 18:26
  • Note that I've removed your Swing tag since the specific problem, that of how to find non-repeating random numbers, has nothing to do with Swing GUI coding. Your code though does have a Swing related problem with the for loop and `Thread.sleep` as they go against Swing threading rules. It looks like you want to use a Swing Timer in place of both the loop and the sleep call. – Hovercraft Full Of Eels Feb 11 '17 at 18:27
  • Shuffle, and pick the numbers off in order. – rossum Feb 11 '17 at 21:36

0 Answers0