簡體   English   中英

如何生成指定數量的隨機數?

[英]How to generate specified amount of random numbers?

好的,我有一個大小為 24 的堆棧。我需要用 1-6 之間的隨機數填充它。 但條件是每個數字數量都應​​該相同。 就像四次 1、四次 2、四次 3。在一天結束時,我需要堆棧大小為 24,其中包含 1-6 之間的隨機數,但每個數字的數量應該是 4。我只需要使用堆棧,我不能使用數組或數組列表。 我該怎么做?

 int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0;
    while(!(ottf.isFull())) {
        int x = rnd.nextInt(6) + 1;
        if (x == 1)
            counter1++;
        else if (x == 2)
            counter2++;
        else if (x == 3)
            counter3++;
        else if (x == 4)
            counter4++;
        else if (x == 5)
            counter5++;
        else
            counter6++;

        if (counter1 <= 4 && counter2 <= 4 && counter3 <= 4 && counter4 <= 4 && counter5 <= 4 && counter6 <= 4)
            ottf.push(x);}

我試過用計數器來做,但我想這不會解決我的問題。

if (counter1 <= 4 && counter2 <= 4 && counter3 <= 4 && counter4 <= 4 && counter5 <= 4 && counter6 <= 4)

我猜這是錯誤的,因為您使用&&會在計數器之一超過 4 時停止添加任意數量。

改用這個:

if (x == 1 && counter1 < 4)
        ottf.push(x);
        counter1++;

等。如果有問題,請嘗試調試並發布結果

你能試試這個嗎? 我給了你一段關於你的想法的代碼。 它更短更干凈。

//this array is for counting only. The output is also a stack.
int[] count = new int[6];

while(!ottf.isFull()) {
    int x = rnd.nextInt(6) + 1;
    if(count[x-1]>4) {
       continue;
    }
    count[x-1]++;

    if (count[x-1]<=4) {
      //push to stack
      ottf.push(x);
    }
}

嘗試這個。 它只使用堆棧。 沒有數組和列表。

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 24; i++) {
            stack.push((i%6)+1);
        }
        Collections.shuffle(stack);

        System.out.println(stack);

除非您進行隨機播放,否則輸出將始終不同。 但這是一個例子。

[5, 2, 1, 6, 3, 4, 5, 1, 4, 3, 2, 6, 1, 4, 3, 6, 2, 5, 3, 2, 6, 5, 4, 1]

這種情況可以使用switch...case更好地處理。

請按以下步驟操作:

import java.util.Collections;
import java.util.Random;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Random rnd = new Random();
        int counter1 = 0, counter2 = 0, counter3 = 0, counter4 = 0, counter5 = 0, counter6 = 0;
        Stack ottf = new Stack();
        do {
            int x = rnd.nextInt(6) + 1;
            switch (x) {
            case 1:
                if (counter1 < 4) {
                    ottf.push(x);
                    counter1++;
                }
                break;
            case 2:
                if (counter2 < 4) {
                    ottf.push(x);
                    counter2++;
                }
                break;
            case 3:
                if (counter3 < 4) {
                    ottf.push(x);
                    counter3++;
                }
                break;
            case 4:
                if (counter4 < 4) {
                    ottf.push(x);
                    counter4++;
                }
                break;
            case 5:
                if (counter5 < 4) {
                    ottf.push(x);
                    counter5++;
                }
                break;
            case 6:
                if (counter6 < 4) {
                    ottf.push(x);
                    counter6++;
                }
            }
        } while (ottf.size() < 24);
        Collections.sort(ottf);// Not required. Doing it just to validate the result quickly
        System.out.println(ottf);
    }
}

輸出:

[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6]

暫無
暫無

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

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