当前位置:网站首页>Random number generation of C #

Random number generation of C #

2022-04-23 17:56:00 It technology ape

​ Random numbers are the results of special randomized trials . Random numbers are used in different techniques of statistics , For example, when taking a representative sample from a statistical population , Or in the process of assigning experimental animals to different experimental groups , Or in Monte Carlo simulation, etc . There are many different ways to generate random numbers . These methods are called random number generators . The most important characteristic of random number is : The latter number it produces has nothing to do with the previous number . ​

 

  public class RandomHelper
    {
        // Random number object 
        private Random _random;
 
        #region  Constructors 
        /// <summary>
        ///  Constructors 
        /// </summary>
        public RandomHelper()
        {
            // Assign values to random number objects 
            this._random = new Random();
        }
        #endregion
 
        #region  Generates a random integer in a specified range 
        public int GetRandomInt(int minNum, int maxNum)
        {
            return this._random.Next(minNum, maxNum);
        }
        #endregion
 
        #region  Generate a 0.0 To 1.0 A random fraction of 
        public double GetRandomDouble()
        {
            return this._random.NextDouble();
        }
        #endregion
 
        #region  Sort an array randomly 
        public void GetRandomArray<T>(T[] arr)
        {
            // An algorithm for random sorting of arrays : Randomly choose two positions , Exchange the values in the two positions 
 
            // The number of exchanges , Here, the length of the array is used as the number of exchanges 
            int count = arr.Length;
 
            // Start swapping 
            for (int i = 0; i < count; i++)
            {
                // Generate two random number positions 
                int randomNum1 = GetRandomInt(0, arr.Length);
                int randomNum2 = GetRandomInt(0, arr.Length);
 
                // Define temporary variables 
                T temp;
 
                // Swap the values of two random number positions 
                temp = arr[randomNum1];
                arr[randomNum1] = arr[randomNum2];
                arr[randomNum2] = temp;
            }
        }
        #endregion

        #region  Randomly generate non repeating numeric strings 
        private int rep = 0;
        public string GenerateCheckCodeNum(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }
	#endregion
 
        
        #region  Randomly generate strings ( Mix of numbers and letters )
        public string GenerateCheckCode(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }
	#endregion
     

        #region  Randomly get... From the string , A specified number of strings 
        private string GetRandomCode(string allChar, int CodeCount)
        {
            //string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; 
            string[] allCharArray = allChar.Split(',');
            string RandomCode = "";
            int temp = -1;
            Random rand = new Random();
            for (int i = 0; i < CodeCount; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
                }

                int t = rand.Next(allCharArray.Length - 1);

                while (temp == t)
                {
                    t = rand.Next(allCharArray.Length - 1);
                }

                temp = t;
                RandomCode += allCharArray[t];
            }
            return RandomCode;
        }
        #endregion

      
 
 
    }

 

版权声明
本文为[It technology ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231753552350.html