How to create a random number in the range 0-8, except for one specific number?

I am trying to develop a Tic Tac Toe game where player 1 will click on one button to put an “X” and later player 2 (computer) will overlay “O” on other buttons randomly in the first step. To do this, I need to create a random number between 0-8, with the exception of one click of player 1.

I use the following code to generate a random number

Random random=new Random();
int number=random.nextInt(9);
+4
source share
7 answers

0 8 , 8 , 9. , 0 7 - random.nextInt(8) - , 1 .

+4

, . .

// Initialize the list:
List<Integer> numbers;

public void init() {
    numbers = new LinkedList<Integer>(9);
    for (int i = 0; i < 9; ++i) { 
        numbers.add(i);
    }
}

public int nextSelectNumber() {
    int index = random.nextInt (numbers.size());
    return numbers.get(index);
}
+1

0 8, , , . , (?) . , . List<Integer> , . , @Mureinik answer:

class TicTacToeCells {
    private static final int MAX_CELLS = 9;
    private List<Integer> cells;
    private Random random;

    public void init() {
        random = new Random();
        cells = new ArrayList<Integer>(MAX_CELLS);
        for (int i = 0; i < MAX_CELLS; ++i) { 
            numbers.add(i);
        }
    }

    //for human moves
    public boolean canPickCell(int cellToPick) {
        return cells.contains(cellToPick);
    }

    public int pickCellByComputer() {
        int index = random.nextInt(cells.size());
        return cells.get(index);
    }

    public void pickCell(int cellToPick) {
        cells.remove(cellToPick);
        //further logic to display the move or something else...
    }
}
+1

:

    int alreadyPickedNumber=3 //Number to exclude
    int number=random.nextInt(9);
    while(number==alreadyPickedNumber){
         number=random.nextInt(9);
    }
0

. :

@ThreadSafe
public final class TicTacToeNumberChooser
{
    private final Random random = new Random();
    // Default initialization value for boolean arrays is false
    private final boolean[] picked = new boolean[9];
    private int nrPicked = 0;

    public synchronized int randomPick()
    {
        if (nrPicked == 9)
            throw new IllegalStateException();

        int ret;
        while (true) {
            ret = random.nextInt(9);
            if (picked[ret])
                continue;
        }
        picked[ret] = true;
        nrPicked++;
        return ret;
    }

    public synchronized boolean isPicked(final int number)
    {
        return picked[number];
    }

    public synchronized void doPick(final int number)
    {
        if (picked[number])
            throw new IllegalStateException();
        picked[number] = true;
        nrPicked++;
    }
}
0

nextint (availableSpaces) , .. 3

0

0-8, "set used" . 0 (8-used.size()), , .

int get_random_int(set<int>& used) {
    int no_of_numbers = 8 - used.size();     
    int index = rand() % no_of_numbers;
    int target = 0;
    for (int i = 0; i <= index;) {
         if (used.find(target) == used.end()) i++;
         target++;
    }
    return target;
}

. , .

0

Source: https://habr.com/ru/post/1529661/


All Articles