高性能(无需判重)批量生成优惠券码生成方案

生成验证工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xxxxDemo
{
    public class CouponCodeUtil
    {
        // 根据需要自行调整字符库顺序
        private static readonly char[] _codePool = { 'z', 'N', 'x', '5', 'h', 'g', 'W', 'b', 'S', '4', 'q', 'X', '7', 'K', 'L', '3', 'a', 'n', 'J', 'P', 'D', 't', 'Z', 'C', 'E', 'A', 'e', 'H', 'M', 'G', 'j', 'Q', 'V', 'Y', 'w', '8', 'f', '2', 'y', 'p', 'u', 'c', 'U', 's', 'd', 'k', 'm', 'B', 'r', 'v', 'T', '6', 'F', 'R', '9' };
        private static readonly int _poolSize = _codePool.Length;
        // 根据需要自行调整
        private static readonly int[] _weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        private static readonly int _weightSize = _weights.Length;

        public static string HexToStr(long i)
        {
            StringBuilder sb = new StringBuilder();
            while (i >= _poolSize)
            {
                long a = i % _poolSize;
                i /= _poolSize;
                sb.Append(_codePool[a]);
            }
            sb.Append(_codePool[i]);

            return sb.ToString();
        }

        public static string GenerateCode()
        {
            return HexToStr(GuidToLongID());
        }

        public static string GenerateCodeWithParityBit()
        {
            string code = GenerateCode();
            code += CalcParityBit(code);
            return code;
        }

        public static char CalcParityBit(string str)
        {
            int sum = 0;

            char[] parts = str.ToCharArray();
            for (int i = 0; i < parts.Length; i++)
            {
                char part = parts[i];
                int weight = _weights[i % _weightSize];
                sum += weight * part;
            }

            int position = sum % _poolSize;
            return _codePool[position];
        }

        public static bool Verify(string str)
        {
            string code = str.Substring(0, str.Length - 1);
            char parityBit = str[str.Length - 1];
            return CalcParityBit(code) == parityBit;
        }

        private static long GuidToLongID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }
    }
}

使用

do
{
    string coupon = CouponCodeUtil.GenerateCodeWithParityBit();

    Console.WriteLine(coupon);
    Console.WriteLine(CouponCodeUtil.Verify(coupon));
} while (true);

升级版

using System.Text;

namespace xxxxxx
{
    public class CouponCodeUtilV2
    {
        private static readonly char[] _codePool = { 'z', 'N', 'x', '5', 'h', 'g', 'W', 'b', 'S', '4', 'q', 'X', '7', 'K', 'L', '3', 'a', 'n', 'J', 'P', 'D', 't', 'Z', 'C', 'E', 'A', 'e', 'H', 'M', 'G', 'j', 'Q', 'V', 'Y', 'w', '8', 'f', '2', 'y', 'p', 'u', 'c', 'U', 's', 'd', 'k', 'm', 'B', 'r', 'v', 'T', '6', 'F', 'R', '9' };
        private static readonly int _poolSize = _codePool.Length;
        private static readonly int[] _weights = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        private static readonly int _weightSize = _weights.Length;

        public static string GenerateCode()
        {
            long uniqueId = BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0);

            StringBuilder codeBuilder = new StringBuilder();
            int sum = 0;
            int position = -1;

            char currentChar;
            int weight;
            while (uniqueId >= _poolSize)
            {
                long poolPosition = uniqueId % _poolSize;
                uniqueId /= _poolSize;
                currentChar = _codePool[poolPosition];
                codeBuilder.Append(currentChar);

                position++;
                weight = _weights[position % _weightSize];
                sum += weight * currentChar;
            }

            currentChar = _codePool[uniqueId];
            codeBuilder.Append(currentChar);

            position++;
            weight = _weights[position % _weightSize];
            sum += weight * currentChar;

            position = sum % _poolSize;
            codeBuilder.Append(_codePool[position]);

            return codeBuilder.ToString();
        }

        public static bool Verify(string code)
        {
            if (string.IsNullOrWhiteSpace(code) || code.Length < 2) return false;

            char[] parts = code.ToCharArray();
            int sum = 0;
            int weight;
            char part;

            for (int i = 0; i < parts.Length - 1; i++)
            {
                part = parts[i];
                weight = _weights[i % _weightSize];
                sum += weight * part;
            }

            int position = sum % _poolSize;
            return parts[parts.Length - 1] == _codePool[position];
        }
    }
}

使用

var code = CouponCodeUtilV2.GenerateCode();
var verify = CouponCodeUtilV2.Verify(code);
Console.WriteLine($"{code}\t{verify}");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值