how to create a random generated alphabet in Java

佐手、 提交于 2021-02-08 12:18:51

问题


Looking to create a randomly generated alphabet for a substitution cipher. My idea was something like this.

        char randomChar = (char) (97 + r.nextInt(25));

However this will cause repetition of letters. Going through the char array and seeing if the letter is already present seems inefficient also.

edit: I was too vague in my request and see this now. Here is the full question I am trying to solve. The alphabet must also contain the space button character e.g ' '.

Write a Java program which converts (user entered) plain text to cipher text using a substitution cipher (in which plain text letters are randomly assigned to cipher text letters). Note that a Substitution Cipher replaces plaintext with cipher-text. The most common substitution ciphers replace single characters of plaintext with predefined single characters of cipher-text (e.g. the plain-text character `a' might be replaced by cipher text character 'q', 'b' might be replaced by 'x', 'c' by 'k' and so on). Each plain-text character should be replaced by a different cipher-text character. As part of your solution you must write and use at least the following functions/methods: (i) createCipher() which determines and returns the mapping from plain text to cipher text. Each plain text character ('a' .. 'z', ' ') must be randomly assigned a cipher-text character;


回答1:


I have thought of a solution, said solution works but is it efficient?

    public static char[] createCipher(char[] cipher) {
    char[] cipher = new char[27];
    int characterNumber = 97;
    cipher[0] = ' ';
    for(int counter = 1; counter < cipher.length;counter++)
    {
        char character = (char) characterNumber;
        cipher[counter] = character;
        characterNumber++;  
    }

    for(int counter = 0; counter < cipher.length;counter++)
    {
        int randomLocation = (int) (Math.random()*26);
        char temporaryCharacter = cipher[randomLocation];
        cipher[randomLocation] = cipher[counter];
        cipher[counter] = temporaryCharacter;
    }
    return cipher;

}



回答2:


To do a reshuffling of the alphabet (26 characters but in different order) you do

boolean[] b=new boolean[26];
  for(i=0; i<b.length; i++) b[i]=false;
 for(int counter = 0; counter < 26;counter++)
{
    int randomLocation = (int) (Math.random()*26);
    while(b[randomLocation]) randomLocation = (int) (Math.random()*26);
    b[randomLocation]=true;
    cipher[counter]=alphabet[randomLocation];
}  

forget about "efficient" and stuff first you solve the problem




回答3:


If you want to shuffle an alphabet you can use the Collections.shuffle(..) metode. Somethink like this:

public static void main(String[] args) {
    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char[] randomAlphabet = new char[alphabet.length];

    // Copy to a List
    List<Character> list = new ArrayList<Character>();
    for (char c : alphabet) {
        list.add(c);
    }

    // shuffle it       
    Collections.shuffle(list);

    // Copy it back to an array
    for (int i = 0; i < list.size(); i++) {
        randomAlphabet[i] = list.get(i);
    }

    System.out.print("Random alphabet: ");
    for (int i = 0; i < randomAlphabet.length; i++) {
        System.out.print(" " + randomAlphabet[i]);
    }
}

That gives this when I run it:

Random alphabet:  j b w q o c r f z k g n p a u s i d m y h v e l x t


来源:https://stackoverflow.com/questions/34569743/how-to-create-a-random-generated-alphabet-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!