SooHoo's recent timeline updates
SooHoo

SooHoo

V2EX member #189130, joined on 2016-08-28 16:11:45 +08:00
SooHoo's recent replies
@wdjwxh

图,我这看着还在,微博图床,试了好久,才发成功。😭
@spamss

没问题么。。。

Thread 调用的是 run
@mscststs 我只是来特意强调下,coding 就是 tx 的。 τ ...
屏蔽完了,搜索页还有内容吗 😂,直接 Google 多好
Jan 16, 2018
Replied to a topic by xfund4 PHP 如何生成固定长度唯一随机字符串?
@SooHoo

接上 ,没发完 不小心发出来了
----------
大概思想就是把 uid 放入到串里面,然后,随机插入 不包括 uid 的字符

为了让用户稍微难些分析。就加入了进制转换,然后随机插入位置。

可能有 BUG,欢迎指正。哈哈

-----------------------------------------



/**
*
* @param count 字符个数
* @param uid 用户 id
* @return
*/
public static String randomString(int count, int uid) {

String randomKey = CODE.substring(0, 20);
String numKey = CODE.substring(20);

StringBuilder result = new StringBuilder();

while (uid > 0) { //转成 numKey.length 进制
int p = uid % numKey.length();
result.append(numKey.substring(p, p + 1));
uid = uid / numKey.length();
}

if (result.length() < count) {//字数不足,随机字符补全
Random random = new Random();
int size = count - result.length();
for (int i = 0; i < size; i++) {
int r = random.nextInt(randomKey.length()); //随机取一个字符
int p = random.nextInt(result.length() + 1);//随机一个位置
result.insert(p, randomKey.substring(r, r + 1));
}
}
return result.toString();
}
Jan 16, 2018
Replied to a topic by xfund4 PHP 如何生成固定长度唯一随机字符串?
这两天公司业务需求,提现码(不能重复,不能被猜测到,不能太长,最多 6 位)
大概说下生成规则

先将数字小写字母大写字母打乱
然后左边取 20 个字符作为 randomKey
剩下右边给 numKey

将用户 id 进行 numKey.length 进制换算,生成字符串 result

长度不足 6 个,从 randomKey 随机取 6 个字符,插入 result 字符串 随机位置
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3006 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 13:43 · PVG 21:43 · LAX 06:43 · JFK 09:43
♥ Do have faith in what you're doing.