Пользовательский алгоритм перемешивания - PullRequest
0 голосов
/ 21 ноября 2018

Я знаю, что существует эффективный алгоритм тасования, называемый Фишером Йейтсом, но я пытаюсь создать собственный (из-за упрямства).

Я пытаюсь сгенерировать новое случайное число в нужном диапазоне,чем использовать его, чтобы определить новое случайное место в скопированном массиве.А затем сохраните это случайное число, чтобы НЕ использовать его дважды.Мой метод не работает.Кто-нибудь захочет дать мне немного просветления?

public int[] shuffled(int[] numbers) {
    int rand;
    int copyNumbers[] = new int[numbers.length];
    int usedRand[] = new int[numbers.length];


    for (int i = 0; i < numbers.length; i++) {
        rand = new Random().nextInt(numbers.length + 1);
        if ((i != rand) && (usedRand[i]!=rand)) {
            copyNumbers[rand] = numbers[i];
            usedRand[i]=rand;
        }
    }
    numbers = copyNumbers;
    return numbers;
}

1 Ответ

0 голосов
/ 21 ноября 2018

Как я уже указывал на ошибки в комментариях.Здесь возможна реализация shuffle ().Я прокомментировал код на основе моих модификаций.Если у вас есть вопросы, пожалуйста, не стесняйтесь спрашивать!

public static void main(String[] args) {
    //sample values
    int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};

    //First call of recursive function
    int[] x = shuffled(numbers, null, null, false, 0);

    //Print out values
    for (int i = 0; i < x.length; i++) {
        System.out.print(x[i] + "|");
    }
}

public static int[] shuffled(int[] numbers, int usedRand[], int[] copyNumbers, boolean newRandomValue, int current) {

    //Initialisation
    int rand = -1;
    int start = 0;

    //If function hasn't been called before, initialise.
    if (usedRand == null) {
        usedRand = new int[numbers.length];
        Arrays.fill(usedRand, -1);
    }
    if (copyNumbers == null) {
        copyNumbers = new int[numbers.length];
    }

    //If function has been called recursively, reassign the start-Integer for for-loop
    if (newRandomValue) {
        start = current;
    }

    //Iterate over numbers-array with <start> as start-index
    for (int i = start; i < numbers.length; i++) {
        /*
            new Random().nextInt(numbers.length+1 ) gets a random number between 0 and numbers.length + 1
            In our example, numbers.length+1 is 20. So we will get a random number between 0 and 20. 
            But we want to get random values between -1 and 19, so 
            new Random().nextInt(numbers.length+1 ) --> values between 0 to 20
            rand = new Random().nextInt(numbers.length+1 ) -1  --> values between -1 to 19
         */
        rand = new Random().nextInt(numbers.length+1 ) -1;

        //Iterate through usedRand array and compare if current rand value has already been used yet.
        for (int j = 0; j < usedRand.length; j++) {
            if (usedRand[j] == rand) {
                /*
                    If current rand value has already been used, recursively call shuffle again with the current index i
                    as new start-index
                 */
                return shuffled(numbers, usedRand, copyNumbers, true, i);
            }
        }
        copyNumbers[rand] = numbers[i];
        usedRand[i] = rand;
    }
    numbers = copyNumbers;
    return numbers;
}
...