當前位置:才華齋>IT認證>JAVA認證>

最全的java隨機數生成算法

JAVA認證 閲讀(2.48W)

java隨機數生成算法是怎麼樣的?下面yjbys小編為大家分享最新最全的java隨機數生成算法,希望對大家學習有所幫助!

最全的java隨機數生成算法

一個最全的隨機數的生成算法,最代碼的找回密碼的隨機數就是用的這個方法:

1 String password = rateString(10);

源碼如下:

001 package ;

002

003 import om;

004

005 public class RandomUtil {

006 public static finalString ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

007 public static finalString LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

008 public static final String NUMBERCHAR = "0123456789";

009

010 /**

011 * 返回一個定長的隨機字符串(只包含大小寫字母、數字)

012 *

013 * @param length

014 * 隨機字符串長度

015 * @return 隨機字符串

016 */

017 public static String generateString(int length) {

018 StringBuffer sb = new StringBuffer();

019 Random random = new Random();

020 for (int i = 0; i < length; i++) {

021 nd(At(Int(th())));

022 }

023 return ring();

024 }

025

026 /**

027 * 返回一個定長的`隨機純字母字符串(只包含大小寫字母)

028 *

029 * @param length

030 * 隨機字符串長度

031 * @return 隨機字符串

032 */

033 public static String generateMixString(int length) {

034 StringBuffer sb = new StringBuffer();

035 Random random = new Random();

036 for (int i = 0; i < length; i++) {

037 nd(At(Int(th())));

038 }

039 return ring();

040 }

041

042 /**

043 * 返回一個定長的隨機純大寫字母字符串(只包含大小寫字母)

044 *

045 * @param length

046 * 隨機字符串長度

047 * @return 隨機字符串

048 */

049 public static String generateLowerString(int length) {

050 return generateMixString(length)。toLowerCase();

051 }

052

053 /**

054 * 返回一個定長的隨機純小寫字母字符串(只包含大小寫字母)

055 *

056 * @param length

057 * 隨機字符串長度

058 * @return 隨機字符串

059 */

060 public static String generateUpperString(int length) {

061 return generateMixString(length)。toUpperCase();

062 }

063

064 /**

065 * 生成一個定長的純0字符串

066 *

067 * @param length

068 * 字符串長度

069 * @return 純0字符串

070 */

071 public static String generateZeroString(int length) {

072 StringBuffer sb = new StringBuffer();

073 for (int i = 0; i < length; i++) {

074 nd('0');

075 }

076 return ring();

077 }

078

079 /**

080 * 根據數字生成一個定長的字符串,長度不夠前面補0

081 *

082 * @param num

083 * 數字

084 * @param fixdlenth

085 * 字符串長度

086 * @return 定長的字符串

087 */

088 public static String toFixdLengthString(long num, int fixdlenth) {

089 StringBuffer sb = new StringBuffer();

090 String strNum = eOf(num);

091 if (fixdlenth - th() >= 0) {

092 nd(generateZeroString(fixdlenth - th()));

093 } else {

094 throw new RuntimeException("將數字" + num + "轉化為長度為" + fixdlenth

095 + "的字符串發生異常!");

096 }

097 nd(strNum);

098 return ring();

099 }

100

101 /**

102 * 每次生成的len位數都不相同

103 *

104 * @param param

105 * @return 定長的數字

106 */

107 public static int getNotSimple(int[] param, int len) {

108 Random rand = new Random();

109 for (int i = th; i > 1; i--) {

110 int index = Int(i);

111 int tmp = param[index];

112 param[index] = param[i - 1];

113 param[i - 1] = tmp;

114 }

115 int result = 0;

116 for (int i = 0; i < len; i++) {

117 result = result * 10 + param[i];

118 }

119 return result;

120 }

121

122 public static void main(String[] args) {

123 tln("返回一個定長的隨機字符串(只包含大小寫字母、數字):"+ generateString(10));

124

125 tln("返回一個定長的隨機純字母字符串(只包含大小寫字母):"+ generateMixString(10));

126 tln("返回一個定長的隨機純大寫字母字符串(只包含大小寫字母):"

127 + generateLowerString(10));

128 tln("返回一個定長的隨機純小寫字母字符串(只包含大小寫字母):"

129 + generateUpperString(10));

130 tln("生成一個定長的純0字符串:" + generateZeroString(10));

131 tln("根據數字生成一個定長的字符串,長度不夠前面補0:"

132 + toFixdLengthString(123, 10));

133 int[] in = { 1, 2, 3, 4, 5, 6, 7 };

134 tln("每次生成的len位數都不相同:" + getNotSimple(in, 3));

135 }

136 }