. 1 <Script Language = "JavaScript"> 2 function randomString (len) { . 3 len = len || 32 ; . 4 var $ chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; / * *** default removed confusing characters oOLl, 9gq, Vv , the Uu, I1 *** * / . 5 var maxPos = $ chars.length; . 6 var pwd = '' ; . 7 for (I = 0; I <len; I ++ ) { . 8 pwd + = $ chars.charAt (the Math. Floor (Math.random () * maxPos)); . 9 } 10 return pwd; . 11 } 12 is document.write(randomString(32)); 13 </script>
Call randomString method parameter len is the length of the returned random string.
JS produce several uses random numbers!
1 <script> 2 function GetRandomNum(Min,Max) 3 { 4 var Range = Max - Min; 5 var Rand = Math.random(); 6 return(Min + Math.round(Rand * Range)); 7 } 8 var num = GetRandomNum(1,10); 9 alert(num); 10 </script> 11 var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; 12 13 function generateMixed(n) { 14 var res = ""; 15 for(var i = 0; i < n ; i ++) { 16 var id = Math.ceil(Math.random()*35); 17 res += chars[id]; 18 } 19 return res; 20 }
1.Math.random (); the result is a random number between 0-1 (including 0, excluding. 1)
2.Math.floor (num); num is a parameter value to an integer portion of the function of num.
3.Math.round (num); num is a parameter value, the function result is an integer rounding num.
Math: mathematical object, mathematical calculation to provide data.
Math.random (); return 0 and 1 (including 0, not including 1) a random number.
Math.ceil (n); returns the smallest integer greater than or equal to n.
With Math.ceil (Math.random () * 10) ; when the main acquired random integer from 1 to 10, taking a very small probability 0.
Returns the value of an integer n rounded; Math.round (n).
With Math.round (Math.random ()); equalizes obtaining random integer 0 to 1.
With Math.round (Math.random () * 10) ; time, may be substantially balanced to obtain random integer from 0 to 10, wherein the probability of obtaining a minimum value of 0 and a maximum 10 less than half.
Math.floor (n); returns the largest integer less than or equal to n.
With Math.floor (Math.random () * 10) ; when the equalization can obtain random integer from 0 to 9.
generating a random string js + stamp obtaining
The default is generated JS 13, needs to pass php / 1000
1 timestamp = timestamp/1000; 2 <script type="text/javascript"> 3 function randomChar(l) { 4 var x="0123456789qwertyuioplkjhgfdsazxcvbnm"; 5 var tmp=""; 6 var timestamp = new Date().getTime(); 7 for(var i=0;i< l;i++) { 8 tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length); 9 } 10 return timestamp+tm
Original: https://www.jb51.net/article/50910.htm