首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建具有特定要求的随机数字串

创建具有特定要求的随机数字串
EN

Stack Overflow用户
提问于 2016-01-08 02:30:15
回答 4查看 570关注 0票数 2

我想要创建一个随机的数字串。

  • 从0到9。
  • 10位长。
  • 第一个数字不能是0。
  • 其中一个数字必须在字符串中出现2次,而另一个则必须完全不存在。
  • 或者一个数字必须在那里三次,而另外两个数字根本不可能在那里。

为了使这一点更加清楚,这里有一些例子:

1223456789 - 10位,没有起始零,一位数(2)是2倍,一位数(0)根本不存在。

1000345678 - 10位数,没有起始零,一位数(0)有3次,两位数(2,9)根本没有。

使用startsWith -方法可以很容易地捕捉到起始零,但是我还没有找到检查其余部分的方法,而且我不太擅长regex,同时我也不完全确定您是否可以使用regex完成这个任务。

为了生成随机字符串本身,我使用了随机类和RandomStringUtils,这两个类都没有创建数字的限制。

你们中有谁知道如何做到这一点吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-01-08 03:12:29

其思想是:首先生成一个随机字符串,每个字符串以0-9开头,而不是以0开头,然后: 1.替换一个数字将另一个或2.将两个数字替换为另一个数字。

代码语言:javascript
复制
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;
    }

}
票数 0
EN

Stack Overflow用户

发布于 2016-01-08 03:32:30

假设您有10个袋子,每个袋子上都有相应的编号,从0到9,如下所示:

代码语言:javascript
复制
   .---.._                  
  {------'; 
   }====={    
 .´       '.   
/    .´|    \       inside there are
|      |    |  <--- stones with '1' engraved
\:    _|_   /
  -__    =.´

你也有一枚硬币可以在你的手上抛正面或反面。

代码语言:javascript
复制
       .------..-                  
     ´  . /___  `.`.
   ;   / /    ´}  ; ;     ______________________________ 
  :    "|'__'  //  : :   /                              |
  '    .|/__\. } \ ' '  /_    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个数字结束,相同数字的ͷ重复,不会以零开始,而你前面剩下的袋子只是沿途移除麻袋的一个副作用。

票数 1
EN

Stack Overflow用户

发布于 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.

  1. 创建一个LinkedList,然后向它添加数字1到9。
  2. 在0-8之间生成一个随机数(列表索引的范围),使用索引从列表中检索一个值(如删除它),然后将其添加到字符串中,这样第一个数字就不是0。
  3. 将0添加回列表,以便它可以在其他地方使用。
  4. 现在LinkedList中还剩下9个数字,第一个数字为非零,并且按照步骤2已经在字符串变量中了。从这里开始,在LinkedList索引范围内生成另一个随机数。无论这个数字是什么,从LinkedList中删除它,将它两次添加到ArrayList中。
  5. 现在LinkedList中还剩下8个数字,字符串中还有1个非零数字。和3个数字在ArrayList中,总共4个数字在您的序列被证实是正确的。你必须再得到6个号码才能完成它。到目前为止它看起来会是这样的。

String sequence => "4"

ArrayList beingBuilt => [2, 6, 6]

LinkedList available => [1, 3, 4, 5, 7, 8, 9, 0]

  1. 似乎你只能有10个数字,再通过LinkedList循环6次,使用随机数抽取一个随机索引,从LinkedList中删除它,将它添加到ArrayList中。

在此之后,ArrayList应该有9个数字,您可以对其进行洗牌,使其更随意,然后将其转换为字符串,并在序列的末尾附加到其中。你的规则现在应该得到满足。

为了使其更随意,您可以操纵如何从LinkedList中提取数字,以及您所拥有的最后一条规则,您可以将其更改得太简单。由于删除速度更快,我使用了一个LinkedList,我确实考虑过使用一个集合,但可能需要做更多的工作来处理映射到集合中实际存在的一个数字的随机数索引。

不过,只是一个想法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34668543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档