Examples of random sequences of javascript arrays

Source: Internet
Author: User

Core code

The code is as follows Copy Code

function Fnshuffle (arr) {
var tmp, ran, len = arr.length;
for (var i = 0; i < len; i++) {
ran = parseint (Math.random () * len);
TMP = Arr[i];
Arr[i] = Arr[ran];
Arr[ran] = tmp;
}
return arr;
}

Example

The code is as follows Copy Code

<!doctype html>
<meta charset= "Utf-8"/>
<title> array Random sequence </title>
<body>
Before sorting: <span id = "Qian" ></span><br/>
After sorting: <span id = "HOU" ></span>
<script type= "Text/javascript" >

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
document.getElementById (' Qian '). InnerHTML = arr;
arr = Fnshuffle (arr);
document.getElementById (' Hou '). InnerHTML = arr;

Www.111cn.net

function Fnshuffle (arr) {
var tmp, ran, len = arr.length;
for (var i = 0; i < len; i++) {
ran = parseint (Math.random () * len);
TMP = Arr[i];
Arr[i] = Arr[ran];
Arr[ran] = tmp;
}
return arr;
}
</script>
</body>

Run results

Before sort: 0,1,2,3,4,5,6,7,8,9
After sorting: 6,4,1,8,7,9,5,0,2,3

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.