随机 MAC地址生成,
全静态方法,直接调用!
using System;
/// <summary>
/// 随机MAC地址
/// </summary>
public static class RadomMac
{
private static string _sparChar = ":";
/// <summary>
/// 返回一个随机MAC地址
/// </summary>
/// <returns></returns>
public static string GetRadomMacAddress()
{
return GetMacChar() + GetMacChar() + _sparChar + GetMacChar() + GetMacChar() + _sparChar + GetMacChar() + GetMacChar() + _sparChar + GetMacChar() + GetMacChar() + _sparChar + GetMacChar() + GetMacChar() + _sparChar + GetMacChar() + GetMacChar();
}
/// <summary>
/// 返回一个随机MAC地址
/// </summary>
/// <param name="spar">分隔符</param>
/// <returns></returns>
public static string GetRadomMacAddress(string spar)
{
_sparChar = spar;
return GetRadomMacAddress();
}
/// <summary>
/// 获得随机MAC字符
/// </summary>
/// <returns></returns>
private static string GetMacChar()
{
Random r = new Random(GetRandomSeed());
if (r.Next(0, 2) == 1)
{
return Convert.ToChar(r.Next(65, 71)).ToString();
}
else
{
return r.Next(0, 9).ToString();
}
}
/// <summary>
/// 获得随机种子
/// </summary>
/// <returns></returns>
private static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider hyl = new System.Security.Cryptography.RNGCryptoServiceProvider();
hyl.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
}