import random


def generate_mac_address(type="XEN"):
    """
    <comment-ja>
    ランダムなMACアドレスを生成する
    (XENなら、00:16:3e:00:00:00 から 00:16:3e:7f:ff:ff の範囲)
    (KVMなら、52:54:00:00:00:00 から 52:54:00:ff:ff:ff の範囲)

    @param type: ハイパーバイザのタイプ (XEN or KVM)
    @return: MACアドレス
    </comment-ja>
    <comment-en>
    Generate random MAC address
    (if hypervisor is XEN, generates address by range between 00:16:3e:00:00:00 and 00:16:3e:7f:ff:ff)
    (if hypervisor is KVM, generates address by range between 52:54:00:00:00:00 and 52:54:00:ff:ff:ff)

    @param type: The type of hypervisor (XEN or KVM)
    @return: The string that stands for MAC address
    </comment-en>
    """
    if type == "KVM":
        mac = [ 0x52, 0x54, 0x00,
                random.randint(0x00, 0xff),
                random.randint(0x00, 0xff),
                random.randint(0x00, 0xff) ]
    else:
        mac = [ 0x00, 0x16, 0x3e,
                random.randint(0x00, 0x7f),
                random.randint(0x00, 0xff),
                random.randint(0x00, 0xff) ]
    return ':'.join(["%02x" % x for x in mac])