随机MAC地址生成

来源:互联网 发布:企业在淘宝开店费用 编辑:程序博客网 时间:2024/05/18 09:37

随机 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);
    }
}