Choosing a random element in an Array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Choosing a random element in an Array

  1. #1
    Join Date
    Sep 2004
    Posts
    37

    Choosing a random element in an Array

    Hi everyone, I have written the below program in which I would like to deal out 1 card and then end the program. Output would be like this:
    Ace of Spades.

    I was wondering how do I choose a random number of the card and a random suit of a card out of the arrays. i have tried using the random() method I have used for random numbers but have hit a wall. Any help would be much apprectiated. Thankyou!

    public class CardDealer
    {
    public static void main(String[] args)
    {
    String[] value = { "Twp", "Three", "Four", "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
    String[] suit = { "Clubs", "Spades", "Diamonds", "Hearts"};

    System.out.println(random(value) " of " random(suit));

    }
    }

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    I think it'd be better to just have an array of all 52 cards. It'd be a lot easier to remove an element that way rather than how you're trying to do it now.

  3. #3
    Join Date
    Sep 2004
    Posts
    37
    Hi Phaelex, thanks for your reply. If I do have an array with all 52 cards how do I then just select one randomly. Wouldn't i just be able to do this with the two arrays I have in my above code as this seems a more elegant solution. I guess my main query is with any random method I can use to output any element in an array. Any suggestions you or anyone has would be great. Thanks again!!

  4. #4
    Join Date
    Jul 2004
    Posts
    83
    import java.util.Random;

    public class CardDealer
    {
    public static void main(String[] args)
    {
    Random rand=new Random();
    int valuePicker;
    int suitPicker;
    String[] value = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
    "Nine", "Ten", "Jack", "Queen", "King", "Ace"};

    String[] suit = { "Clubs", "Spades", "Diamonds", "Hearts"};
    //13 vaules
    valuePicker=rand.nextInt(13);
    //4 suits
    suitPicker=rand.nextInt(4);

    System.out.println(value[valuePicker]+" of "+suit[suitPicker]);
    }
    }
    They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    when you're picking the cards, do the cards get put back in the deck or are they removed from the deck?

  6. #6
    Join Date
    Sep 2004
    Posts
    37

    begcanoc

    Mike, the card is just pulled from the pack and then that is the end of the function. Thanks Sportsdude once again! I don't think I am allowed to import the random class but I am working on my program following your ideas. Thanks once again to Mike and Sportsdude, you guys are brilliant!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center