首页 > 其他分享 >高性能(无需判重)批量生成优惠券码生成方案

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

时间:2022-10-10 14:00:47浏览次数:55  
标签:code 判重 weight int sum 生成 优惠券 currentChar position

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}");

标签:code,判重,weight,int,sum,生成,优惠券,currentChar,position
From: https://www.cnblogs.com/linyijia/p/16775473.html

相关文章

  • 富文本插件quill生成内容后,字体样式未生效
    后台返回的富文本显示的时候字体大小无变化或不居中使用uniapp或者vue都可以使用一下方式解决场景描述:1、在富文本编辑器内,对文字进行了排版,以下为文字居中样式2、在页面......
  • 生成https license
     方法一:1.key的生成 1openssl genrsa -des3 -out server.key 2048这样是生成rsa私钥,des3算法,openssl格式,2048位强度。server.key是密钥文件......
  • 分布式唯一id生成器
    分布式唯一ID要求唯一性:生成的ID全局唯一,在特定范围内冲突概率极小。有序性:生成的ID按某种规则有序,便于数据库插入及排序递增可用性:可保证高并发下的可用性,确保任何......
  • Mybatis——plus 代码生成器
    MybatisPlus 给我们提供了更加强大的代码生成器  ## 代码生成器的简单的对比  MybatisPlus 给我们提供的代码生成器,不仅仅可以生成dao层,还可以生成 Service层,Cont......
  • Jmeter使用beanshell加密,调用AES代码,生成jar包
    工作中需要对接口进行AES加密,找开发要来了加密的代码(如下),记录下具体的使用方法:新建一个AESUtil包,在里面新建一个类(建议类的名字也为AESUtil)。把下面的代码复制进去,注意,......
  • mysql 查询生成对账单 当前数据为上条数据累计
    需求思路:期初数据做个对账开始时间之前的数据,查询时以时间为排序条件查询分别做出收款核销期初等基础数据SELECT billdate,CASE WHENtradetypecode='D2'......
  • uniapp 使用 qrcode.js 生成二维码只显示一次
    使用qrcode.js生成二维码canvas画布不能用v-if要用v-show......
  • 一个浏览器上面呈现的内容 是怎么生成的呢?
     浏览器的渲染过程:浏览器从服务器下载完文件后,就要对其进行解析和渲染.HTMLParser将HTML解析转换成DOM树.CSSParser将样式表解析转换成CSS规则树.转换完成后的DOM树......
  • 分布式主键生成设计策略
    1写作目的这几天被虾皮裁员的消息刷屏了,实话实说互联网的行情确实很差,各处都在裁员。而且最近在忙试用期转正答辩,还不错,光荣的成为了一个大厂的正式员工,很庆幸在这么不稳定......
  • spring boot项目使用mybatis-plus代码生成实例
    前言mybatis-plus官方地址https://baomidou.commybatis-plus是mybatis的增强,不对mybatis做任何改变,涵盖了代码生成,自定义ID生成器,快速实现CRUD,自动分页,逻辑删除等功能......