2 weikzhao0521 weikzhao0521 于 2015.05.28 15:58 提问

java 生成随机的6位字符 包括a-z A-Z 0-9

java 生成随机的6位字符 包括a-z A-Z 0-9

10个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.05.28 16:08
已采纳
 public static void main(String[] args) {
        String a = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] rands = new char[6]; 
        for (int i = 0; i < rands.length; i++) 
        { 
            int rand = (int) (Math.random() * a.length()); 
            rands[i] = a.charAt(rand); 
        } 
        for(int i=0;i<rands.length;i++){
            System.out.println(rands[i]);
        }
    }
weikzhao0521
weikzhao0521 System.out.println(new String(rands));
2 年多之前 回复
weikzhao0521
weikzhao0521 最后的循环可以改成
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.05.28 16:04

String s = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random r = new Random();
String result;
for (int i =0; i < 6; i++ )
{
int n = r.nextInt(62);
result += s.subString(n, 1);
}
result就是你要的。

kulaqwe
kulaqwe String s = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; Random r = new Random(); String result; for (int i =0; i < 6; i++ ) { int n = r.nextInt(62); result += s.subString(n, n+1); } 这个样子才对 楼主漏掉了
7 个月之前 回复
u012454084
u012454084   2015.05.28 16:10

定义一个62位长的数组存放这些字母数字,用随机函数随机生成一个整数,限制范围0到61,(自己看随机函数的api)
6次随机结果连接赋给一个字符串就行。
想要细致的分化,可以设置3个数组分别存放大小写字母及数字,这样你可以控制随机数产生的规则。

jiawei0418
jiawei0418   2015.05.28 16:16

再搞个hashset 把n放进去判断是否重样 可以搞出不重样的六位字符

yuyang199
yuyang199   2015.05.28 16:52

public String randSix(){
String []baseStr = {"1","2","3","4","5","6","7","8","9","0",
"a","b","c","d","e","A","B"};
String str="";
for(int i = 0 ;i<6;i++){
int index = ((int)(100*Math.random()))%baseStr.length;
System.out.println(index);
str += baseStr[index];
}
return str;
}

这个可以自己定义想要输出的字符串包含哪些,特殊符号都可以

weikzhao0521
weikzhao0521 很有用,新手学习了。。。
2 年多之前 回复
u012216727
u012216727   Ds   Rxr 2015.05.28 17:17

如果还包括汉字的话就需要Unicode编码了,用法和他们写的类似,你可以百度下。

cuiwei1026522829
cuiwei1026522829   Ds   Rxr 2015.05.28 22:31

随机生成
希望对你有用

vip545058
vip545058   2015.05.29 11:28

public static String generateWord(int digits,boolean onlyNumber) {
String[] beforeShuffle = null;
if(onlyNumber){
beforeShuffle = new String[] { "0","1","2", "3", "4", "5", "6", "7",

"8", "9"};
}else{
beforeShuffle = new String[] { "0","1","2", "3", "4", "5", "6", "7",

"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",

"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",

"W", "X", "Y", "Z" };
}

List list = null;
if(beforeShuffle.length list = new ArrayList(Arrays.asList(beforeShuffle));
while(list.size()<digits){
list.addAll(Arrays.asList(beforeShuffle));
}
}else{
list = Arrays.asList(beforeShuffle);
}

Collections.shuffle(list);

StringBuilder sb = new StringBuilder(digits);

for (int i = digits-1; i>=0; i--) {
sb.append(list.get(i));

}

return sb.toString();
}

public static void main(String[] args) {
System.out.println(generateWord(6, true));
}

w86440044
w86440044   2015.05.29 11:33

网上有很多现成的代码啊
package com.yuchao.javadoc;

/**

  • 生成6位的随机字符串
  • @author Jack.yu
  • */

    public class SixNumberRadom {

    /**

    • @param args */
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      String result="";
      for(int i=0;i<6;i++){
      int intVal=(int)(Math.random()*26+97);
      result=result+(char)intVal;
      }
      System.out.print(result);
      }

}

baidu_25310663
baidu_25310663   2015.05.29 19:24
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!