Here You can find how to generate random number in java.
Step-1:
Create Class RandomString.java
Step-2:
Copy and paste following code to your Class.
Step-3:
Now you can use generate(int length) method to create random string.(i.e. 'int length' parameter is use for random number length)
Step-1:
Create Class RandomString.java
Step-2:
Copy and paste following code to your Class.
Step-3:
Now you can use generate(int length) method to create random string.(i.e. 'int length' parameter is use for random number length)
package com.test;
import static java.lang.Math.round;
import static java.lang.Math.random;
import static java.lang.Math.pow;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static org.apache.commons.lang.StringUtils.leftPad;
public class RandomString {
public static String generate(int length) {
StringBuffer sb = new StringBuffer();
for (int i = length; i > 0; i -= 12) {
int n = min(12, abs(i));
sb.append(leftPad(Long.toString(round(random() * pow(36, n)), 36), n, '0'));
}
System.out.println("Random String: "+sb.toString());
return sb.toString();
}
}






0 comments:
Post a Comment