Non-repetitive random number scheme 1:1531 ms
The general idea is to create an array of 1-3000 first, each time, and then let the array reduce one, take one, reduce one, so you can never repeat.
<textarea id="runcode10389"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/> <title> random repeat _www.jb51.net</title> <script type=" Text/javascript "> var count= 3000; var original=new array;//The original array//to the original array original assignment for (Var i=0;i<count;i++) {original[i]=i+1; var d1=new Date (). GetTime (); for (i=0;i<count;i++) {var index=math.floor (Math.random () *original.length);//randomly take a position document.write (index+ ","); Print Original.splice (index,1)} var d2=new Date (). GetTime (); document.write ("Operation time consuming" + (D2-D1)); </script> </pead> <body> Cloud Community </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
non-repetitive random number scheme 2:297 MS
But scenario 1 uses the slice method, which regenerates the array, consumes memory and CPU operations, and is inefficient, so improve, take a number out of the original array, and then assign the position of the original array to null. So the next time you take a number, you can judge if it is null until it is null. After testing, it can significantly improve efficiency.
<textarea id="runcode2103"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/> <title> random do not repeat _ cloud Habitat Community _www.jb51.net</title> <script type=" Text/javascript "> var count=3000; var original=new array;//The original array//to the original array original assignment for (Var i=0;i<count;i++) {original[i]=i+1; var d1=new Date (). GetTime (); for (Var num,i=0;i<count;i++) {do{Num=math.floor (math.random () *count); }while (Original[num]==null); document.write (original[num]+ ","); Original[num]=null; var d2=new Date (). GetTime (); document.write ("Operation time consuming" + (D2-D1)); </script> </pead> <body> Cloud Community </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
non-repetitive random number scheme 3:234 MS
To change the idea, if the original array origin scattered, and then print,
This can also be done randomly and never repeat, and more efficient,
Because scenario 2 runs farther back, the higher the frequency of the original array being null, the more times the operation will be.
<textarea id="runcode80077"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/> <title> random repeat _www.jb51.net</title> <script type=" Text/javascript "> var count= 3000; var original=new array;//The original array//to the original array original assignment for (Var i=0;i<count;i++) {original[i]=i+1; var d1=new Date (). GetTime (); Original.sort (function () {return 0.5-math.random ();}); for (Var i=0;i<count;i++) {document.write (original[i]+ ","); var d2=new Date (). GetTime (); document.write ("Operation time consuming" + (D2-D1)); </script> </pead> <body> Cloud Community </body> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]