检查阵列中的一个整数 [英] Checking for an integer in array
问题描述
我有一个锻炼的大学,其中一部分包括创建,那么它必须被洗牌一副纸牌做。我有卡阵列(未洗牌),并希望他们洗牌,推动他们到自制堆叠,从中我可以弹出卡关对付他们。
I have an exercise to do in college, part of which consists of creating a pack of cards which must then be shuffled. I have the cards in an array (not shuffled) and want to shuffle them and push them onto a home made Stack, from which I can pop the cards off to deal them.
我的问题是,我要检查随机数生成我(和重新presents的数组中的卡之一)尚未在堆栈。从阅读这个论坛上,我已经拿出code,低于此,在我看来应该工作。调试虽然我注意到重复。
按我的注释下,我们不能用集合框架(编辑)
My problem is that I want to check that the random number I generate (and which represents one of the cards in the array) is not already in the Stack. From reading on this forum I have come up with the code below which, it seems to me should work. On debugging though I notice duplicates.
As per my comment below we can't use the Collections framework (edit)
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我稍微调整了code如下,因为我只需要充分洗牌甲板和它的工作原理一种享受,我会弹出卡从堆栈处理
@PeterLawrey I have tweaked the code slightly as follows as I only need to shuffle full decks and it works a treat, I will pop cards off the Stack to deal
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]);
}
}
感谢彼得和所有其他的贡献者。米
Thanks to Peter and all the other contributors. M.
推荐答案
与
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是随机卡的数量。
The cost is O(N) where N is the number of random cards.
假设你有一个小的甲板像
Imagine you have a small Deck like
AS AC AD AH 2S 2C 2D 2H
和你需要选择一个随机的第一张牌,你选择一个从甲板,并交换了名片。说nextInt()是5 => 2C
and you need to pick a random first card, you select one from the deck and swap that card. Say nextInt() is 5 => 2C
2C | AC AD AH 2S AS 2D 2H
该台是由随机选择+没有选择卡。你有没有重复,因为同样的卡绕过移动。下一个随机卡说2H是交换与交流。
The desk is made up of cards randomly selected + not selected. You have no duplicates because the same cards get moved around. The next random card is say 2H which is swapped with AC
2C 2H | AD AH 2S AS 2D AC
最后广告恰好被选中。
Finally AD happens to be selected.
2C 2H AD | AH 2S AS 2D AC
这为您提供了三个随机卡和休息。相同的阵列可以再次使用与排序的或随机甲板开始不会使结果任何或多或少随机
This gives you three random cards and the rest. The same array can be use again as starting with a sorted or random deck doesn't make the result any more or less random.
在回答的答案<一个href=\"http://stackoverflow.com/questions/859253/why-does-this-simple-shuffle-algorithm-produce-biased-results-what-is-a-simple\">Why做这个简单的洗牌算法产生偏见的结果?如果有123,可能的结果是
In reply to the answer Why does this simple shuffle algorithm produce biased results? if there is 123, the possible outcomes are
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只可能的结果,都同样有可能。
As you can see there is only 6 possible outcomes, all equally likely.
这篇关于检查阵列中的一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!