java之家

          開發心得

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            3 隨筆 :: 1 文章 :: 1 評論 :: 0 Trackbacks

          我是先用math類的random方法生成數字的隨機數,再將某些位的數字隨機替換成大小寫的字母,這個方法比較麻煩,哪位有更好的實現方法?  
            關注ing.......

           

          如果用你的這個方法可以生成你滿意的隨機碼,那么這個方法就已經是最好  
            的了。但如果結果不是令人滿意的話,可以考慮別的算法。

           

          import   java.util.*;  
            public   class   test   {  
                public   bbb()   {  
                }  
                public   static   void   main(string[]   args)   {  
                    test   test1   =   new   test();  
                    system.out.println(math.abs(new   random().nextint())%100000000);  
                }  
            }

          發表者:wellsoon

           
                  long   nowtime   =   new   java.util.date().gettime();    
            //取當前的時間作為一個隨機的參數  
             
                    string   rodomtext=   omd5.getmd5ofstr(string.valueof(nowtime)+"rodom").substring(0,8);//md5后再取前8位.  
             
            這個字符串的隨機性   非常高了。  
             
             
             
           

          發表者:caina3

          /**  
                  *   方法說明:  
                  *       該函數獲得隨機數字符串  
                  *       @param       ilen   int   :需要獲得隨機數的長度  
                  *       @param       itype   int:隨機數的類型:0:表示僅獲得數字隨機數;1:表示僅獲得字符隨機數;2:表示獲得數字字符混合隨機數  
                  *       @since   1.0.0  
                  */  
                public   static   final   string   createradom(int   ilen,int   itype){  
                    string   strrandom="";//隨機字符串  
                    random   rnd   =   new   random();  
                    if   (ilen<0){  
                        ilen=5;  
                    }  
                    if   ((itype>2)   ||(itype<0)){  
                        itype=2;  
                    }  
                    switch   (itype)   {  
                        case   0   :  
                            for   (int   iloop=0;iloop<ilen;iloop++){  
                                strrandom   +=   integer.tostring(rnd.nextint(10));  
                            }  
                            break;  
                        case   1   :  
                            for   (int   iloop=0;iloop<ilen;iloop++){  
                                strrandom   +=   integer.tostring((35-rnd.nextint(10))   ,36);  
                            }  
                            break;  
                        case   2   :  
                            for   (int   iloop=0;iloop<ilen;iloop++){  
                                strrandom   +=   integer.tostring(rnd.nextint(36)   ,36);  
                            }  
                            break;  
                    }  
                    return   strrandom;  
                }

          posted on 2007-12-14 09:52 java fan nb 閱讀(241) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 巴楚县| 得荣县| 泸州市| 简阳市| 四会市| 浙江省| 额敏县| 长汀县| 凤翔县| 恭城| 高安市| 南郑县| 全椒县| 当雄县| 内丘县| 育儿| 任丘市| 桦南县| 乌拉特前旗| 田东县| 岑巩县| 平塘县| 高安市| 太保市| 渝北区| 湖北省| 石家庄市| 永昌县| 大埔县| 南通市| 彭阳县| 沾化县| 哈巴河县| 丹棱县| 尚义县| 肥东县| 定襄县| 金门县| 曲靖市| 阿图什市| 涿鹿县|