关键代码:
using System;using System.Text;namespace YanZhiwei.DotNet2.Utilities.Common{ /// <summary> ///Random的帮助类 /// </summary> public static class RandomHelper { #region 生成设置范围内的Double的随机数 /// <summary> /// 生成设置范围内的Double的随机数 ///<para>eg:RandomHelper.NextDouble(1.5, 1.7);</para> /// </summary> /// <param name="miniDouble">生成随机数的最大值</param> /// <param name="maxiDouble">生成随机数的最小值</param> /// <returns>Double的随机数</returns> public static double NextDouble(double miniDouble, double maxiDouble) { Random _random = new Random((int)(DateTime.Now.Ticks)); return _random.NextDouble() * (maxiDouble - miniDouble) + miniDouble; } #endregion #region 生成随机字符串 /// <summary> /// 生成随机字符串 /// <para>RandomHelper.NetxtString(4, false);</para> /// </summary> /// <param name="size">字符串长度</param> /// <param name="lowerCase">字符串是小写</param> /// <returns>随机字符串</returns> public static string NetxtString(int size, bool lowerCase) { Random _random = new Random((int)(DateTime.Now.Ticks)); StringBuilder _builder = new StringBuilder(size); int _startChar = lowerCase ? 97 : 65;//65 = A / 97 = a for (int i = 0; i < size; i++) _builder.Append((char)(26 * _random.NextDouble() + _startChar)); return _builder.ToString(); } #endregion #region 0~9 A~Z字符串 /// <summary> /// 0~9 A~Z字符串 /// </summary> public static string RandomString_09AZ = "0123456789ABCDEFGHIJKMLNOPQRSTUVWXYZ"; #endregion #region 依据指定字符串来生成随机字符串 /// <summary> /// 依据指定字符串来生成随机字符串 /// <para>RandomHelper.NetxtString(RandomHelper.RandomString_09AZ, 4, false);</para> /// </summary> /// <param name="randomString">指定字符串</param> /// <param name="size">字符串长度</param> /// <param name="lowerCase">字符串是小写</param> /// <returns>随机字符串</returns> public static string NetxtString(string randomString, int size, bool lowerCase) { string _nextString = string.Empty; Random _random = new Random((int)(DateTime.Now.Ticks)); if (!string.IsNullOrEmpty(randomString)) { StringBuilder _builder = new StringBuilder(size); int _maxCount = randomString.Length - 1; for (int i = 0; i < size; i++) { int _number = _random.Next(0, _maxCount); _builder.Append(randomString[_number]); } _nextString = _builder.ToString(); } return lowerCase ? _nextString.ToLower() : _nextString.ToUpper(); } #endregion #region 生成随机时间 /// <summary> /// 生成随机时间 /// <para>RandomHelper.NextDateTime();</para> /// </summary> /// <returns>随机时间</returns> public static DateTime NextDateTime() { Random _random = new Random((int)(DateTime.Now.Ticks)); int _hour = _random.Next(2, 5); int _minute = _random.Next(0, 60); int _second = _random.Next(0, 60); string _dateTimeString = string.Format("{0} {1}:{2}:{3}", DateTime.Now.ToString("yyyy-MM-dd"), _hour, _minute, _second); DateTime _nextTime = Convert.ToDateTime(_dateTimeString); return _nextTime; } #endregion #region 生成随机MAC地址 /// <summary> /// 生成随机MAC地址 /// <para>RandomHelper.NextMacAddress();</para> /// </summary> /// <returns>新的MAC地址</returns> public static string NextMacAddress() { int _minValue = 0, _maxValue = 16; Random _random = new Random((int)(DateTime.Now.Ticks)); string _newMacAddress = string.Format("{0}{1}:{2}{3}:{4}{5}:{6}{7}:{8}{9}:{10}{11}", _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x"), _random.Next(_minValue, _maxValue).ToString("x") ).ToUpper(); return _newMacAddress; } #endregion }}
新闻热点
疑难解答