廉頗老矣,尚能飯否

          java:從技術到管理

          常用鏈接

          統計

          最新評論

          Java產生隨機數代碼【轉載】

          //根據不同的難度產生隨機字母和數字
          for(int i=0; readomNumStart.length()<4; i++) {
          if(difficult == 1) {

          //產生隨機的0-9的數字
          a = String.valueOf((int)(Math.random() * 10)) ;
          }
          if(difficult == 2) {

          //在0-9和a,b,c,d中隨機產生。
          String[] readomWord = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d"};
          int readomWordIndex = (int)(Math.random() * 13);
          a = readomWord[readomWordIndex];
          }
          if(difficult == 3) {

          //產生隨機的字母
          char readomLetter = (char)(Math.random ()*26+'a');
          a = String.valueOf(readomLetter) ;
          }
          if(difficult == 4) {

          //產生隨機的數字和字母的組合
          String[] readomHard = new String[20];
          int readomWordIndex = (int)(Math.random() * 19);
          for(int j=0; j<20; j++) {
          int readomWordNum = (int)(Math.random() * 10);
          char readomLetter = (char)(Math.random ()*26+'a');
          if(readomWordNum % 2 == 0) {
          readomHard[j] = readomWordNum + "";
          }else{
          readomHard[j] = String.valueOf(readomLetter);
          }
          }
          a = readomHard[readomWordIndex];
          }

          //
          char[] numbersAndLetters = ("0123456789" + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
          char[] randBuffer = new char[length];
          for (int i = 0; i < randBuffer.length; i++) {
          randBuffer[i] = numbersAndLetters[randGen.nextInt(numbersAndLetters.length)];
          }
          return new String(randBuffer);


          柳德才
          13691193654
          18942949207
          QQ:422157370
          liudecai_zan@126.com
          湖北-武漢-江夏-廟山

          posted on 2009-04-08 16:11 liudecai_zan@126.com 閱讀(277) 評論(0)  編輯  收藏 所屬分類: 程序人生

          主站蜘蛛池模板: 阳山县| 甘孜| 贵溪市| 恩施市| 吉隆县| 阿坝| 阜南县| 九龙坡区| 高青县| 无棣县| 民县| 河东区| 绵阳市| 苍溪县| 泗水县| 龙海市| 巍山| 盐亭县| 偏关县| 内黄县| 鄂托克前旗| 灵璧县| 得荣县| 寿阳县| 赣州市| 吉首市| 遂平县| 金塔县| 普兰县| 波密县| 临漳县| 元江| 砚山县| 黄平县| 隆安县| 驻马店市| 高淳县| 舒兰市| 安宁市| 马鞍山市| 桦川县|