C#实现发牌(1-48)

using System;
using System.Collections.Generic;
using System.Security.Cryptography;


namespace Poker1
{
   public struct Poker
   {
       public  int num;
       public  string hua;
       public  int sum;//进行求和
    }
    class Program
    {
        
        static void Main(string[] args)
        {


            
            Poker[] pokers = new Poker[49];
            byte[] array = new byte[1];
            for (int i=0;i<4;i++)
                for(int j=0;j<12;j++)
                {
                    pokers[i*12+j].num = j+1;


                    //通过Ascll码进行ABCD字符串的转换。
                    array[0] = (byte)(Convert.ToInt32('A' + i));
                    pokers[i*12+j].hua = Convert.ToString(System.Text.ASCIIEncoding.ASCII.GetString(array));
                    pokers[i * 12 + j].sum = i * 12 + j;
                    Console.WriteLine("shu:{0},num:{1},hua:{2},sum:{3}", i * 12 + j, pokers[i * 12 + j].num, pokers[i * 12 + j].hua, pokers[i * 12 + j].sum);


                }
            Poker[] pokers2 = new Poker[12];
            //生成随机数字1-48
            int max = 47;
            decimal _base = long.MaxValue;
            // code from DevCurry.com
            byte[] randomBytes = new byte[8];
            int rngNum;
            RNGCryptoServiceProvider rngCrypto =
       new RNGCryptoServiceProvider();


            int[] a = new int[48];
            
            Console.WriteLine("随机数");
            for (int i=0;i<12;i++)
            {
              while(true)
                { 
                    rngCrypto.GetBytes(randomBytes);


                    //   rngNum = (int)(Math.Abs(BitConverter.ToInt64(randomBytes, 0)) / _base * (max + 1));
                    rngNum = (int)Math.Round(Math.Abs(BitConverter.ToInt64(randomBytes, 0)) / _base * max, 0);
                    if (a[rngNum] == 0)
                    {
                        a[rngNum] += 1;
                        break;
                    }
                } 


                
                pokers2[i].num = pokers[rngNum].num;
                pokers2[i].hua = pokers[rngNum].hua;
                pokers2[i].sum = pokers[rngNum].sum;
                Console.WriteLine(rngNum);
                
            }
            Console.WriteLine("输出前");
            for(int i = 0;i<12;i++)
            {
                Console.WriteLine(pokers2[i].num + pokers2[i].hua);
            }
            shuchu(pokers2);
            Console.ReadKey();
        }


        static void shuchu(Poker[] pokers2)
        {
            for(int i=1;i<12;i++)
            for(int j=0;j<12-i;j++)
                {
                    Poker p = new Poker();
                    if(pokers2[j].sum> pokers2[j+1].sum)
                    {
                        p = pokers2[j];
                        pokers2[j] = pokers2[j + 1];
                        pokers2[j + 1] = p;
                    }
                }
            Console.WriteLine("顺序输出");
            for (int i = 0; i < 12; i++)
                Console.WriteLine(pokers2[i].num + pokers2[i].hua);
        }




    }
   

}





用RNGCryptoServiceProvider,结果发现依然会有重复的值,所以只好用了一个数组a进行判断,初始化是零,如果用到了,就让他设为1.

猜你喜欢

转载自blog.csdn.net/qq_32354205/article/details/79920879