Is there functionality to generate a random character in Java?

前端 未结 18 2030
难免孤独
难免孤独 2020-11-28 06:00

Does Java have any functionality to generate random characters or strings? Or must one simply pick a random integer and convert that integer\'s ascii code to a character?

相关标签:
18条回答
  • 2020-11-28 06:21

    Here is the code to generate random alphanumeric code. First you have to declare a string of allowed characters what you want to include in random number.and also define max length of string

     SecureRandom secureRandom = new SecureRandom();
     String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        StringBuilder generatedString= new StringBuilder();
        for (int i = 0; i < MAXIMUM_LENGTH; i++) {
            int randonSequence = secureRandom .nextInt(CHARACTERS.length());
            generatedString.append(CHARACTERS.charAt(randonSequence));
        }
    

    Use toString() method to get String from StringBuilder

    0 讨论(0)
  • 2020-11-28 06:23

    Take a look at Java Randomizer class. I think you can randomize a character using the randomize(char[] array) method.

    0 讨论(0)
  • 2020-11-28 06:23

    In fact mentioned methods don't generate real random char. To generate real random char you should give it a random seed! in example time in millisecond. this code generate 10 random char and then Convert it to String:

    import java.util.Random;
    public class MyClass {
        public static void main() {
    
         String randomKey;
    
        char[] tempArray={0,0,0,0,0,0,0,0,0,0};  //ten characters
    
        long seed=System.currentTimeMillis();
        Random random=new Random(seed);
        for (int aux=0; aux<10;aux++){
    
            tempArray[aux]=(char) random.nextInt(255);
            System.out.println(tempArray[aux]);
        }
    
        randomKey=String.copyValueOf(tempArray);  
    
    
          System.out.println(randomKey);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:25

    There are many ways to do this, but yes, it involves generating a random int (using e.g. java.util.Random.nextInt) and then using that to map to a char. If you have a specific alphabet, then something like this is nifty:

        import java.util.Random;
    
        //...
    
        Random r = new Random();
    
        String alphabet = "123xyz";
        for (int i = 0; i < 50; i++) {
            System.out.println(alphabet.charAt(r.nextInt(alphabet.length())));
        } // prints 50 random characters from alphabet
    

    Do note that java.util.Random is actually a pseudo-random number generator based on the rather weak linear congruence formula. You mentioned the need for cryptography; you may want to investigate the use of a much stronger cryptographically secure pseudorandom number generator in that case (e.g. java.security.SecureRandom).

    0 讨论(0)
  • 2020-11-28 06:27

    If you don't mind adding a new library in your code you can generate characters with MockNeat (disclaimer: I am one of the authors).

    MockNeat mock = MockNeat.threadLocal();
    
    Character chr = mock.chars().val();
    Character lowerLetter = mock.chars().lowerLetters().val();
    Character upperLetter = mock.chars().upperLetters().val();
    Character digit = mock.chars().digits().val();
    Character hex = mock.chars().hex().val(); 
    
    0 讨论(0)
  • 2020-11-28 06:29
    public static void  main(String[] args) {
    
      //  System.out.println("Enter a number to changeit at char  ");
        Random random = new Random();
    
        int x = random.nextInt(26)+65;    //0  to 25
        System.out.println((char)x);
    }
    
    0 讨论(0)
提交回复
热议问题