Some examples of random numbers in JavaScript

Source: Internet
Author: User

JS Random number method

The following methods apply to hidden windows: An IFRAME page, a mobile browser, and so on.

Method: Gets the current page URL, which is loaded after the URL plus a random number. You can force the refresh of the current page.

Disadvantage: Because a new URL is loaded that produces ' history ', the browser can return to look at a page (actually the same page).

  code is as follows copy code

var url= '; To acquire, js,php,asp, etc. as needed;
Function Randomnumber () {
//Get random number
    var now=new Date ();
    var number = Now.getseconds () + (Now.getmilliseconds ()%43) *7/1.131412567;
    if (number ==0) {
        number = Now.getfullyear () + (no W.getmilliseconds ()%1.24377) *7/1.131412567;
   }
    return number;
}
window.location.href=url+ ' &number= ' +randomnumber ();

//Note that the delimiters here & need to be based on the symbols used in the actual variable URL;

random number, number of random two number of intervals

  code is as follows copy code

/*
  * Description: Random integer that takes any value starting to any value
  * Parameters:
      under: Starting value
      ; Over: Termination value
*/
Function Frandomby (under, over) {
    switch (arguments.length) {
   ;      case 1:return parseint (Math.random () *under+1);
        case 2:return parseint (Math.random () * (over-under+1) + under); 
        Default:return 0;
   }
}

A set of beautiful color arrays that you can add to your face to produce random colors:

The code is as follows Copy Code


/*
* Note: According to random number of random color, you must first define a color value of the global array variable
Parameters
Num: Random number
*/
var colorarr=[' #FF6666 ', ' #D366FF ', ' #FF8166 ', ' #6672FF ', ' #FF667F ', ' #9B66FF ', ' #FF66D8 ', ' #FF6675 ', ' #66B2FF ', ' Ff8e66 ', ' #FF66F9 ', ' #FF8B66 ', ' #FF7766 ', ' #FF66C6 ', ' #FFC666 ', ' #6866FF ', ' #FF667C ', ' #FF7566 ', ' #FF66BC ', ' #6666FF ', ' 6696FF ', ' #FF9666 ', ' #66DDFF ', ' #669DFF ', ' #CE66FF ', ' #66C6FF ', ' #FFAD66 ', ' #BA66FF ', ' #8966FF ', ' #E266FF ', ' #F466FF ', ' Ff66be ', ' #36F08B ', ' #66A3FF ', ' #F166FF ', ' #66BAFF ', ' #8E66FF ', ' #FF6689 ', ' #66AAFF ', ' #C966FF ', ' #66A0FF ', ' #8766FF ', ' fa558b ', ' #66B7FF ', ' #ED66FF '];

function Randomcolor (num) {
return Colorarr[num];
}

var color=randomcolor (Frandomby (0, 30)); Get the color


generate random color values

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Refresh try-random color practice </title>
<style type= "Text/css" >
. div {width:100px; height:100px; float:left;}
</style>

<body>
<div class= "Div" ></div>
<div class= "Div" ></div>
<div class= "Div" ></div>
<div class= "Div" ></div>
<div class= "Div" ></div>
<div class= "Div" ></div>

<script type= "Text/javascript" >
Window.onload=function () {
var obj=document.getelementsbyclassname (' div ');
for (Var i=0;i<obj.length;i++) {
Obj[i].style.backgroundcolor=hsl2color ([Radomfuc (360), 100, 70])
}
}

function Radomfuc ($value) {//Take random numbers
return parseint ($value * math.random ());
}

function Hsl2color (HSL) {//HSL color algorithm
if (Hsl[0] > 360 | | hsl[0] < 0 | | hsl[1] > | | hsl[1] < 0 | | hsl[2] > | | hsl[2] < 0) return "#000 000 ";
var RGB = [0, 0, 0];
if (Hsl[0] <= 60) {
Rgb[0] = 255;
RGB[1] = Math.floor (255/60 * hsl[0));
else if (Hsl[0] <= 120) {
Rgb[0] = Math.floor (255-(255/60) * (hsl[0)-60));
RGB[1] = 255;
else if (Hsl[0] <= 180) {
RGB[1] = 255;
RGB[2] = Math.floor ((255/60) * (hsl[0)-120));
else if (Hsl[0] <= 240) {
RGB[1] = Math.floor (255-(255/60) * (hsl[0)-180));
RGB[2] = 255;
else if (Hsl[0] <= 300) {
Rgb[0] = Math.floor ((255/60) * (hsl[0)-240));
RGB[2] = 255;
else if (Hsl[0] <= 360) {
Rgb[0] = 255;
RGB[2] = Math.floor (255-(255/60) * (hsl[0)-300));
}
var sat = Math.Abs ((hsl[1]-100)/100);
Rgb[0] = Math.floor (rgb[0]-(rgb[0)-128) * sat);
RGB[1] = Math.floor (rgb[1]-(rgb[1)-128) * sat);
RGB[2] = Math.floor (rgb[2]-(rgb[2)-128) * sat);
var lum = (Hsl[2]-50)/50;
if (Lum > 0) {
Rgb[0] = Math.floor (rgb[0] + (255-rgb[0)) * Lum);
RGB[1] = Math.floor (rgb[1] + (255-rgb[1)) * Lum);
RGB[2] = Math.floor (rgb[2] + (255-rgb[2)) * Lum);
else if (Lum < 0) {
Rgb[0] = Math.floor (rgb[0] + rgb[0] * lum);
RGB[1] = Math.floor (rgb[1] + rgb[1] * lum);
RGB[2] = Math.floor (rgb[2] + rgb[2] * lum);
}
Return "#" + (0x1000000 + rgb[0] * 0x010000 + rgb[1] * 0x000100 + rgb[2]). toString. substring (1);
}
</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.