❔ Blackjack card randomizer
public static void Blackjack()
{
Console.Clear();
int card1 = 0;
int card2 = 0;
int card3 = 0;
while (true)
{
Random random1 = new Random();
card1 = random1.Next(1, 11);
Random random2 = new Random();
card2 = random2.Next(1, 11);
Console.WriteLine("your cards: {0} and {1}", card1, card2);
Console.WriteLine("do you want another card? y/n");
string hitstand = Convert.ToString(Console.ReadLine());
hitstand = hitstand.ToLower();
if (hitstand == "y")
{
Random random3 = new Random();
card3 = random3.Next(1, 11);
}
else if (hitstand == "n")
{
int cardtotal = card1 + card2 + card3;
bjMath(cardtotal);
}
}
i want to give the user 2 cards to begin with and they can keep picking cards that are random 1-10
ty 🙂
10 Replies
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/Create the rng outside the loop, and reuse it each time
Or use the static instance,
Random.Shared
A better way to do this would be to build an array/list/etc of 50 cards; 1-10 4 times, then 10 11's (for a cheap approximation). Or a bit extra effort to include suits.
Then shuffle the collection (once) and you can easily give them 'cards' from it without having to mess with random anymore
Or use
Random.Shared
How would i do that
google how to shuffle arrays
Fisher-Yates
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.