. 당신은 당신이 내 생각은이
private Random rand = new Random();
private List<int> used = new List<int>;
protected int randomNonrepeating() {
int i = rand.next();
while(used.contains(i))
i = rand.next();
used.add(i);
return i;
}
처럼 뭔가를 할 수 반복하지 임의의 숫자를 생성 할 경우에, 그것은 당신이 비록 찾고있는 아주 것이 아니다. 제공된 링크에서 알고리즘을 수정하여 문자열이 아닌 정수 배열로 작업하려는 경우. 유형을 변경하기 만하면됩니다. 이 같은 것이
using System;
System.Collections.Generic; using System.Linq;
정적 클래스 RandomStringArrayTool { static Random _random = new Random();
public static string[] RandomizeStrings(int[] arr)
{
List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>();
// Add all strings from array
// Add new random int each time
foreach (var s in arr)
{
list.Add(new KeyValuePair<int, int>(_random.Next(), s));
}
// Sort the list by the random number
var sorted = from item in list
orderby item.Key
select item;
// Allocate new string array
int[] result = new string[arr.Length];
// Copy values to array
int index = 0;
foreach (KeyValuePair<int, int> pair in sorted)
{
result[index] = pair.Value;
index++;
}
// Return copied array
return result;
}
}이 도움이
희망.
여기서 달성하고자하는 것은 예제 배열에 반복이 없습니까? –
'var array2 = new int [] {1, 3, 5, 7, 9} .OrderBy (_ => rnd.Next())'충분하지 않습니까? – I4V
가능한 모든 주문이 똑같이 * 보장 될 필요가 있습니까? 또한 과거 셔플에서 미래 셔플을 추론 할 수 없다는 보장이 필요합니까 *? (후자의 요구 사항은 우연한 게임에 필수적이며, 현재 데크에서 미래의 데크를 추론 할 수 있다면 온라인 포커가 훨씬 쉽게됩니다.) 두 질문 중 하나에 대한 대답이 "예"이면 여기에 게시 된 답변 중 어느 것도 맞지 않습니다. 작은 편견이나 공격자가 내부 상태를 예측할 수있는 능력에 신경 쓰지 않으면 여기에 게시 된 답변이 합리적입니다. –