Sort random numbers in javascript by some instances

Source: Internet
Author: User

The following are some common examples by using the random number function. The examples include: generate random color values, number of random two numbers, and random number method.

JS Random Number Method

The following method applies to hidden windows: Iframe pages, mobile browsers, and so on.

Method: Obtain the URL of the current page, add a random number after the URL, and load it. You can force refresh the current page.

Disadvantage: because a new URL is loaded, a 'historical record' is generated. The browser can return to the previous page (actually the same page ).

The Code is as follows: Copy code

Var url = ''; // obtain as needed, such as JS, PHP, and ASP;
Function RandomNumber (){
// Obtain a random number
Var now = new Date ();
Var number = now. getSeconds () + (now. getMilliseconds () % 43) * 7/1. 131412567;
If (number = 0 ){
Number = now. getFullYear () + (now. getMilliseconds () % 1.24377) * 7/1. 131412567;
}
Return number;
}
Window. location. href = url + '& number =' + RandomNumber ();

// Note that the separator here & the symbol used in the actual variable url;

Random number. The number of random intervals.

The Code is as follows: Copy code

/*
* Description: Random integers starting from any value to any value.
* Parameters:
Under: start 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 own beautiful color Arrays can be added with the following method to generate random colors:

The Code is as follows: Copy code


/*
* Description: A Global Array variable with color values must be defined based on the random color in the random number area.
* Parameters:
Num: Random Number
*/
Var colorArr = ['# FF6666', '# d0000ff', '# 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); // obtain 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> try to refresh-random color practices </title>
<Style type = "text/css">
. Div {width: 100px; height: 100px; float: left ;}
</Style>
</Head>

<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) {// obtain a random number
Return parseInt ($ value * Math. random ());
}

Function hsl2color (hsl) {// HSL color Algorithm
If (hsl [0]> 360 | hsl [0] <0 | hsl [1]> 100 | hsl [1] <0 | hsl [2]> 100 | hsl [2] <0) return "#000000 ";
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 (16 ). substring (1 );
}
</Script>
</Body>
</Html>

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.