Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

filling two arraylists with random ints without duplicating

$
0
0
Hello
I'm trying to randomly generate the hands of two players for the initial deal of a card game.
Im using ArrayLists player1Deck and player2Deck to cut 52 cards in half , but I can't seem to figure out how to get rid of the duplicates floating around in each player's deck.
Any tips to point me in the right direction would be great.
Thanks

      
public void initialDeal() {
	
	 while ( player1Deck.size() < 26 ) {

		// random number between 0 and 51  
                int randomNum = (int)(Math.random() * 51); 		
                if  ( !player1Deck.contains (randomNum ))
		  {
		     player1Deck.add ( randomNum ) ;
		  }
	                                                     } 
	 
	  while ( player2Deck.size () < 26 ) { 

		 int randomNum = ( int ) ( Math.random () * 51 ) ;   
		 if  ( !player2Deck.contains ( randomNum ))
		  {
		     player2Deck.add ( randomNum ) ;
		  }
	                                                      } 
          
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>