20 개의 값과 또 다른 배열이 포함 된 배열 (예 : 'origA')이 있습니다 (단 하나의 값만있는 "itemA"라고 말합니다.) 10 개 임의 값 "itemA"로 "ORIGA"의.하지만 난 이미 "itemA"로 밀어되는 동일한 값을 밀어 수 없습니다. 우리는이 작업을 수행 할 수있는 방법임의의 값을 한 배열에서 다른 배열로 반복없이 전달
?
20 개의 값과 또 다른 배열이 포함 된 배열 (예 : 'origA')이 있습니다 (단 하나의 값만있는 "itemA"라고 말합니다.) 10 개 임의 값 "itemA"로 "ORIGA"의.하지만 난 이미 "itemA"로 밀어되는 동일한 값을 밀어 수 없습니다. 우리는이 작업을 수행 할 수있는 방법임의의 값을 한 배열에서 다른 배열로 반복없이 전달
?
// Define how many random numbers are required.
const REQUIRED:int = 10;
// Loop until either the original array runs out of numbers,
// or the destination array reaches the required length.
while(origA.length > 0 && itemA.length < REQUIRED)
{
// Decide on a random index and pull the value from there.
var i:int = Math.random() * origA.length;
var r:Number = origA[i];
// Add the value to the destination array if it does not exist yet.
if(itemA.indexOf(r) == -1)
{
itemA.push(r);
}
// Remove the value we looked at this iteration.
origA.splice(i, 1);
}
당신은 사본을 만들 수 있습니다 origA
을 입력하고 itemA
에 추가하는 항목을 제거하십시오.
비 최적화 된 버전 (루프 내부 length
없이 전화, indexOf
, splice
또는 push
)
var origA:Array = [1, 2, 3, 4, 5, 6, 7];
var itemA:Array = [0];
var copyA:Array = origA.concat();
var N:int = 10;
var n:int = Math.min(N, copyA.length);
for (var i:int = 0; i < n; i++) {
// Get random value
var index:int = Math.floor(Math.random() * copyA.length);
var value:int = copyA[index];
// Remove the selected value from copyA
copyA.splice(index, 1);
// Add the selected value to itemA
itemA.push(value);
}
trace(itemA);
//0,1,7,2,6,4,3,5
최적화 된 버전 :
var origA:Array = [1, 2, 3, 4, 5, 6, 7];
var itemA:Array = [0];
var copyA:Array = origA.concat();
var copyALength:int = copyA.length;
var itemALength:int = itemA.length;
var N:int = 10;
var n:int = Math.min(N, copyALength);
for (var i:int = 0; i < n; i++) {
// Get random value
var index:int = Math.floor(Math.random() * copyALength);
var value:int = copyA[index];
// Remove the selected value from copyA
copyA[index] = copyA[--copyALength];
// Add the selected value to itemA
itemA[itemALength++] = value;
}
trace(itemA);
//0,2,5,7,4,1,3,6
EDIT1 : 원래의 배열이있는 경우 단지 몇 가지 항목, 내 첫 번째 버전이나 다른 답변을 다른 답변을 사용하십시오. 하지만 수천 가지 항목 이상을 보유하고 있다면 최적화 된 버전을 사용하는 것이 좋습니다.
편집 :
2000ms
12ms
1,000,000
항목을 포함하는 배열로
1,000
임의로 선택 항목을 복사하는 데 걸리는 시간
1ms
길이가 10 인 배열이나 100 이하의 배열로 작업해도 좋지만 * indexOf()가 없어도 작동하는 솔루션이 필요하지는 않습니다. 훨씬 더 큰 솔루션을 찾는 사람들에게 적합하다고 인정합니다. 배열 :) – Marty
주로 (splice()를 사용하여) 변경하지 않고'indexOf()'의 사용을 피하기 위해 원래 배열의 복사본을 사용했습니다.두 번째로 최적화 된 버전에서 나는 값 비싼 메소드 인'length','indexOf','splice'와'push'를 피할 수 있습니다. – sch
필자는'indexOf()'부분에 동의해야합니다 - 항목을 발견 할 때까지 인덱스 0에서 반복 할 것이고, 이는 더 큰 배열을 사용하는 경우 비용이 많이 듭니다. – weltraumpirat
다음은 실제 짧은 것입니다. 당신이 MAX에 도달 할 때까지 다음 대상 배열에 CONCAT, 원래의 배열에서 임의의 항목을 제거
const MAX:int = 10;
var orig:Array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
var target:Array = [];
var tmp:Array = [];
var i : int = -1;
var len : int = orig.length;
while (++i < MAX && len > 0) {
var index:int = int(Math.random()*len);
tmp[i] = orig[index];
orig[index] = orig[--len];
}
target = target.concat(tmp);
편집 항목을 제거
채택 @의 모델명 : SCH의 방법. 받아 들여지는 것이 그의 대답입니다. 나는 단지 이것을 while-loop를 위해 보관했다.
그는 "무작위"라고 말했어 ... – weltraumpirat
나는 명확하지는 않지만 미안하지만 반복하지 않고 10 가지 임의의 값이 필요합니다. – Sravan0313
@ Marty Wallace "origA"의 모든 값이 다르므로이 문제가 발생하지 않아야합니다. 어쨌든, 내가 반복없이 말한 것은 임의의 값이 "origA"에서 "itemA"로 푸시되고 동일한 값이 다시 푸시되지 않아야 함을 의미합니다. 무슨 뜻인지 알 겠어? – Sravan0313