我想要创建一个随机的数字串。
为了使这一点更加清楚,这里有一些例子:
1223456789 - 10位,没有起始零,一位数(2)是2倍,一位数(0)根本不存在。
1000345678 - 10位数,没有起始零,一位数(0)有3次,两位数(2,9)根本没有。
使用startsWith -方法可以很容易地捕捉到起始零,但是我还没有找到检查其余部分的方法,而且我不太擅长regex,同时我也不完全确定您是否可以使用regex完成这个任务。
为了生成随机字符串本身,我使用了随机类和RandomStringUtils,这两个类都没有创建数字的限制。
你们中有谁知道如何做到这一点吗?
发布于 2016-01-08 03:12:29
其思想是:首先生成一个随机字符串,每个字符串以0-9开头,而不是以0开头,然后: 1.替换一个数字将另一个或2.将两个数字替换为另一个数字。
import java.util.Random;
public class Main {
public static void main(String[] args) {
System.out.println(generateRandomString());
System.out.println(generateRandomString());
}
public static String generateRandomString() {
String alphabet = "0123456789";
String result = "";
Random random = new Random();
// build a random string construct will 0-9 and each digital appear once
for (int i = 0; i < 10; i++) {
int index = random.nextInt(alphabet.length());
if (i == 0) { // first cannot be 0
index = random.nextInt(alphabet.length() - 1) + 1;
}
String c = alphabet.substring(index, index + 1);
result += c;
alphabet = alphabet.replace(c, "");
}
return random.nextInt(2) == 0 ? shuffle1(random, result) : shuffle2(random, result);
}
// One of the digits has to be in the String 2 times and one has to not be there at all.
private static String shuffle1(Random random, String result) {
int from = random.nextInt(10);
int to = random.nextInt(9) + 1;
while (from == to) {
to = random.nextInt(9) + 1;
}
result = result.replace(result.substring(to, to + 1), result.substring(from, from + 1));
return result;
}
// One digit has to be there 3 times, and 2 other digits can not be there at all
private static String shuffle2(Random random, String result) {
int from = random.nextInt(10);
int to1 = random.nextInt(9) + 1;
int to2 = random.nextInt(9) + 1;
while (from == to1) {
to1 = random.nextInt(9) + 1;
}
while (from == to2 || to2 == to1) {
to2 = random.nextInt(9) + 1;
}
result = result.replace(result.substring(to1, to1 + 1), result.substring(from, from + 1));
result = result.replace(result.substring(to2, to2 + 1), result.substring(from, from + 1));
return result;
}
}发布于 2016-01-08 03:32:30
假设您有10个袋子,每个袋子上都有相应的编号,从0到9,如下所示:
.---.._
{------';
}====={
.´ '.
/ .´| \ inside there are
| | | <--- stones with '1' engraved
\: _|_ /
-__ =.´你也有一枚硬币可以在你的手上抛正面或反面。
.------..-
´ . /___ `.`.
; / / ´} ; ; ______________________________
: "|'__' // : : / |
' .|/__\. } \ ' ' /_ HEAD! You shall pick 3 |
' /"../ ' ' | stones from the 1st sack! |
; / \/ ͷ ; ; \____________________________/
`_/ ´ ´
" -------´-´ 首先,我们将决定是有3个重复数字还是2个重复数字。抛硬币决定!尾巴是3,头是2。我们将这个结果称为ͷ。
移除与0(0)有关的麻袋片刻。
现在从你面前的9袋随机袋中挑选ͷ(2或3)石头。记住,你不能从0开始,这就是为什么我们暂时删除它的原因!永远把你刚从麻袋线上挑出来的麻袋拿掉。你不能再从这个里挑了。把0(零)袋放回线上。
把你刚捡到的石头放在自己面前。把ͷ-1握在手里.
现在重复这一步骤,直到你手中有9块石头:
选择一个随机的袋子,从里面挑一块石头,然后拿在你的手里。把袋子从绳子上拿开。
在这个过程的最后,你将有9块石头在你的手中,一颗在你面前。把你手里的东西洗洗干净。把它们放在你面前的一条直线上,就在你前面的石头旁边。
你将以10个数字结束,相同数字的ͷ重复,不会以零开始,而你前面剩下的袋子只是沿途移除麻袋的一个副作用。
发布于 2016-01-08 03:34:49
先用规则来做你想要的,然后再构建剩下的规则,怎么样?
这里有一个可能的想法
使用第一条规则
One of the digits has to be in the String 2 times and one has to not be there at all.
String sequence => "4"
ArrayList beingBuilt => [2, 6, 6]
LinkedList available => [1, 3, 4, 5, 7, 8, 9, 0]
在此之后,ArrayList应该有9个数字,您可以对其进行洗牌,使其更随意,然后将其转换为字符串,并在序列的末尾附加到其中。你的规则现在应该得到满足。
为了使其更随意,您可以操纵如何从LinkedList中提取数字,以及您所拥有的最后一条规则,您可以将其更改得太简单。由于删除速度更快,我使用了一个LinkedList,我确实考虑过使用一个集合,但可能需要做更多的工作来处理映射到集合中实际存在的一个数字的随机数索引。
不过,只是一个想法
https://stackoverflow.com/questions/34668543
复制相似问题