Home >Backend Development >PHP Tutorial >Is there any good way to generate 100,000 unique 13-digit pure numeric strings?

Is there any good way to generate 100,000 unique 13-digit pure numeric strings?

WBOY
WBOYOriginal
2016-10-11 14:23:111288browse

Is there any good way to generate 100,000 unique 13-digit pure numeric strings

Reply content:

Is there any good way to generate 100,000 unique 13-digit pure numeric strings

Come one by one. Start with 13 zeros and add 1 at a time. . .

Can be generated by timestamp, or followed by a random number

<code>package com.xtl.demo;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * 生成20组不重复的13位数随机数
 * @author xiatianlong
 *
 * @date 2016年10月8日 上午11:44:57
 */
public class CreatRandomNumber {
    
    
    public static void main(String[] args) {
        Set<String> numberSet = new HashSet<String>();
        
        while(true){
            // 数量大于20条即结束
            if (numberSet.size()>20){
                break;
            }
            String randomNumber = createRandomNumber(13);
            numberSet.add(randomNumber);
        }
        
        for (String string : numberSet) {
            System.out.println(string);
        }
    }
    
    /**
     * 生成指定位数的随机数
     * @param length
     *             生成的位数
     * @return
     */
    public static String createRandomNumber(int length){
        String number ="";
        for (int i = 0; i<length; i++){
            int randomNumber = new Random().nextInt(10);
            number += randomNumber+"";
        }
        return number;
    }
}
</code>

I didn’t think much about performance. . .

You can encrypt any number of characters in one direction and add salt, just 13 bits.

Available in

python random.sample:

<code>random.sample(range(10**12, 10**13), 10**5)
</code>

python2 uses xrange, if you want to start from scratch use format.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn