Home > OS >  Java random letter generator
Java random letter generator

Time:11-03

I have a task to create a small program in Java that will generate random 5 combinations of small and capital Latin letters each time it runs. Like aA, Ac, Ml, Bc, Ii; should be the result.

After running to code every time, it generates only 1 small letter, "z". I have written the code using the requirements and I cannot use other techniques like random. and other operators. Any clues on how to fix this?

import java.lang.String.*;

class CopyOfP3_3_1 {
    
    public static void main(String args[]) {
        char mb; mb = 0;
        String symbolRow; symbolRow = " ";
        byte msi = 0X60;
        byte bs = 26;
        System.out.println();
        for (byte i = 1; i <= bs; i  ) {
            msi = (byte)(msi   1);
            mb = (char)msi;
            symbolRow = symbolRow   mb   ' ';
        }
    
        System.out.println("Latin alphabet letter: ");
        System.out.print(mb   "\n");
    } 
}  

Best regards, Sandis

CodePudding user response:

you can use Math.random()

public static void genrateRandomNum(int count) {
        for (int i = 0; i < count; i  ) {
            int rnd1 = (int) (Math.random() * 26);
            int rnd2 = (int) (Math.random() * 52);

            char char1 = (char) ('A'   rnd1 % 26);
            char char2 = (char) ('a'   rnd2);

            System.out.println(char1   ""   char2);
        }
    }

    public static void main(String[] args) {
        genrateRandomNum(5);
    }
  •  Tags:  
  • java
  • Related