View Full Version : [JAVA] Stringa di numeri random di lunghezza x
Devo creare una stringa di numeri casuali di "x" cifre, ho provato cosi ma il metodo mi restituisce una stringa di numeri e lettere mentre io vorrei solo numeri:
public String randomString (int x) {
Random rnd = new Random ();
char[] arr = new char[x];
for (int i=0; i<x; i++) {
int n = rnd.nextInt (9);
arr[i] = (char) n;
}
return arr.toString();
}
morskott
06-12-2009, 15:25
Devo creare una stringa di numeri casuali di "x" cifre, ho provato cosi ma il metodo mi restituisce una stringa di numeri e lettere mentre io vorrei solo numeri:
public String randomString (int x) {
Random rnd = new Random ();
char[] arr = new char[x];
for (int i=0; i<x; i++) {
int n = rnd.nextInt (9);
arr[i] = (char) n;
}
return arr.toString();
}
public String randomString(int x){
Random rand=new Random();
String res="";
for (int i=0;i<x;i++){
res+=String.valueOf(rand.nextInt(9));
}
return res;
}
Cosė dovrebbe funzionare
e anche bene! grazie mille! devo dire che sto approfittando troppo di questo forum, ma sono troppo pigro!
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.