mercredi 12 décembre 2018

How do you generate random values between 0-z with Math.random?

Hello as the title says I want to generate a random text with only characters between 0 and a lowercase z. So far I am able to get values in an area and typecast them to a char which I then save in my array.

import javax.swing.JOptionPane;
class Versuch4{
public static void main(String[] args){
    char[] zeichen = new char [12];
    String symbol;

    for(int i=0;i<zeichen.length; i++){
        int [] zufall = new int [12];
        zufall[i] = (int)(33+Math.random()*(127-33)); //just used these numbers to test if it works.
        zeichen[i] = (char) zufall[i];
        System.out.print(zeichen[i]);
    }
  }
}

So I know how I can get completely random symbols which is what I am doing above and I know how I can get completely random numbers, but getting random values between both is what I am struggling with. I only saw past questions that used different methods, but for my task I need to use this.




Aucun commentaire:

Enregistrer un commentaire