邀请码生成器,java生成六位数N位数随机邀请码,不重复

直接贴代码 具体逻辑写代码里面了

运行结果如下
在这里插入图片描述

邀请码生成器,算法原理:

    1. 获取id: 1127738
    1. 使用自定义进制转为:gpm6
    1. 转为字符串,并在后面加’o’字符:gpm6o
    1. 在后面随机产生若干个随机数字字符:gpm6o7
  • 转为自定义进制后就不会出现o这个字符,然后在后面加个’o’,这样就能确定唯一性。最后在后面产生一些随机字符进行补全。

在线测试运行方法

https://tool.lu/coderunner/embed/aJf.html

package io.renren.modules.common.utils;

import java.util.Random;

/**
 * 邀请码生成器,算法原理:
 * 1) 获取id: 1127738
 * 2) 使用自定义进制转为:gpm6
 * 3) 转为字符串,并在后面加'o'字符:gpm6o
 * 4)在后面随机产生若干个随机数字字符:gpm6o7
 * 转为自定义进制后就不会出现o这个字符,然后在后面加个'o',这样就能确定唯一性。最后在后面产生一些随机字符进行补全。<br/>
 */
public class ShareCodeUtil {

    private static final char[] r=new char[]{'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z', 'x', '9', 'c', '7',
            'p', '5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v', 'y', 'l', 't', 'n', '6', 'b', 'g', 'h'
            ,'A','B','C','D','E','F','G','H','I','J','K','M','L','O','Q','P','R','S','T','U','V','W','X','Y','Z'};

    /** (不能与自定义进制有重复) */
    private static final char b='o';

    /** 进制长度 */
    private static final int binLen=r.length;

    /** 序列最小长度 */
    private static final int s=8;

    /**
     * 根据ID生成八位随机码
     * @param id ID
     * @return 随机码
     */
    public static String toSerialCode(long id) {
        char[] buf=new char[32];
        int charPos=32;

        while((id / binLen) > 0) {
            int ind=(int)(id % binLen);
            // System.out.println(num + "-->" + ind);
            buf[--charPos]=r[ind];
            id /= binLen;
        }
        buf[--charPos]=r[(int)(id % binLen)];
        // System.out.println(num + "-->" + num % binLen);
        String str=new String(buf, charPos, (32 - charPos));
        // 不够长度的自动随机补全
        if(str.length() < s) {
            StringBuilder sb=new StringBuilder();
            sb.append(b);
            Random rnd=new Random();
            for(int i=1; i < s - str.length(); i++) {
                sb.append(r[rnd.nextInt(binLen)]);
            }
            str+=sb.toString();
        }
        return str;
    }

    public static long codeToId(String code) {
        char chs[]=code.toCharArray();
        long res=0L;
        for(int i=0; i < chs.length; i++) {
            int ind=0;
            for(int j=0; j < binLen; j++) {
                if(chs[i] == r[j]) {
                    ind=j;
                    break;
                }
            }
            if(chs[i] == b) {
                break;
            }
            if(i > 0) {
                res=res * binLen + ind;
            } else {
                res=ind;
            }
            // System.out.println(ind + "--&gt;" + res);
        }
        return res;
    }


    public static void main(String[] args) {
        String s = toSerialCode(1);
        System.out.println(s);
        System.out.println(codeToId(s));
    }

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚蓝de笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值