java - 随机不重复昵称怎么生成?
阿神
阿神 2017-04-17 17:46:14
0
5
1032

随机不重复昵称怎么生成?

阿神
阿神

闭关修行中......

répondre à tous(5)
伊谢尔伦
  1. HashSet détermine s'il est dupliqué

  2. UUID garanti de ne pas se répéter

Peter_Zhu

Vous pouvez ajouter des caractères chinois aléatoires, des chaînes anglaises aléatoires et des nombres aléatoires. S'ils sont répétés, les nombres suivants augmenteront dans l'ordre.

洪涛

test du paquet ;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
classe publique RandomChinese {

public static void main(String args[]) {
    HashSet<String> set = new HashSet<String>();
    for (int i = 0; i < 100; i++) {
        String chineseName = getRandomJianHan(3);
        if (!set.contains(chineseName)) {
            set.add(chineseName);
        }
    }
    Iterator<String> iterator = set.iterator();
    while (iterator.hasNext()) {
        System.err.print(iterator.next() + "\n");
    }
}
public static String getRandomJianHan(int len) {
    String ret = "";
    for (int i = 0; i < len; i++) {
        String str = null;
        int hightPos, lowPos; // 定义高低位
        Random random = new Random();
        hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
        lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
        byte[] b = new byte[2];
        b[0] = (new Integer(hightPos).byteValue());
        b[1] = (new Integer(lowPos).byteValue());
        try {
            str = new String(b, "GBK"); // 转成中文
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        ret += str;
    }
    return ret;
}

}

伊谢尔伦
/**
     * 生成交易号
     */
    public function generatePaymentId()
    {
        $i = rand(0, 9999);
        do {
            if (9999 == $i) {
                $i = 0;
            }
            $i++;
            $id = time() . str_pad($i, 4, '0', STR_PAD_LEFT);
            $row = (new Query())->from(self::tableName())->where(['id' => $id])->exists();
        } while ($row);
        return $id;
    }

Je ne commenterai pas le code ci-dessus

Ty80

Option 1, trouver un endroit et récupérer une bibliothèque de surnoms
Option 2, trouver une bibliothèque de vocabulaire chinois, artificiellement grande ou petite, combiner aléatoirement les deux mots

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal