有没有一种简单的方法可以在java中重新排列数字?

发布于 2024-11-08 12:24:14 字数 156 浏览 0 评论 0原文

基本上简而言之,我想取一个随机数:

73524896

我想要相同的数字,但像这样随机重新排列它:

46932857

有没有一种方法可以在java中轻松做到这一点?

Basically in a nut shell I want to take a random number say:

73524896

and I want to us the same number but randomly rearrange it like this:

46932857

Is there a way I can do this easily in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

分開簡單 2024-11-15 12:24:14

这是我最喜欢的解决方案。 (请注意,所有洗牌的可能性都是相同的。)

Random rnd = new Random();
int rndInt = rnd.nextInt(10000000);

String[] rndChars = ("" + rndInt).split("(?<=.)");
Collections.shuffle(Arrays.asList(rndChars));
String result = "";
for (String s : rndChars) result += s;
int shuffled = Integer.parseInt(result);

// Print original and shuffled
System.out.println(rndInt);
System.out.println(shuffled);

示例输出:

4769797
9497767

Here's my favorite solution. (Note that all shufflings are equally probable.)

Random rnd = new Random();
int rndInt = rnd.nextInt(10000000);

String[] rndChars = ("" + rndInt).split("(?<=.)");
Collections.shuffle(Arrays.asList(rndChars));
String result = "";
for (String s : rndChars) result += s;
int shuffled = Integer.parseInt(result);

// Print original and shuffled
System.out.println(rndInt);
System.out.println(shuffled);

Sample output:

4769797
9497767
不一样的天空 2024-11-15 12:24:14
//generate random number
String number = "73524896";

//put each digit in an element of a list
List<Character> numberList = new ArrayList<Character>();
for (char c : number.toCharArray()){
  numberList.add(c);
}

//shuffle
Collections.shuffle(numberList);

//output
String shuffledNumber = "";
for (Character c : numberList){
  shuffledNumber += c;
}
System.out.println(shuffledNumber);
//generate random number
String number = "73524896";

//put each digit in an element of a list
List<Character> numberList = new ArrayList<Character>();
for (char c : number.toCharArray()){
  numberList.add(c);
}

//shuffle
Collections.shuffle(numberList);

//output
String shuffledNumber = "";
for (Character c : numberList){
  shuffledNumber += c;
}
System.out.println(shuffledNumber);
情痴 2024-11-15 12:24:14

这将对您有所帮助:随机数 - 洗牌

不是完整的代码,只是链接中的片段

Random rgen = new Random();  // Random number generator
int[] cards = new int[52];  

//--- Initialize the array to the ints 0-51
for (int i=0; i<cards.length; i++) {
    cards[i] = i;
}

//--- Shuffle by exchanging each element randomly
for (int i=0; i<cards.length; i++) {
    int randomPosition = rgen.nextInt(cards.length);
    int temp = cards[i];
    cards[i] = cards[randomPosition];
    cards[randomPosition] = temp;
}

This will this help you : Random Numbers - shuffling.

Not a complete code, just a snippet from the link

Random rgen = new Random();  // Random number generator
int[] cards = new int[52];  

//--- Initialize the array to the ints 0-51
for (int i=0; i<cards.length; i++) {
    cards[i] = i;
}

//--- Shuffle by exchanging each element randomly
for (int i=0; i<cards.length; i++) {
    int randomPosition = rgen.nextInt(cards.length);
    int temp = cards[i];
    cards[i] = cards[randomPosition];
    cards[randomPosition] = temp;
}
彡翼 2024-11-15 12:24:14

将其转为字符串,获取字符数组,然后将字符数组随机化,然后将其转回字符串。

这可以让你随机化任何字符串,而不仅仅是数字。

turn it into a string, get the character array, then randomize the character array, and turn it back into a string.

that would let you randomize any string, not just numbers.

梦晓ヶ微光ヅ倾城 2024-11-15 12:24:14

我想说最简单的方法是将整数解析为字符串,重新排列字符串中的数字,然后将其解析回整数。

public int rearrange(int num){
  StringBuilder asStr = new StringBuilder(num + ""); //convert to string

  inplaceShuffle(asStr);


  return Integer.parseInt(asStr.toString());

}

I would say the easiest way to do this is to parse the integer to a String, rearrange the digit in the String then parse it back to integer.

public int rearrange(int num){
  StringBuilder asStr = new StringBuilder(num + ""); //convert to string

  inplaceShuffle(asStr);


  return Integer.parseInt(asStr.toString());

}
丢了幸福的猪 2024-11-15 12:24:14

(1) 将数字转换为字符串

(2) 使用标准的洗牌算法进行洗牌 (3) 将字符

转换回数字

(1) Convert the number to a string

(2) Use a standard shuffling algorithm to shuffle the characters

(3) Convert back to a number

雨轻弹 2024-11-15 12:24:14

Collections.shuffle() 接受一个列表并返回一个列表,其中有一个 toArray()Arrays.asList() 接受一个数组并返回一个列表。 String 有一个 toCharArray() 和一个接受 char 数组的构造函数。整数有 toString()valueOf()

Collections.shuffle() takes a list and returns a list, which has a toArray(). Arrays.asList() takes an array and returns a list. String has a toCharArray() and a constructor that accepts a char array. Integer has toString() and valueOf()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文