随机生成字符串中的字符

发布于 2024-09-15 17:06:05 字数 116 浏览 6 评论 0原文

当用户单击按钮时,我需要在字符串java中生成随机字符。 例如:如果我们以猫为例,我需要在字符串中显示字符,如下所示:

CAT,ACT,TAC,TCA

提前致谢

阿斯旺

i need to generate random character in a String java when user click on button.
for example :if we take cat example i need to disply character in a string like follwing:

CAT,ACT,TAC,TCA

Thanks in advance

Aswan

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

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

发布评论

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

评论(2

枕花眠 2024-09-22 17:06:05

关于Fisher-Yates shuffle算法

Fisher-Yates shuffle 是一种标准算法用于洗牌。这是伪代码:

To shuffle an array a of n elements:
   for i from n - 1 downto 0 do
         j ← random integer with 0 ≤ j ≤ i
         exchange a[j] and a[i]

这是一个简单的 Java 实现:

static String shuffled(String s) {
    char[] a = s.toCharArray();
    final int N = a.length;
    Random r = new Random();
    for (int i = N - 1; i >= 0; i--) {
        int j = r.nextInt(i + 1);
        swap(a, i, j);
    }
    return new String(a);
}
static void swap(char[] a, int i, int j) {
    char t = a[i];
    a[i] = a[j];
    a[j] = t;
}

然后您可以:

    String text = "stackoverflow";
    for (int i = 0; i < 10; i++) {
        System.out.println(shuffled(text));
    }

这将生成字符串 "stackoverflow" (另请参阅 ideone.com)。


Guava + Java Collections Framework 替代解决方案

如果您安装了 Guava 库,那么这将是一个不错的解决方案。以下是关键事实:

然后我们可以将两者结合起来得到以下干净可读的代码

import com.google.common.primitives.*;
import java.util.*;

public class AnagramCreator {
    public static void main(String[] args) {
        String text = "stackoverflow";
        char[] arr = text.toCharArray();
        List<Character> list = Chars.asList(arr);

        for (int i = 0; i < 10; i++) {
            Collections.shuffle(list);
            System.out.println(new String(arr));
        }
    }
}

:打印 “stackoverflow” 的 10 个字谜。

请注意,Guava 仅用于提供 List char[] 的实时视图char[] 不会自动装箱为 Character[](反之亦然),否则 Arrays.asList(T...)就足够了。

另请参阅

相关内容问题

On Fisher-Yates shuffling algorithm

Fisher-Yates shuffle is a standard algorithm for shuffling. Here's the pseudocode:

To shuffle an array a of n elements:
   for i from n - 1 downto 0 do
         j ← random integer with 0 ≤ j ≤ i
         exchange a[j] and a[i]

Here's a straightforward Java implementation:

static String shuffled(String s) {
    char[] a = s.toCharArray();
    final int N = a.length;
    Random r = new Random();
    for (int i = N - 1; i >= 0; i--) {
        int j = r.nextInt(i + 1);
        swap(a, i, j);
    }
    return new String(a);
}
static void swap(char[] a, int i, int j) {
    char t = a[i];
    a[i] = a[j];
    a[j] = t;
}

Then you can have:

    String text = "stackoverflow";
    for (int i = 0; i < 10; i++) {
        System.out.println(shuffled(text));
    }

This will generate 10 shuffling of the string "stackoverflow" (see also on ideone.com).


Guava + Java Collections Framework alternative solution

If you have Guava library installed, then this would be a nice solution. Here are the key facts:

We can then combine the two to get the following clean and readable code:

import com.google.common.primitives.*;
import java.util.*;

public class AnagramCreator {
    public static void main(String[] args) {
        String text = "stackoverflow";
        char[] arr = text.toCharArray();
        List<Character> list = Chars.asList(arr);

        for (int i = 0; i < 10; i++) {
            Collections.shuffle(list);
            System.out.println(new String(arr));
        }
    }
}

The above will print 10 anagrams of "stackoverflow".

Note that Guava is only used to provide the List<Character> live view of the char[]. A char[] does not get autoboxed to a Character[] (and vice versa), otherwise Arrays.asList(T...) would've been sufficient.

See also

Related questions

或十年 2024-09-22 17:06:05

嗨,谢谢大家,我终于找到了问题的解决方案。

public String RandomString(String word){

    int no=word.length();
    String temp="";
    String temp2=null;
     while(no>0){
         int genNo=ran.nextInt(word.length());
         if(temp2==null){
             temp2=""+genNo;
             temp=Character.toString(word.charAt(genNo));
             no--;
         }else{
            if(!temp2.contains(""+genNo)){
                temp2=temp2+""+genNo;
                temp=temp+Character.toString(word.charAt(genNo));
                no--;
            }
         }
     }

    if(!temp.equals(word)){
         Log.v("check","temp2 = "+temp2);
         Log.v("check","String = "+temp);
        return temp;
    }else{
        RandomGenerate(word);
    }
    return null;

}

Hi Thankq for all finally i got solution for my problem.

public String RandomString(String word){

    int no=word.length();
    String temp="";
    String temp2=null;
     while(no>0){
         int genNo=ran.nextInt(word.length());
         if(temp2==null){
             temp2=""+genNo;
             temp=Character.toString(word.charAt(genNo));
             no--;
         }else{
            if(!temp2.contains(""+genNo)){
                temp2=temp2+""+genNo;
                temp=temp+Character.toString(word.charAt(genNo));
                no--;
            }
         }
     }

    if(!temp.equals(word)){
         Log.v("check","temp2 = "+temp2);
         Log.v("check","String = "+temp);
        return temp;
    }else{
        RandomGenerate(word);
    }
    return null;

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