The _javascript technique of the content random output of the JavaScript implementation array

Source: Internet
Author: User

Sometimes we may need to randomly extract a piece of content from an array, and here's how to do that with a code example.

The code is as follows:

<script type= "Text/javascript" > 
var thearray=new Array ();
thearray[0]= "cloud-dwelling community";
thearray[1]= "cloud-dwelling community one";
thearray[2]= "cloud-dwelling Community II";
thearray[3]= "cloud-dwelling community three";
thearray[4]= "cloud-dwelling community four";
thearray[5]= "cloud-dwelling community five";
thearray[6]= "Yun-qi Community Six";
function Ranfun ()
{return
 parseint (Math.random () *7);
}
document.write (Thearray[ranfun ());
</script>

The above code can randomly output elements in an array, the code is very simple, by using the random number function to provide a random index.

Randomly reading n different data from an array

Method One

var ary = new Array ();
var xiaowu=new Array (
"1 My
",
"2 my"
,
"3 My
",
"4 My
",
"5 My
",
" 6 my "
,
" 7 mine, "
8 My
",
"9 My
",
"10 My
",
"11 my"
,
" 12 my "
,
" 13 Mine, "
14 My
",
"15 My
",
"16 My
",
"17 My
",
"18 My
",
"19 mine
",
"20 mine
"
);
var s01=xiaowu.length while
(Ary.length < 6)
{
var tmp =xiaowu[parseint (Math.random () *s01)]
var B = false;
for (var i=0; i<ary.length; i++)
{
if (ary[i] = = tmp)
{
b = true;
break;
}
}
if (!b)
ary[ary.length] = tmp;
}
document.write (Ary[0]);
document.write (ary[1]);
document.write (ary[2]);
document.write (Ary[3]);
document.write (Ary[4]);
document.write (Ary[5]);

Method Two

From a given array arr, a random return of num not duplicates
function getarrayitems (arr, num) {
///Create an array, copy the incoming array for operation, not directly manipulate the incoming array;
var temp_array = new Array ();
For (var index in arr) {
temp_array.push (Arr[index]);
}
The value of the item to be removed, saved in this array
var return_array = new Array ();
for (var i = 0; i<num; i++) {
//judge if the array also has the elements that can be taken out, in case the subscript crosses
over if (temp_array.length>0) {
///generates a random index in the array C13/>var Arrindex = Math.floor (Math.random () *temp_array.length);
Copy the corresponding array element value of this random index
return_array[i] = Temp_array[arrindex];
Then the array element of this index is deleted, at which point the Temp_array becomes the new array
temp_array.splice (Arrindex, 1), or
else {
//The data entry in the array exits the loop For example, the array originally had only 10 items, but required 20 items to be removed.
break;
}
}
return return_array;
}

Test
var arrlist=[ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33];
Alert (Getarrayitems (arrlist,6));

This JS code can be in the array of elements of random arrangement, this is very useful, such as we play poker when you can arrange cards, that is, computer shuffle.

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.