There is an array a=[4,19,23,44,56,1], create a new array b, and b randomly selects one element at a time from a until it is exhausted. Implemented in JavaScript
認(rèn)證高級PHP講師
You can shuffle a with pseudo-randomness, exchange the numbers in any two positions, do this n times to achieve the shuffling effect, and then assign it to b.
Or just follow the steps. If code efficiency is not considered, the array operations provided by the lodash library can make the code more elegant:
var src = [4,19,23,44,56,1];
var shuffle = [];
while(src.length > 0){
var random_index = Math.floor(Math.random() * src.length);
shuffle.push(src[random_index]);
src = src.filter(function(el, i){
return i != random_index;
});
}
I thought of a better pseudorandom method, which is directly sorted randomly, the code is simpler, and the operation efficiency is high:
var src = [4,19,23,44,56,1];
var shuffle = src;
shuffle.sort(function(){
return Math.floor(Math.random() * 3) - 1;
});
var a=[4,19,23,44,56,1];
var b=[];
var c=[0,0,0,0,0,0];
var aLen = a.length;
while (c.indexOf(0)!=-1){
var randomNum = Math.floor(Math.random()*aLen);
if (c[randomNum] == 0){
b.push(a[randomNum]);
c[randomNum]=1
}
}
console.log(b);
The implementation is as follows:
var a = [4,19,23,44,56,1];
function randomSort(array){
var newArray = [];
for(let i = 0; i < array.length;){
let j = Math.random() * array.length; // 獲取隨機(jī)的下標(biāo)值
j = ~~j; // 取整
let item = array.splice(j, 1)[0]; // 從原數(shù)組中剔除選中的元素
newArray.push(item);
}
return newArray;
}
randomSort(a);
My idea is to traverse this array, get random elements in this array, delete them from array a and push them into array b, and so on
var a = [4,19,23,44,56,1],
aLen = a.length,
b = [];
// 定義刪除數(shù)組中指定元素的方法
Array.prototype.removeByValue = function(val) {
for(var i=0; i<this.length; i++) {
if(this[i] == val) {
this.splice(i, 1);
break;
}
}
}
// 遍歷a數(shù)組
for(var i=0; i<aLen; i++){
var cur = a[Math.floor(Math.random()*a.length)]; //獲取隨機(jī)的元素
a.removeByValue(cur); // 從a中刪除
b.push(cur); // 添加進(jìn)b
}
console.log(b)
var a=[4,19,23,44,56,1];
function randomSelect(str){
var leng= a.length;
var b=[];
while(leng!=0){
b.push(a.splice(Math.random()*leng,1).join())
leng--;
}
return b;
}
console.log(randomSelect(a));
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
//有一個(gè)數(shù)組a=[4,19,23,44,56,1],新建一個(gè)數(shù)組b,b從a中一次隨機(jī)選取一個(gè)元素,取完為止。用JavaScript實(shí)現(xiàn)
var a = [4,19,23,44,56,1];
var b = [];
function findInArr(num, arr){
for(var i=0; i<arr.length; i++){
if(num == arr[i]){
return true;
}
}
return false;
}
function rnd(m,n){
return m + Math.floor(Math.random()*(n-m));
}
function randomPass(arr){
var len = arr.length;
var rndIndex = rnd(0,len-1);
console.log(rndIndex);
var temp = arr[rndIndex];
if(!findInArr(temp,b)){
b.push(temp);
arr.splice(rndIndex,1);
if(arr.length > 0){
randomPass(arr);
}
}
}
randomPass(a);
console.log(b); // [44, 56, 19, 23, 4, 1] (每次隨機(jī))
console.log(a); // []
</script>
</body>
</html>
I won’t write it down to the fourth floor. I just randomly select one from the array one at a time. After randomization, remove it from the array. Continue randomizing. Just push the removed ones into the new array and it will be ok