不重复随机数生成工具类

public static List<String> genInviteCode(HashMap<String, String> codes,int length, int num) {
		List<String> strList = new ArrayList<String> ();
		char[] d = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
				'U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
		int len = 35;

		Random random = new Random();
		while(num > 0) {
			int l = length;
			StringBuffer sb = new StringBuffer();
			while(l > 0) {
				sb.append(d[random.nextInt(len)]);
				l --;
			}
			String inviteCode = sb.toString();
 			if(codes.containsKey(inviteCode)) {
 				continue;
			}
			codes.put(inviteCode, "A");
			strList.add(inviteCode);
			num--;
		}
		return strList;
	}
	/**
     * 获取6895900个不能重复的邀请码
     *
     * @param list
     */
    private void creatAndCheckInviteCode(List<String> list) {
        //要发送的邀请码总数
        int count = 66666;
        HashMap<String, String> hash = new HashMap<String,String> ();
        while (count > 0) {
            List<String> inviteCodeList = RandomUtil.genInviteCode(hash, 5, 10);
            System.out.println(inviteCodeList.toString());
            list.addAll(inviteCodeList);
            count--;
        }
    }

	//每个用户发十个邀请码
	@PostMapping("index")
    private void index() {
        List<String> inviteCodes = new ArrayList<>();
        inviteCodes.add("1");
        Long addTime = System.currentTimeMillis();
        long l = 1570550399000L - addTime;
        creatAndCheckInviteCode(inviteCodes);
        if (inviteCodes != null && inviteCodes.size() > 0) {
            EntityWrapper<Organization> userEntityWrapper = new EntityWrapper<>();
            userEntityWrapper.eq("organization_type", 3);
            userEntityWrapper.eq("organization_state", 1);
            userEntityWrapper.eq("state", 1);
            userEntityWrapper.eq("group_id", 2);
            List<Organization> organizationList = organizationService.selectList(userEntityWrapper);
            int size = organizationList.size();
            for (int i = 0; i < size; i++) {
                List<InviteCodeCopy1> inviteCodeCopy1List = new ArrayList<>();
                int count = 0;
                Organization organization = organizationList.get(i);
                int orgId = organization.getOrganizationId();
                while (count <= 9) {
                    String ic = inviteCodes.get(i*10 + count);
                    InviteCodeCopy1 inviteCodeCopy1 = new InviteCodeCopy1(null, 2, ic, orgId, "", 7, "", BigDecimal.valueOf(0), null, addTime, l, 2, 1, 0L, 0L, "");
                    inviteCodeCopy1.setRemark("国庆特派");
                    inviteCodeCopy1.setCmsUserId(null);
                    inviteCodeCopy1List.add(inviteCodeCopy1);
                    System.out.println(inviteCodeCopy1.getOrganizationId());
                    count++;
                }
                inviteCodeCopy1Service.insertBatch(inviteCodeCopy1List);
            }
        }
    }

生成不重复的随机数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值