2010-08-22T19:25:31の更新内容

programming/tips/shuffle_array/index.wiki.txt

current previous
1,147 1,93
 
${smdncms:title,配列のシャッフル}
${smdncms:title,配列のシャッフル}
 
${smdncms:keywords,VB.NET,C#,配列,ランダム,シャッフル}
${smdncms:keywords,VB.NET,C#,配列,ランダム,シャッフル}
 
${smdncms:tags,algo,lang/vb,lang/c#}
${smdncms:tags,algo,lang/vb,lang/c#}
-
配列のシャッフルを行うためのランダムな順番を作成するための関数。 見ての通り、配列の添え字の範囲のランダムな数値が配列の各要素に代入されて返される。 たいしたアルゴリズムを使っているわけではないので、改良の余地がある。
-
#googleadunit
 

        

        
~
配列のシャッフルを行うためのメソッド。 使いやすいように拡張メソッドとして呼び出せるようにしてある。 オリジナルの配列には変更を加えず、シャッフルした配列を戻り値として返す。
#code(vb){{
-
Public Shared Function MakeRandomizedArray(ByVal length As Integer) As Integer()
 

        

        
~
#googleadunit
    Dim i As Integer
~

          
    Dim j As Integer
~
#tabpage(C#)
    Dim rnd As New System.Random()
~
#code(cs){{
    Dim blnIsUsed As Boolean
~
public static class ArrayExtensions {
    Dim intRandom As Integer
~
  public static T[] Shuffle<T>(this T[] array)
    Dim arr(length - 1) As Integer
+
  {
+
    return Shuffle(array, new Random());
+
  }
+

          
+
  public static T[] Shuffle<T>(this T[] array, Random random)
+
  {
+
    if (array == null)
+
      throw new ArgumentNullException("array");
+
    if (random == null)
+
      throw new ArgumentNullException("random");
+

          
+
    var shuffled = (T[])array.Clone();
+

          
+
    if (shuffled.Length < 2)
+
      return shuffled;
+

          
+
    for (var i = 1; i < shuffled.Length; i++) {
+
      var j = random.Next(0, i + 1);
+

          
+
      // swap
+
      var temp    = shuffled[i];
+
      shuffled[i] = shuffled[j];
+
      shuffled[j] = temp;
+
    }
 

        

        
~
    return shuffled;
    For i = 0 To length - 1
+
  }
+
}
 

        

        
~
class Sample {
        Do
+
  public static void Main()
+
  {
+
    var arr1 = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+

          
+
    Print(arr1);
+
    Print(arr1.Shuffle());
+

          
+
    var arr2 = new string[] {"foo", "bar", "baz"};
+

          
+
    Print(arr2);
+
    Print(arr2.Shuffle());
+

          
+
    var arr3 = new int[] {};
+

          
+
    Print(arr3);
+
    Print(arr3.Shuffle());
+
  }
+

          
+
  private static void Print<T>(T[] arr)
+
  {
+
    foreach (var val in arr) {
+
      Console.Write(val);
+
      Console.Write(" ");
+
    }
 

        

        
~
    Console.WriteLine();
            intRandom = rnd.Next(0, length)
+
  }
+
}
+
}}
+
#tabpage(VB)
+
#code(vb){{
+
Imports System
 

        

        
~
Module ArrayExtensions
            blnIsUsed = False
+
  <System.Runtime.CompilerServices.Extension()> _
+
  Public Function Shuffle(Of T)(ByVal array As T()) As T()
+
    Return Shuffle(array, New Random())
+
  End Function
 

        

        
~
  Public Function Shuffle(Of T)(ByVal array As T(), ByVal random As Random) As T()
            For j = 0 To i - 1
+
    If array Is Nothing Then Throw New ArgumentNullException("array")
+
    If random Is Nothing Then Throw New ArgumentNullException("random")
 

        

        
~
    Dim shuffled As T() = DirectCast(array.Clone(), T())
                If intRandom = arr(j) Then
 

        

        
~
    If shuffled.Length < 2 Then Return shuffled
                    blnIsUsed = True
 

        

        
~
    For i As Integer = 1 To shuffled.Length - 1
                    Exit For
+
      Dim j As Integer = random.Next(0, i + 1)
 

        

        
~
      ' swap
                End If
+
      Dim temp As T = shuffled(i)
+
      shuffled(i) = shuffled(j)
+
      shuffled(j) = temp
+
    Next
 

        

        
~
    Return shuffled
            Next j
+
  End Function
+
End Module
 

        

        
~
Class Sample
        Loop While blnIsUsed
+
  Public Shared Sub Main()
+
    Dim arr1 As Integer() = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
 

        

        
~
    Print(arr1)
        arr(i) = intRandom
+
    Print(arr1.Shuffle())
 

        

        
~
    Dim arr2 As String() = New String() {"foo", "bar", "baz"}
    Next i
 

        

        
~
    Print(arr2)
    Return arr
+
    Print(arr2.Shuffle())
 

        

        
~
    Dim arr3 As Integer() = New Integer() {0}
End Function
-
}}
 

        

        
~
    Print(arr3)
#code(cs){{
~
    Print(arr3.Shuffle())
public static int[] MakeRandomizedArray( int length )
~
  End Sub
{
-
    int iRandom;
-
    int[] arr = new int[length];
-
    bool bIsUsed;
-
    System.Random rnd = new System.Random();
-

          
-
    for ( int i = 0; i < length; i++ )
-
    {
-
        do
-
        {
-
            iRandom = rnd.Next( 0, length );
-

          
-
            bIsUsed = false;
-

          
-
            for ( int j = 0; j < i; j++ )
-
            {
-
                if ( iRandom == arr[j] )
-
                {
-
                    bIsUsed = true;
-
                    break;
-
                }
-
            }
-
        } while ( bIsUsed );
 

        

        
~
  Private Shared Sub Print(Of T)(ByVal arr As T())
        arr[i] = iRandom;
~
    For Each val As T In arr
    }
+
      Console.Write(val)
+
      Console.Write(" ")
+
    Next
 

        

        
~
    Console.WriteLine()
    return arr;
~
  End Sub
}
+
End Class
 
}}
}}
+
#tabpage-end
+

          
+
実行結果。
 

        

        
 
#prompt(実行結果){{
#prompt(実行結果){{
~
0 1 2 3 4 5 6 7 8 9 
arr(0) = 7
~
4 8 5 9 3 0 2 6 1 7 
arr(1) = 9
~
foo bar baz 
arr(2) = 8
~
bar foo baz 
arr(3) = 1
~

          
arr(4) = 0
~

          
arr(5) = 4
-
arr(6) = 2
-
arr(7) = 5
-
arr(8) = 6
-
arr(9) = 3
 
Press any key to continue
Press any key to continue
 
}}
}}
+

          
+
-参考
+
--[[http://ray.sakura.ne.jp/tips/shaffle.html]]
+