簡體   English   中英

數組中的隨機數到java中具有不同出現規則的另一個數組

[英]random numbers from an array into another array with different occurences rules in java

我在學校需要幫助。

我需要創建一個包含以下數組中6個隨機整數的數組: montab[] = {1,2,3,4,5,6,7,8,9,10,25,50,75,100}以及以下數組規則:

  1. 數字25,50,75,100只能在陣列中出現一次
  2. 數字110只能在數組中出現兩次

我現在嘗試了第一條規則,但在極少數情況下,我仍然不止一次得到這個數字。

這是我的代碼:

public class Exo7bis {
public static void main (String[] args){
    Random random = new Random();
    int montab[] = {1,2,3,4,5,6,7,8,9,10,25,50,75,100};
    int[] ar1 = new int[6];
    int j = 0, compteur25 = 0, compteur50 = 0, compteur75 = 0, compteur100 = 0;
        for (int i = 0; i < ar1.length; i++) {
            ar1[i] = (montab[new Random().nextInt(montab.length)]);
            if (ar1[i] == 25) {
                compteur25++;
                if (compteur25 > 1) {
                    while (ar1[i] == 25)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
            if (ar1[i] == 50) {
                compteur50++;
                if (compteur50 > 1) {
                    while (ar1[i] == 50)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
            if (ar1[i] == 75) {
                compteur75++;
                if (compteur75 > 1) {
                    while (ar1[i] == 75)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
            if (ar1[i] == 100) {
                compteur100++;
                if (compteur100 > 1) {
                    while (ar1[i] == 100)
                        ar1[i] = (montab[new Random().nextInt(montab.length)]);
                }
            }
        }

        for (int i = 0; i < ar1.length; i++) {
            System.out.print(ar1[i] +" ⎢ " + "\t");
        }
    }
}

我知道我的測試並不完全正確,我發現了問題,但我找不到合適的解決方案。

如果有人可以幫助我或建議我,那將是很酷的。

提前致謝!

傑里米

實際上有一個簡單的解決方案,但它並不能保證真正的隨機數。

我們要做的是創建一個陣列,其中數字1 - 10列出兩次,25 - 100列出一次。 每次選擇一個數字時,我們將其替換為0。

因此,不可能選擇超過一個25或超過兩個8。

在代碼中,

public class Exo7bis {

    public static void main (String[] args){

        Random random = new Random();

        int[] intPool = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,25,50,75,100};
        int[] chosen = {0,0,0,0,0,0};

        int counter = 0;

        // while the list is not full
        while (chosen[5] == 0) {

            // generate a number from 0 - 23 (representing the numbers in intPool)
            int temp = random.randInt(24);

            // if that element in intPool = 0, it means that it's already chosen
            // and can't be chosen again.
            if (intPool[temp] != 0) {
                chosen[counter] = intPool[temp];
                intPool[temp] = 0;

                counter++;
            }
        }
    }
}

有很多方法可以實現這一點,但由於你只是使用數組,我會建議你制作一個計算重復1-10次的函數。 對於第一個條件,您可以將元素替換為0以便下次不會重復。 我認為在代碼中解釋很容易,所以看看你的代碼中你可以改變什么:

public static void main(String[] args) {
        Random random = new Random();
        int montab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100 };
        int[] ar1 = new int[6];
        int j = 0, count = 0;
        while (j < ar1.length) {
            count = 0;
            int index = random.nextInt(montab.length);
            int num = montab[index];
            if (num >= 25) { //adds any number greater or equal to 25
                ar1[j] = num;
                j++;
                montab[index] = 0; // replace the origianl array with 0.
            } else if (num != 0) {
                if(!isRepeated(ar1,num)){ //checks if the array has more than two of the number.
                ar1[j] = num;
                j++;
                }
            }
        }
        for (int i = 0; i < ar1.length; i++) {
            System.out.print(ar1[i] + " ⎢ " + "\t");
        }
    }

    public static boolean isRepeated(int[] arr, int num) { //method that verifies if the array has a number repeated twice or not.
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num)
                count++;
        }
        return count==2 ? true : false;
    }

我沒有測試過,但我很確定它會起作用!!

這是我的方法。 我嘗試修改原始列表/數組,以便原始數字縮小,如果它們按照規則使用。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Exo7bis {
    private static final List<Integer> montab = new ArrayList<Integer>(Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100 }));
    private static final List<Integer> allowOnce = new ArrayList<Integer>(Arrays.asList(new Integer[] { 25, 50, 75, 100 }));
    private static final List<Integer> allowTwice = new ArrayList<Integer>(Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }));
    public static void main(String[] args) {
        int[] ar1 = new int[6];
        for (int i = 0; i < ar1.length; ++i) {
            int index = new Random().nextInt(montab.size());
            int value = montab.get(index);
            ar1[i] = value;
            if (isOneOff(value)) {
                montab.remove(index);
            } else if (isTwoOff(value))
                montab.remove(index);
        }
        System.out.println(Arrays.toString(ar1));
    }
    public static boolean isOneOff(final int n) {
        for (int i = 0; i < allowOnce.size(); ++i)
            if (allowOnce.get(i) == n)
                return true;
        return false;
    }
    public static boolean isTwoOff(final int n) {
        boolean one = false, two = false;
        for (int i = 0; i < allowTwice.size(); ++i) {
            int j = 0;
            for ( ; j < montab.size(); ++j) {
                if (montab.get(j) == n) {
                    one = true;
                    break;
                }
            }
            ++j;
            for ( ; j < montab.size(); ++j) {
                if (montab.get(j) == n) {
                    two = true;
                    break;
                }
            }
        }
        return (one && two);
    }
}

這是一個只涉及數組並提供所需隨機性的解決方案。 我們的想法是將拾取值的出現存儲在另一個int []數組中,並將這些值與您的限制進行比較。 [底部的完整代碼]

說明

這條線

if ((randomNum < 25 && newOccurence == 2) || randomNum >= 25)

檢查你的最大發生率。

然后,重新創建原始數組(montab),而不是已達到其限制的值。

int[] tempTab = new int[montab.length - 1];
int skips = 0;
for (int j = 0; j < tempTab.length; j++) {
    if (montab[j] == randomNum) {
        skips++;
    }
    tempTab[j] = montab[j + skips];
}
montab = tempTab;

如果原始數組中沒有重復值,這種方法顯然有效。 由於數組的重新創建,性能可能不是最好的,但至少你可以避免可能一遍又一遍地使用相同值的隨機性。

經過測試的代碼

import java.util.Random;

public class Exo7bis {

    public static void main (String[] args){

        int[] montab = {1,2,3,4,5,6,7,8,9,10,25,50,75,100};
        int[] ar1 = new int[6];
        int[] occurences = new int[montab.length];

        for (int i = 0; i < ar1.length; i++) {

            int randomIndex = new Random().nextInt(montab.length);
            int randomNum = montab[randomIndex];
            int newOccurence = (occurences[randomIndex] = occurences[randomIndex] + 1);

            /*  UNCOMMENT THIS FOR VISUAL STEPS
            for (int a : montab) { System.out.print(a + " | "); }
            System.out.println("");
            */

            if ((randomNum < 25 && newOccurence == 2) || randomNum >= 25) 
            { 
                int[] tempTab = new int[montab.length - 1];
                int skips = 0;
                for (int j = 0; j < tempTab.length; j++) {
                    if (montab[j] == randomNum) {
                        skips++;
                    }

                    tempTab[j] = montab[j + skips];
                }
                montab = tempTab;
            }
            ar1[i] = randomNum;
        }

        for (int i = 0; i < ar1.length; i++) {
            System.out.print(ar1[i] +" |" + "\t");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM