各位朋友对" JavaScript挑战赛--随机颜色"的挑战代码

来源:互联网 发布:sql 某字段相等的数量 编辑:程序博客网 时间:2024/05/17 18:47

当天,也就是6月3日,我很高兴收到了来自江、的代码(QQ: 676596050,Blog:http://hi.baidu.com/jiang_yy_jiang )的代码。

在他博客中的原始代码见 http://hi.baidu.com/jiang_yy_jiang/blog/item/1494c5b3a961b5add8335a72.html。

对于他的代码,我整理如下:


/******
 * name: 江、
 * qq: 676596050
 * blog: http://hi.baidu.com/jiang_yy_jiang
 */
function GetColor(){
    var str = "0123456789abcdef", t = "";
    for (j = 0; j < 6; j++) {
        t = t + str.charAt(Math.random() * str.length);
    }
    return t;
}

 

 

 

一旦有新的代码,我将列出来,供各位javascript爱好者讨论,切磋,并评比



/****
         * name:shelly→水
         * QQ:49812643
         */
        function randomColor(){
            return "rgb(" + Math.round(Math.random() * 256) + "," + Math.round(Math.random() * 256) + "," + Math.round(Math.random() * 256) + ")";
        }

 


 

 

 

/****
         * name:我佛山人
         * qq:52739105
         * blog:
         */
        function randomColor_foshan(){
            return ((new Date()).getTime().toString(16).substr(-6));
           
        }

 

 

 

 

 

/****
         * name:匿名
         * qq:
         * blog:
         */
        function randomColor(){
            return 'rgb(' + [Math.random() * 256 | 0, Math.random() * 256 | 0, Math.random() * 256 | 0].join(',') + ')';
        }

 

 

 

 


 

 

/****
         * name:角蝰er
         * qq:16198008
         * blog:http://www.zerolone.com
         */
        function randomColor_zerolone(){
            return Math.random().toString(16).substring(2, 8);
        }

/*本人(FuDesign2008@163.com)经过测试,randomColor_zerolone 比我的函数要优秀,除了在WindosXP+FF的环境下;基于角蝰er的代码简洁,清晰,易读,在性能上更好,且是第一个,因此本人决定送出一本《jjavascript:The Definitive Guide》。因为角蝰er更喜欢中文版,所以送出中文版的。*/

 

/*基于治方(zm)(QQ:15586293)的道理,本人先暂停(只是暂停)书籍的送出,待资料查证确认后,再做决定。*/

由于我个人的疏忽和技术水平,向给我javascript爱好者致歉;尤其是角蝰er网友,为了保持公正和追求技术的完美,我不能送出书籍,非常非常抱歉!!!

治方(zm)(QQ:15586293)说的是正确的,在javascript中Math.random()的值域是[0,1)。参考资料见 http://objjob.phrogz.net/js/method/732

 

 

 



/****
     * name:我佛山人
     * qq:52739105
     * blog:
     */
    function randomColor_foshan_2(){
        return (new Date().getTime() & 0xffffff).toString(16);
    }

 

 

    
    /****
     * name:治方(zm)15586293
     * qq:15586293
     * blog:
     */
    function randomColor_zm(){
        var color = Math.round(Math.random() * 0xffffff).toString(16);
        (color == 0) && (color = "000000");
        return color;
    }



/****
     * name:角蝰er
     * qq:16198008
     * blog:http://www.zerolone.com
     */
    function randomColor_zerolone_2(){
        return (Math.random()+0.00000001).toString(16).substring(2, 8);
    }

 

 

    /****
     * name:我佛山人
     * qq:52739105
     * blog:
     */
    function randomColor_foshan_3(){
        var v = (new Date() & 0xffffff).toString(16), l = v.length;
        return l < 6 && (['00000','0000','000','00','0'][l - 1] + v) : v;
    }


 

原创粉丝点击