tag:blogger.com,1999:blog-865923359735383241.post3987292899729564629..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: Two random number generators (card dealing)Unknownnoreply@blogger.comBlogger3125tag:blogger.com,1999:blog-865923359735383241.post-90231869822777471372012-02-14T19:08:40.268-07:002012-02-14T19:08:40.268-07:00If you are trying to pick a card randomly from a l...If you are trying to pick a card randomly from a list you should ma a list of the cards and then pick randomly from it. The code you are using does not guarantee that you will every pick a card that you have not pick before.<br /><br />Random rng = new Random();<br /><br />// making a list of cards<br />List<Integer> possibleCards = new ArrayList<Integer>();<br /><br />for ( int i=0; i < 52; i++ ){<br /> possibleCards.add(i);<br />}<br /><br />// copy the list<br />List<Integer> cardsThisRound = new ArrayList<Integer>(possibleCards);<br />// randomly remove from list<br />for (;cardsThisRound.size() > 0; ){<br /> int randomPick = cardsThisRound.remove(rng.nextInt(cardsThisRound.size()));<br /> test(int);<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-4049673615668150762012-02-14T19:08:39.780-07:002012-02-14T19:08:39.780-07:00The obvious problem is that Random.nextInt generat...The obvious problem is that Random.nextInt generates a number 0 (inclusive) and n (exclusive). So when you pass in a limit of 53, you're generating values between 0 and 52 — that's 53 possible values, and there aren't that many cards. So change the upper limit to 52.<br /><br />Also, as other answers note, the reason you're getting collisions (dealing the same card to multiple hands), etc. is that you don't want to randomly select the cards. You actually want to randomly shuffle the deck.<br /><br />Try using a shuffle algorithm instead.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-36484716035203697432012-02-14T19:08:38.528-07:002012-02-14T19:08:38.528-07:00You'd be better off dealing the cards out rand...You'd be better off dealing the cards out randomly.<br /><br />Using the O(n) Knuth Shuffle you can pass along one card at a time, from one array to another.<br /><br />Each new array you pass the card to will be a different persons' hand.<br /><br />Keep adding 1 card at a time to each array from the deck array until each player has the number of cards your rules require.<br /><br />Also, don't rely on random numbers being equal. See stupid-sort. Look for some other pattern instead. Like a random number of cards % cards_left you should stop giving out cards.<br /><br />Update with code-sample, as per request:<br /><br />public static void shuffle (int[] array) {<br /> for(int n = array.length; (n > 1);) {<br /> int k = gen.nextInt(n--);<br /> int temp = array[n];<br /> array[n] = array[k];<br /> array[k] = temp;<br /> }<br />}Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com