コードコピーは次のとおりです。
文字列パスワード= randomutil.generatestring(10);
ソースコードは次のとおりです。
コードコピーは次のとおりです。
パッケージcom.javaniu.core.util;
java.util.randomをインポートします。
パブリッククラスのrandomutil {
public static final string allchar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final string retterchar = "abcdefghijkllmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public static final string numberChar = "0123456789";
/**
*固定長のランダム文字列を返します(上限と小文字と数字のみを含む)
*
* @paramの長さ
*ランダムな文字列長
* @returnランダム文字列
*/
public static string generatestring(int length){
stringbuffer sb = new StringBuffer();
RANDOM RANDOM = new Random();
for(int i = 0; i <length; i ++){
sb.append(allchar.charat(random.nextint(allchar.length())));
}
return sb.tostring();
}
/**
*固定された長さのランダムな純粋な文字列(上限および小文字のみを含む)を返します
*
* @paramの長さ
*ランダムな文字列長
* @returnランダム文字列
*/
public static string generatemixstring(int length){
stringbuffer sb = new StringBuffer();
RANDOM RANDOM = new Random();
for(int i = 0; i <length; i ++){
sb.append(allchar.charat(random.nextint(letterchar.length())));
}
return sb.tostring();
}
/**
*固定された長さのランダムな純粋な大文字の文字列を返します(大文字のみを含む)
*
* @paramの長さ
*ランダムな文字列長
* @returnランダム文字列
*/
public static string generateLowerString(int length){
return generatemixstring(length).tolowercase();
}
/**
*固定長のランダムな小文字の文字列を返します(大文字と小文字のみを含む)
*
* @paramの長さ
*ランダムな文字列長
* @returnランダム文字列
*/
public static string generateupperstring(int length){
return generatemixstring(length).touppercase();
}
/**
*固定長の純粋な0文字列を生成します
*
* @paramの長さ
*文字列長
* @return pure 0文字列
*/
public static string generatezerostring(int length){
stringbuffer sb = new StringBuffer();
for(int i = 0; i <length; i ++){
sb.append( '0');
}
return sb.tostring();
}
/**
*数値に基づいて固定長の文字列を生成すると、長さは前の文字列に追加されません。
*
* @param num
* 番号
* @param fixdlenth
*文字列長
* @return固定長さの文字列
*/
public static string tofixdlengthstring(long num、int fixdlenth){
stringbuffer sb = new StringBuffer();
string strnum = string.valueof(num);
if(fixdlenth -strnum.length()> = 0){
sb.append(generatezerostring(fixdlenth -strnum.length()));
} それ以外 {
新しいruntimeexception( "long" + num + "convert in" + fixdlenthを投げる
+「文字列で例外が発生しました!」);
}
sb.append(strnum);
return sb.tostring();
}
/**
*毎回生成されるレンディジットの数は異なります
*
* @param param
* @return固定長の数
*/
public static int getNotsimple(int [] param、int len){
ランダムrand = new Random();
for(int i = param.length; i> 1; i-){
int index = rand.nextint(i);
int tmp = param [index];
param [index] = param [i -1];
param [i -1] = tmp;
}
int result = 0;
for(int i = 0; i <len; i ++){
結果= result * 10 + param [i];
}
返品結果;
}
public static void main(string [] args){
system.out.println( "固定長のランダム文字列を返します(大文字と大文字と数字のみが含まれます):" + generatestring(10));
System.out
.println( "固定された長さのランダムな文字文字列を返します(上限と小文字の文字と小文字のみが含まれます):" + generatemixString(10));
system.out.println( "固定長のランダムな純粋な大文字の文字列(大文字と大文字のみを含む)を返します:」
+ generateLowerString(10));
system.out.println( "固定長のランダムな純粋な小文字の文字列(大文字と小文字のみを含む)を返します:"
+ generateupperstring(10));
system.out.println( "固定長の純粋な0文字列を生成します:" + generatezerostring(10));
system.out.println(「数字に基づいて固定長文字列を作成すると、長さは前のものに追加されるのに十分ではありません。」
+ tofixdlengthstring(123、10));
int [] in = {1、2、3、4、5、6、7};
System.out.println( "毎回生成されるレン桁の数は異なります。" + getNotsimple(in、3));
}
}
写真と真実があります:
Javaランダム文字サプリメント
今日、私はZuidaimaiでJavaランダムなキャラクター生成デモを見ましたが、これはたまたま使用されていましたが、不完全であることがわかりました。
コードコピーは次のとおりです。
public static void main(string [] args){
// string s = randomnum.getrandomnumstr(5);
//system.out.println(S);
System.out.println( "5文字を含む5文字列を生成します:");
randomnum.suijizifuchuan(5,5);
System.out.println( "6文字を含む3つの文字列を生成します:");
randomnum.suijizifuchuan(6,3);
System.out.println( "1〜10文字を含む1〜20本の文字列を生成します:");
randomnum.suijizifuchuan((int)(20*math.random())、(int)(10*math.random()));
System.out.println( "ランダム生成文字:");
int i = 0;
while(i <(int)(10*math.random())){
randomnum.suijizifuchuan((int)(20*math.random())、1);
i ++;
}
}
public static void suijizifuchuan(int x、int y){
for(int j = 0; j <y; j ++){
for(int i = 0; i <x; i ++){
int a =(int)(100*math.random()+100*math.random());
while(true){
if(a> 96&a <123)
壊す;
それ以外
a =(int)(100*math.random()+100*math.random());
}
System.out.print(((char)a);
}
System.out.println();
}
}
実行結果:
出典:http://www.zuidaima.com/share/1585762703215616.htm