您好,登錄后才能下訂單哦!
在項(xiàng)目開(kāi)發(fā)中,一般都會(huì)使用到“隨機(jī)數(shù)”,但是在DotNet中的隨機(jī)數(shù)并非真正的隨機(jī)數(shù),可在一些情況下生成重復(fù)的數(shù)字,現(xiàn)在總結(jié)一下在項(xiàng)目中生成隨機(jī)數(shù)的方法。
1.隨機(jī)布爾值:
/// <summary> /// 隨機(jī)布爾值 /// </summary> /// <param name="random"></param> /// <returns>隨機(jī)布爾值</returns> public static bool NextBoolean(this Random random) { if (random == null) { throw new ArgumentNullException("random"); } return random.NextDouble() > 0.5; }
2.指定枚舉類(lèi)型的隨機(jī)枚舉值:
/// <summary> /// 指定枚舉類(lèi)型的隨機(jī)枚舉值 /// </summary> /// <param name="random"></param> /// <returns>指定枚舉類(lèi)型的隨機(jī)枚舉值</returns> public static T NextEnum<T>(this Random random) where T : struct { var type = typeof(T); if (!type.IsEnum) { throw new InvalidOperationException(); } var array = System.Enum.GetValues(type); var index = random.Next(array.GetLowerBound(0), array.GetUpperBound(0) + 1); return (T)array.GetValue(index); }
3.隨機(jī)數(shù)填充的指定長(zhǎng)度的數(shù)組:
/// <summary> /// 隨機(jī)數(shù)填充的指定長(zhǎng)度的數(shù)組 /// </summary> /// <param name="random"></param> /// <param name="length">數(shù)組長(zhǎng)度</param> /// <returns>隨機(jī)數(shù)填充的指定長(zhǎng)度的數(shù)組</returns> public static byte[] NextBytes(this Random random, int length) { if (length < 0) { throw new ArgumentOutOfRangeException("length"); } var data = new byte[length]; random.NextBytes(data); return data; }
4. 數(shù)組中的隨機(jī)元素:
/// <summary> /// 數(shù)組中的隨機(jī)元素 /// </summary> /// <typeparam name="T">元素類(lèi)型</typeparam> /// <param name="random"></param> /// <param name="items">元素?cái)?shù)組</param> /// <returns>元素?cái)?shù)組中的某個(gè)隨機(jī)項(xiàng)</returns> public static T NextItem<T>(this Random random, T[] items) { return items[random.Next(0, items.Length)]; }
5.指定時(shí)間段內(nèi)的隨機(jī)時(shí)間值:
/// <summary> /// 指定時(shí)間段內(nèi)的隨機(jī)時(shí)間值 /// </summary> /// <param name="random"></param> /// <param name="minValue">時(shí)間范圍的最小值</param> /// <param name="maxValue">時(shí)間范圍的最大值</param> /// <returns>指定時(shí)間段內(nèi)的隨機(jī)時(shí)間值</returns> public static DateTime NextDateTime(this Random random, DateTime minValue, DateTime maxValue) { var ticks = minValue.Ticks + (long)((maxValue.Ticks - minValue.Ticks) * random.NextDouble()); return new DateTime(ticks); }
6. 隨機(jī)時(shí)間值:
/// <summary> /// 隨機(jī)時(shí)間值 /// </summary> /// <param name="random"></param> /// <returns>隨機(jī)時(shí)間值</returns> public static DateTime NextDateTime(this Random random) { return NextDateTime(random, DateTime.MinValue, DateTime.MaxValue); }
7.獲取指定的長(zhǎng)度的隨機(jī)數(shù)字字符串:
/// <summary> /// 獲取指定的長(zhǎng)度的隨機(jī)數(shù)字字符串 /// </summary> /// <param name="random"></param> /// <param name="length">要獲取隨機(jī)數(shù)長(zhǎng)度</param> /// <returns>指定長(zhǎng)度的隨機(jī)數(shù)字符串</returns> public static string GetRandomNumberString(this Random random, int length) { if (length < 0) { throw new ArgumentOutOfRangeException("length"); } char[] pattern = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; var result = ""; var n = pattern.Length; for (var i = 0; i < length; i++) { var rnd = random.Next(0, n); result += pattern[rnd]; } return result; }
8.獲取指定的長(zhǎng)度的隨機(jī)字母字符串:
/// <summary> /// 獲取指定的長(zhǎng)度的隨機(jī)字母字符串 /// </summary> /// <param name="random"></param> /// <param name="length">要獲取隨機(jī)數(shù)長(zhǎng)度</param> /// <returns>指定長(zhǎng)度的隨機(jī)字母組成字符串</returns> public static string GetRandomLetterString(this Random random, int length) { if (length < 0) { throw new ArgumentOutOfRangeException("length"); } char[] pattern = { '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' }; var result = ""; var n = pattern.Length; for (var i = 0; i < length; i++) { var rnd = random.Next(0, n); result += pattern[rnd]; } return result; }
9.獲取指定的長(zhǎng)度的隨機(jī)字母和數(shù)字字符串:
/// <summary> /// 獲取指定的長(zhǎng)度的隨機(jī)字母和數(shù)字字符串 /// </summary> /// <param name="random"></param> /// <param name="length">要獲取隨機(jī)數(shù)長(zhǎng)度</param> /// <returns>指定長(zhǎng)度的隨機(jī)字母和數(shù)字組成字符串</returns> public static string GetRandomLetterAndNumberString(this Random random, int length) { if (length < 0) { throw new ArgumentOutOfRangeException("length"); } char[] pattern = { '0', '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' }; var result = ""; var n = pattern.Length; for (var i = 0; i < length; i++) { var rnd = random.Next(0, n); result += pattern[rnd]; } return result; }
生成隨機(jī)數(shù)的方式較多,可根據(jù)具體的要求來(lái)生成對(duì)應(yīng)的隨機(jī)數(shù)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。