廉頗老矣,尚能飯否

          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)  編輯  收藏 所屬分類: 程序人生

          主站蜘蛛池模板: 时尚| 弋阳县| 三明市| 屯门区| 景德镇市| 石首市| 东兰县| 略阳县| 清原| 金川县| 鹤山市| 宁陵县| 定安县| 和龙市| 文昌市| 额济纳旗| 砀山县| 喜德县| 武强县| 连平县| 乌兰察布市| 钦州市| 扎赉特旗| 霍邱县| 余庆县| 安国市| 桦南县| 陆河县| 浦县| 汾阳市| 商城县| 湖南省| 荔浦县| 寿光市| 沂水县| 辽阳市| 孝义市| 甘洛县| 淮南市| 临洮县| 常宁市|