0

I have a practise exercise to complete soon and I really am baffled on how I would do this part. The exercise is to create a BlackJack game, my Array includes (2,3,4,5,6,7,8,9,10,10,10,10,11) and I need to give the player and dealer 2 cards/numbers each, how do I get 2 randomly selected numbers from that Array into labels???

Thanks Brandon

hometoast
  • 11,522
  • 5
  • 41
  • 58
  • 1
    Do you have a problem with the selecting random entries from the array? Or populating the labels on the form? – hometoast Jun 11 '13 at 17:01
  • http://stackoverflow.com/a/14570501/546922 – emd Jun 11 '13 at 17:02
  • 1
    +1 for being honest that this is homework. You should show what you have tried so far. –  Jun 11 '13 at 17:04
  • 1
    I think for this to be statistically better, you really would have to make a list of items, especially since the Ace can be 1 or 11 in BlackJack. So rather name the cards 2..10, J,Q,K,A, and assign values to the different cards, have N of each cards, and pick a random card of those in the pool. – SinisterMJ Jun 11 '13 at 17:06
  • 1
    @Scott Selbys answers is good but to make the game properly random in a way which mimics a real card game you should remove each card from your array as you use it. – MichelleJS Jun 11 '13 at 17:13
  • @MichelleJS - no not the case with blackjack, if you really wanted to do that you would have an array with 6 decks worth of cards , but only be allowed to use 4 decks worth, it gets too complicated for homework , its acceptable in bj to just have "unlimited cards" since say 5 of spades comming 4 times in a row is ok. – Scott Selby Jun 11 '13 at 19:52

4 Answers4

2
public int GetRandomCard()
{
    int[] allCards = {2,3,4,5,6,7,8,9,10,10,10,10,11}
    Random random = new Random();
    int i = random.Next(0, 12);
    return  allCards[i];
}

this should get you started...

Label1.Text = GetRandomCard.ToString()

Also the array should be 52 long, and each int should have a card assigned to it. This will help for getting aces , and also for if you want to convert these int's to images you will get a random amount of suits.

Scott Selby
  • 9,420
  • 12
  • 57
  • 96
2

One way to do it is running a Fisher-Yates shuffle on the array, and pick the first two items.

You cab pick up an implementation from here. All you need is to pass your array to Shuffle, and pick the two initial items from the shuffled array.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Use the return value from random.next(0, array.length) as index to get value from the array

int start = random.Next(0, your_array.Length);
 Label.Text=your_array[start];
Ankit Dhadse
  • 1,566
  • 1
  • 15
  • 19
0
    int[] array = new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
    Random r = new Random();
    int cardIndex = r.Next(0, array.Length);
  • 1
    `Random` is always initialized with a seed. The default is based on system time. In no way does providing a different seed make a pseudo-random number generator more random - it only changes where you start enumerating from. – Preston Guillot Jun 11 '13 at 17:31
  • As you are right i edited my answer and removed the wrong statement about "making more random". –  Jun 12 '13 at 06:58