当前位置: 首页 > 知识库问答 >
问题:

检查数组中的整数

从阎宝
2023-03-14
private Stack<Card> deck;//to hold cards
private Card[] protoDeck;//to hold cards before shuffling
private Random randomer;

private int cardsDealt;//how many cards used. Used for other methods
private static final int TOTALCARDS = 52;//sets limit of deck for all decks

public void shuffle(){//remove cards from deck and put back in random order
    randomer = new Random();
    int[] temp = new int[TOTALCARDS];//to keep track of random numbers
    int rand = 0;

    for (int i = 0; i < temp.length ; i++) {
        do {//keep creating randoms if 
            rand = randomer.nextInt(TOTALCARDS);
            deck.push(protoDeck[rand]);//puts the Card onto the Deck in a random position
            temp[i] = rand;
        } while (!(Arrays.asList(temp).contains(rand)));//check if the number already used  
    }
}

@PeterLawrey我稍微调整了代码如下,因为我只需要洗牌,这是一个享受,我会弹出卡片的堆栈来处理

public void shuffle() {
    randomer = new Random();
    for(int i = 0; i < TOTALCARDS; i++) {
        // pick a random card from the rest of the deck
        int j = randomer.nextInt(protoDeck.length - i) + i;
        // swap cards
        Card tmp = protoDeck[i];
        protoDeck[i] = protoDeck[j];
        protoDeck[j] = tmp;
        deck.push(protoDeck[i]);
    }

}

感谢彼得和所有其他贡献者。M.

共有1个答案

祝俊
2023-03-14

从…开始

private final Card[] deck;//to hold cards before shuffling
private final Random rand = new Random();

你可以做

public void shuffle() {
    // no need the shuffle the last card.
    shuffle(deck.length - 1);
}

// will leave the first N card random without duplicates.
public void shuffle(int numberOfCards) {
    for(int i = 0; i < numberOfCards; i++) {
        // pick a random card from the rest of the deck
        int j = rand.nextInt(protoDeck.length - i) + i;
        // swap cards
        Card tmp = deck[i];
        deck[i] = deck[j];
        deck[j] = tmp;
    }
}

成本为O(N),其中N是随机卡片的数量。

想象你有一个小甲板

AS AC AD AH 2S 2C 2D 2H

你需要随机选择第一张牌,你从牌组中选择一张并交换那张牌。假设nextInt()是5=>2c

2C | AC AD AH 2S AS 2D 2H

书桌由随机选择+未选择的卡片组成。你没有重复的,因为相同的卡会被移动。下一个随机卡是2H,它与AC交换

2C 2H | AD AH 2S AS 2D AC
2C 2H AD | AH 2S AS 2D AC

这给你三张随机的牌和其余的。同样的数组可以再次使用,因为从排序或随机甲板开始不会使结果或多或少随机。

在回答答案时,为什么这个简单的洗牌算法会产生偏颇的结果?如果有123个,可能的结果是

123
 +- 123          - swap 1 and 1 (these are positions, not numbers)
 |   +- 123      - swap 2 and 2
 |   +- 132      - swap 2 and 3
 +- 213          - swap 1 and 2
 |   +- 213      - swap 2 and 2
 |   +- 231      - swap 2 and 3
 +- 321          - swap 1 and 3
     +- 321      - swap 2 and 2
     +- 312      - swap 2 and 3

正如你所看到的,只有6种可能的结果,所有的可能性都是一样的。

 类似资料:
  • 问题内容: 我需要检查数组以查看用户输入是否已经存在,并显示一条有关是否存在的消息。第一部分工作正常,但是我尝试创建一种用于单词检查的方法,我不确定自己是否走对了,干杯。 问题答案: 对。您显然已经经历了一个糟糕的思考过程,所以让我们清理一下状况,重新思考一下。 第一步:您需要一些用户输入 第二步:将其与所有先前的用户输入进行比较,以查看是否存在。 如果存在,则返回一条消息,指示已输入值。 否则忽

  • 我正在用随机整数1-10填充数组,但是当我这样做的时候,我需要检查整数是否已经在数组中。我用整数填充数组没有问题,但是我检查重复整数的代码不能正常工作。 }

  • 这可能吗?如果可能,我该如何正确地做到这一点?希望能够检查它是否包含1和2,如果是,继续与程序。

  • 问题内容: 如何检查Java整数是否为另一个数字的倍数?例如,if 是4的倍数。 问题答案: 使用余数运算符(也称为模运算符),该运算符返回除法的余数,并检查其是否为零:

  • 问题内容: 是否有任何方法或快速方法来检查Java中数字是否为整数(属于Z字段)? 我考虑过从四舍五入的数字中减去它,但是我没有找到任何可以帮助我解决这个问题的方法。 我应该在哪里检查?整数Api? 问题答案: 又快又脏… 编辑:假设x已经是其他数字形式。如果要处理字符串,请查看。

  • 在PHP中,检查数组是否为递归数组的最佳方法是什么? 给定以下代码: 从PHP手册: print\u r()在到达数组的第三个元素时将显示递归。 似乎没有其他方法可以扫描数组中的递归引用,因此如果需要检查它们,则必须使用print\u r()及其第二个参数来捕获输出并查找单词RECURSION。 还有更优雅的检查方式吗? 附:这就是我如何使用regex和print\u r()检查和获取递归数组键的