java随机生日,java随机字符串生成和生日悖论

我需要编写一个随机字符串生成类,它从31个字符的字符集和一些字母表中生成7char字符串(省略了10 26-5,5个元音).简单的数学给出了一组31 ^ 7种可能的组合~275亿.

我对bday悖论有疑问,我进行了一些测试,重复数量呈指数级增长.

我可以做些什么来避免这种情况吗?

At 1 million, duplicates encountered till now = 19

At 2 million, duplicates encountered till now = 69

At 3 million, duplicates encountered till now = 157

At 4 million, duplicates encountered till now = 280

At 5 million, duplicates encountered till now = 470

At 6 million, duplicates encountered till now = 662

At 7 million, duplicates encountered till now = 896

At 8 million, duplicates encountered till now = 1185

At 9 million, duplicates encountered till now = 1500

At 10 million, duplicates encountered till now = 1823

At 11 million, duplicates encountered till now = 2204

At 12 million, duplicates encountered till now = 2584

At 13 million, duplicates encountered till now = 3020

At 14 million, duplicates encountered till now = 3527

At 15 million, duplicates encountered till now = 4110

At 16 million, duplicates encountered till now = 4683

At 17 million, duplicates encountered till now = 5284

At 18 million, duplicates encountered till now = 5919

At 19 million, duplicates encountered till now = 6611

At 20 million, duplicates encountered till now = 7343

At 21 million, duplicates encountered till now = 8095

At 22 million, duplicates encountered till now = 8858

At 23 million, duplicates encountered till now = 9707

At 24 million, duplicates encountered till now = 10547

At 25 million, duplicates encountered till now = 11452

At 26 million, duplicates encountered till now = 12399

At 27 million, duplicates encountered till now = 13356

At 28 million, duplicates encountered till now = 14393

At 29 million, duplicates encountered till now = 15369

At 30 million, duplicates encountered till now = 16436

以下是测试类:

import java.util.Set;

import org.apache.commons.lang3.RandomStringUtils;

import com.google.common.collect.Sets;

public class RandomUnivmylocaL {

public static void main(String[] argv) {

final int million = 1_000_000;

final int iterations = 30;

// 31 chars

final char[] charArr = new char[] { '1', '2', '3', '4', '5', '6', '7',

'8', '9', '0', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L',

'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' };

// System.out.println(charArr.length);

final Set set = Sets.newHashSetWithExpectedSize(

iterations * million);

for (int i = 0; i < iterations; i++) {

for (int j = 0; j < million; j++) {

final String univCode = RandomStringUtils.random(7, charArr);

set.add(univCode);

}

System.out.println("At " + (i + 1) + " million, " +

"duplicates encountered till now = " +

(((i + 1) * million) - set.size()));

}

System.out.println("done");

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值