110,536
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
Deck myDeck = new Deck();
myDeck.Shufle(); //洗牌,即打乱牌的顺序
Card[] selectedCards = new Card[5];
int[] coCards = new int[5];
for (int i = 0; i < 5;i++ )
{
coCards[i] = i; //初始化
}
//随机抽取0到51之间的5个不重复的数字
for (int i = 0; i < 5;i++ )
{
//检查该数字是否存在于coCards数组
int tempNum = 0;
Boolean exist = false;
Random tempRandom = new Random();
do
{
tempNum=tempRandom.Next(0,52);
for (int j=0;j<i;j++)
{
if (coCards[j]==coCards[i])
{
exist=true;
}
else
{
exist = false;
}
}
} while (exist==true);
if (exist==false)
{
coCards[i]=tempNum;
}
}
//把扑克牌里对应的5张牌取出来
for (int i = 0; i < 5;i++ )
{
selectedCards[i] = myDeck.GetCard(coCards[i]);
}
//显示5张牌
for (int i = 0; i < 5;i++ )
{
Console.WriteLine("{0}",selectedCards[i].ToString());
}
Console.ReadKey();
public static int[] GetRandom2(int minValue, int maxValue, int count)
{
int[] intList = new int[maxValue];
for (int i = 0; i < maxValue; i++)
{
intList[i] = i + minValue;
}
int[] intRet = new int[count];
int n = maxValue;
Random rand = new Random();
for (int i = 0; i < count; i++)
{
int index = rand.Next(0, n);
intRet[i] = intList[index];
intList[index] = intList[--n];
}
const Int32 length = 52;
static void Main(string[] args)
{
Random r = new Random();
//随即生成52个数。
List<Int32> poker = new List<Int32>(length);
for (Int32 i = 1; i <= length; i++)
poker.Add(r.Next(100, 1000));
while (poker.Count > length - 5)
{
int current = poker[r.Next(poker.Count)];
Console.WriteLine(current);
poker.Remove(current);
}
Console.ReadLine();
}