android产生8位16进制的随机数
/*产生8位16进制的数*/
public static String getRandomValue() {
String str = "";
for (int i = 0; i < 8; i++) {
char temp = 0;
int key = (int) (Math.random() * 2);
switch (key) {
case 0:
temp = (char) (Math.random() * 10 + 48);//产生随机数字
break;
case 1:
temp = (char) (Math.random()*6 + 'a');//产生a-f
break;
default:
break;
}
str = str + temp;
}
return str;
}
/*产生numSize位16进制的数*/
public static String getRandomValue(int numSize) {
String str = "";
for (int i = 0; i < numSize; i++) {
char temp = 0;
int key = (int) (Math.random() * 2);
switch (key) {
case 0:
temp = (char) (Math.random() * 10 + 48);//产生随机数字
break;
case 1:
temp = (char) (Math.random()*6 + 'a');//产生a-f
break;
default:
break;
}
str = str + temp;
}
return str;
}
public static String getRandomValue(int numSize) {
String str = "";
for (int i = 0; i < numSize; i++) {
char temp = 0;
int key = (int) (Math.random() * 2);
switch (key) {
case 0:
temp = (char) (Math.random() * 10 + 48);//产生随机数字
break;
case 1:
temp = (char) (Math.random()*6 + 'a');//产生a-f
break;
default:
break;
}
str = str + temp;
}
return str;
}
本文介绍了一种在Android环境中生成指定长度16进制随机数的方法,提供了两个实用函数,分别用于生成固定8位及任意指定长度的16进制随机字符串。这些函数通过组合数字与字母'a'到'f'来确保生成的字符串符合16进制格式。

1168

被折叠的 条评论
为什么被折叠?



