構文
解説擬似乱数は、有限個の数値の中から等しい確率で選び出されます。この数字を選出するために使用する演算アルゴリズムには限界があるため、選び出された数字は完全な乱数ではありませんが、実質的には乱数として十分使用できます。Random クラスの現在の実装は、Donald E. Knuth の乱数ジェネレータ減算アルゴリズムに基づいています。詳細については、D. E. Knuth 著『The Art of Computer Programming, volume 2: Seminumerical Algorithms』(第 2 版、1981 年 Addision-Wesley, Reading, MA 出版) を参照してください。
乱数の生成には、初期値としてシード値を使用します。同じシードを繰り返し使用すると、生成される乱数系列も同じになります。異なる乱数系列を生成する 1 つの方法は、シード値を時間によって決定することです。これにより、Random の新しいインスタンスごとに異なる乱数系列を生成できます。
パフォーマンスを向上するには、1 つの乱数を生成するために繰り返し新しい Random を作成するのではなく、1 つの Random を作成して何回も使用し、多くの乱数を生成するようにします。
ランダム パスワードの作成に適した、暗号として安全な乱数を生成するには、たとえば、System.Security.Cryptography.RNGCryptoServiceProvider などの System.Security.Cryptography.RandomNumberGenerator から派生したクラスを使用します。
使用例クラス コンストラクタのさまざまなオーバーロードを使用して Random オブジェクトを作成し、それらのオブジェクトから整数および倍精度浮動小数点数のランダムなシーケンスを生成するコード例を次に示します。
' Example of the Random class constructors and Random.NextDouble( ) ' method. Imports System Imports System.Threading Imports Microsoft.VisualBasic Module RandomObjectDemo ' Generate random numbers from the specified Random object. Sub RunIntNDoubleRandoms( randObj As Random ) ' Generate the first six random integers. Dim j As Integer For j = 0 To 5 Console.Write( " {0,10} ", randObj.Next( ) ) Next j Console.WriteLine( ) ' Generate the first six random doubles. For j = 0 To 5 Console.Write( " {0:F8} ", randObj.NextDouble( ) ) Next j Console.WriteLine( ) End Sub ' Create a Random object with the specified seed. Sub FixedSeedRandoms( seed As Integer ) Console.WriteLine( vbCrLf & _ "Random numbers from a Random object with " & _ "seed = {0}:", seed ) Dim fixRand As New Random( seed ) RunIntNDoubleRandoms( fixRand ) End Sub ' Create a random object with a timer-generated seed. Sub AutoSeedRandoms( ) ' Wait to allow the timer to advance. Thread.Sleep( 1 ) Console.WriteLine( vbCrLf & _ "Random numbers from a Random object " & _ "with an auto-generated seed:" ) Dim autoRand As New Random( ) RunIntNDoubleRandoms( autoRand ) End Sub Sub Main( ) Console.WriteLine( _ "This example of the Random class constructors " & _ "and Random.NextDouble( ) " & vbCrLf & _ "generates the following output." & vbCrLf ) Console.WriteLine( "Create Random " & _ "objects, and then generate and display six " & _ "integers and " & vbCrLf & "six doubles from each." ) FixedSeedRandoms( 123 ) FixedSeedRandoms( 123 ) FixedSeedRandoms( 456 ) FixedSeedRandoms( 456 ) AutoSeedRandoms( ) AutoSeedRandoms( ) AutoSeedRandoms( ) End Sub End Module ' This example of the Random class constructors and Random.NextDouble( ) ' generates the following output. ' ' Create Random objects, and then generate and display six integers and ' six doubles from each. ' ' Random numbers from a Random object with seed = 123: ' 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 ' 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 ' ' Random numbers from a Random object with seed = 123: ' 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 ' 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 ' ' Random numbers from a Random object with seed = 456: ' 2044805024 1323311594 1087799997 1907260840 179380355 120870348 ' 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 ' ' Random numbers from a Random object with seed = 456: ' 2044805024 1323311594 1087799997 1907260840 179380355 120870348 ' 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 ' ' Random numbers from a Random object with an auto-generated seed: ' 1920831619 1346865774 2006582766 1968819760 332463652 110770792 ' 0.71326689 0.50383335 0.50446082 0.66312569 0.94517193 0.58059287 ' ' Random numbers from a Random object with an auto-generated seed: ' 254927927 1205531663 1984850027 110020849 1438111494 1697714106 ' 0.19383387 0.52067738 0.74162783 0.35063667 0.31247720 0.38773733 ' ' Random numbers from a Random object with an auto-generated seed: ' 736507882 1064197552 1963117288 398705585 396275689 1137173773 ' 0.67440084 0.53752140 0.97879483 0.03814764 0.67978248 0.19488178
// Example of the Random class constructors and Random.NextDouble( ) // method. using System; using System.Threading; public class RandomObjectDemo { // Generate random numbers from the specified Random object. static void RunIntNDoubleRandoms( Random randObj ) { // Generate the first six random integers. for( int j = 0; j < 6; j++ ) Console.Write( " {0,10} ", randObj.Next( ) ); Console.WriteLine( ); // Generate the first six random doubles. for( int j = 0; j < 6; j++ ) Console.Write( " {0:F8} ", randObj.NextDouble( ) ); Console.WriteLine( ); } // Create a Random object with the specified seed. static void FixedSeedRandoms( int seed ) { Console.WriteLine( "\nRandom numbers from a Random object with " + "seed = {0}:", seed ); Random fixRand = new Random( seed ); RunIntNDoubleRandoms( fixRand ); } // Create a random object with a timer-generated seed. static void AutoSeedRandoms( ) { // Wait to allow the timer to advance. Thread.Sleep( 1 ); Console.WriteLine( "\nRandom numbers from a Random object " + "with an auto-generated seed:" ); Random autoRand = new Random( ); RunIntNDoubleRandoms( autoRand ); } static void Main( ) { Console.WriteLine( "This example of the Random class constructors and " + "Random.NextDouble( ) \n" + "generates the following output.\n" ); Console.WriteLine( "Create Random objects, and then generate and " + "display six integers and \nsix doubles from each."); FixedSeedRandoms( 123 ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 456 ); FixedSeedRandoms( 456 ); AutoSeedRandoms( ); AutoSeedRandoms( ); AutoSeedRandoms( ); } } /* This example of the Random class constructors and Random.NextDouble( ) generates the following output. Create Random objects, and then generate and display six integers and six doubles from each. Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with an auto-generated seed: 380213349 127379247 1969091178 1983029819 1963098450 1648433124 0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560 Random numbers from a Random object with an auto-generated seed: 861793304 2133528783 1947358439 124230908 921262645 1087892791 0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005 Random numbers from a Random object with an auto-generated seed: 1343373259 1992194672 1925625700 412915644 2026910487 527352458 0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451 */
// Example of the Random class constructors and Random::NextDouble( ) // method. using namespace System; using namespace System::Threading; // Generate random numbers from the specified Random object. void RunIntNDoubleRandoms( Random^ randObj ) { // Generate the first six random integers. for ( int j = 0; j < 6; j++ ) Console::Write( " {0,10} ", randObj->Next() ); Console::WriteLine(); // Generate the first six random doubles. for ( int j = 0; j < 6; j++ ) Console::Write( " {0:F8} ", randObj->NextDouble() ); Console::WriteLine(); } // Create a Random object with the specified seed. void FixedSeedRandoms( int seed ) { Console::WriteLine( "\nRandom numbers from a Random object with seed = {0}:", seed ); Random^ fixRand = gcnew Random( seed ); RunIntNDoubleRandoms( fixRand ); } // Create a random object with a timer-generated seed. void AutoSeedRandoms() { // Wait to allow the timer to advance. Thread::Sleep( 1 ); Console::WriteLine( "\nRandom numbers from a Random object " "with an auto-generated seed:" ); Random^ autoRand = gcnew Random; RunIntNDoubleRandoms( autoRand ); } int main() { Console::WriteLine( "This example of the Random class constructors and Random" "::NextDouble( ) \ngenerates the following output.\n" ); Console::WriteLine( "Create Random objects, and then generate and " "display six integers and \nsix doubles from each." ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 456 ); FixedSeedRandoms( 456 ); AutoSeedRandoms(); AutoSeedRandoms(); AutoSeedRandoms(); } /* This example of the Random class constructors and Random::NextDouble( ) generates the following output. Create Random objects, and then generate and display six integers and six doubles from each. Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with an auto-generated seed: 1624372556 1894939458 302472229 588108304 23919954 1085111949 0.14595512 0.30162298 0.92267372 0.55707657 0.25430079 0.74143239 Random numbers from a Random object with an auto-generated seed: 2105952511 1753605347 280739490 876793040 1129567796 524571616 0.62652210 0.31846701 0.15984073 0.24458755 0.62160607 0.54857684 Random numbers from a Random object with an auto-generated seed: 440048819 1612271236 259006751 1165477776 87731991 2111514930 0.10708907 0.33531104 0.39700773 0.93209853 0.98891135 0.35572129 */
継承階層
スレッド セーフ
プラットフォームWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照
構文
解説生成される数値の分布は一様で、どの数字も同じ確率で返される可能性があります。
既定のシード値は、システム時計から導かれます。ただし、パフォーマンスの高いシステムで乱数を生成する場合、システム時計の値を使用すると、期待した動作が得られない可能性があります。詳細については、Random コンストラクタの解説を参照してください。
使用例パラメータなしのクラス コンストラクタを使用して Random オブジェクトを作成し、整数および倍精度浮動小数点数のランダムなシーケンスを生成するコード例を次に示します。パラメータのないコンストラクタを使用して Random オブジェクトを再度生成すると、異なるシーケンスが生成されることがわかります。
' Example of the Random class constructors and Random.NextDouble( ) ' method. Imports System Imports System.Threading Imports Microsoft.VisualBasic Module RandomObjectDemo ' Generate random numbers from the specified Random object. Sub RunIntNDoubleRandoms( randObj As Random ) ' Generate the first six random integers. Dim j As Integer For j = 0 To 5 Console.Write( " {0,10} ", randObj.Next( ) ) Next j Console.WriteLine( ) ' Generate the first six random doubles. For j = 0 To 5 Console.Write( " {0:F8} ", randObj.NextDouble( ) ) Next j Console.WriteLine( ) End Sub ' Create a Random object with the specified seed. Sub FixedSeedRandoms( seed As Integer ) Console.WriteLine( vbCrLf & _ "Random numbers from a Random object with " & _ "seed = {0}:", seed ) Dim fixRand As New Random( seed ) RunIntNDoubleRandoms( fixRand ) End Sub ' Create a random object with a timer-generated seed. Sub AutoSeedRandoms( ) ' Wait to allow the timer to advance. Thread.Sleep( 1 ) Console.WriteLine( vbCrLf & _ "Random numbers from a Random object " & _ "with an auto-generated seed:" ) Dim autoRand As New Random( ) RunIntNDoubleRandoms( autoRand ) End Sub Sub Main( ) Console.WriteLine( _ "This example of the Random class constructors " & _ "and Random.NextDouble( ) " & vbCrLf & _ "generates the following output." & vbCrLf ) Console.WriteLine( "Create Random " & _ "objects, and then generate and display six " & _ "integers and " & vbCrLf & "six doubles from each." ) FixedSeedRandoms( 123 ) FixedSeedRandoms( 123 ) FixedSeedRandoms( 456 ) FixedSeedRandoms( 456 ) AutoSeedRandoms( ) AutoSeedRandoms( ) AutoSeedRandoms( ) End Sub End Module ' This example of the Random class constructors and Random.NextDouble( ) ' generates the following output. ' ' Create Random objects, and then generate and display six integers and ' six doubles from each. ' ' Random numbers from a Random object with seed = 123: ' 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 ' 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 ' ' Random numbers from a Random object with seed = 123: ' 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 ' 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 ' ' Random numbers from a Random object with seed = 456: ' 2044805024 1323311594 1087799997 1907260840 179380355 120870348 ' 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 ' ' Random numbers from a Random object with seed = 456: ' 2044805024 1323311594 1087799997 1907260840 179380355 120870348 ' 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 ' ' Random numbers from a Random object with an auto-generated seed: ' 1920831619 1346865774 2006582766 1968819760 332463652 110770792 ' 0.71326689 0.50383335 0.50446082 0.66312569 0.94517193 0.58059287 ' ' Random numbers from a Random object with an auto-generated seed: ' 254927927 1205531663 1984850027 110020849 1438111494 1697714106 ' 0.19383387 0.52067738 0.74162783 0.35063667 0.31247720 0.38773733 ' ' Random numbers from a Random object with an auto-generated seed: ' 736507882 1064197552 1963117288 398705585 396275689 1137173773 ' 0.67440084 0.53752140 0.97879483 0.03814764 0.67978248 0.19488178
// Example of the Random class constructors and Random.NextDouble( ) // method. using System; using System.Threading; public class RandomObjectDemo { // Generate random numbers from the specified Random object. static void RunIntNDoubleRandoms( Random randObj ) { // Generate the first six random integers. for( int j = 0; j < 6; j++ ) Console.Write( " {0,10} ", randObj.Next( ) ); Console.WriteLine( ); // Generate the first six random doubles. for( int j = 0; j < 6; j++ ) Console.Write( " {0:F8} ", randObj.NextDouble( ) ); Console.WriteLine( ); } // Create a Random object with the specified seed. static void FixedSeedRandoms( int seed ) { Console.WriteLine( "\nRandom numbers from a Random object with " + "seed = {0}:", seed ); Random fixRand = new Random( seed ); RunIntNDoubleRandoms( fixRand ); } // Create a random object with a timer-generated seed. static void AutoSeedRandoms( ) { // Wait to allow the timer to advance. Thread.Sleep( 1 ); Console.WriteLine( "\nRandom numbers from a Random object " + "with an auto-generated seed:" ); Random autoRand = new Random( ); RunIntNDoubleRandoms( autoRand ); } static void Main( ) { Console.WriteLine( "This example of the Random class constructors and " + "Random.NextDouble( ) \n" + "generates the following output.\n" ); Console.WriteLine( "Create Random objects, and then generate and " + "display six integers and \nsix doubles from each."); FixedSeedRandoms( 123 ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 456 ); FixedSeedRandoms( 456 ); AutoSeedRandoms( ); AutoSeedRandoms( ); AutoSeedRandoms( ); } } /* This example of the Random class constructors and Random.NextDouble( ) generates the following output. Create Random objects, and then generate and display six integers and six doubles from each. Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with an auto-generated seed: 380213349 127379247 1969091178 1983029819 1963098450 1648433124 0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560 Random numbers from a Random object with an auto-generated seed: 861793304 2133528783 1947358439 124230908 921262645 1087892791 0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005 Random numbers from a Random object with an auto-generated seed: 1343373259 1992194672 1925625700 412915644 2026910487 527352458 0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451 */
// Example of the Random class constructors and Random::NextDouble( ) // method. using namespace System; using namespace System::Threading; // Generate random numbers from the specified Random object. void RunIntNDoubleRandoms( Random^ randObj ) { // Generate the first six random integers. for ( int j = 0; j < 6; j++ ) Console::Write( " {0,10} ", randObj->Next() ); Console::WriteLine(); // Generate the first six random doubles. for ( int j = 0; j < 6; j++ ) Console::Write( " {0:F8} ", randObj->NextDouble() ); Console::WriteLine(); } // Create a Random object with the specified seed. void FixedSeedRandoms( int seed ) { Console::WriteLine( "\nRandom numbers from a Random object with seed = {0}:", seed ); Random^ fixRand = gcnew Random( seed ); RunIntNDoubleRandoms( fixRand ); } // Create a random object with a timer-generated seed. void AutoSeedRandoms() { // Wait to allow the timer to advance. Thread::Sleep( 1 ); Console::WriteLine( "\nRandom numbers from a Random object " "with an auto-generated seed:" ); Random^ autoRand = gcnew Random; RunIntNDoubleRandoms( autoRand ); } int main() { Console::WriteLine( "This example of the Random class constructors and Random" "::NextDouble( ) \ngenerates the following output.\n" ); Console::WriteLine( "Create Random objects, and then generate and " "display six integers and \nsix doubles from each." ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 456 ); FixedSeedRandoms( 456 ); AutoSeedRandoms(); AutoSeedRandoms(); AutoSeedRandoms(); } /* This example of the Random class constructors and Random::NextDouble( ) generates the following output. Create Random objects, and then generate and display six integers and six doubles from each. Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with an auto-generated seed: 1624372556 1894939458 302472229 588108304 23919954 1085111949 0.14595512 0.30162298 0.92267372 0.55707657 0.25430079 0.74143239 Random numbers from a Random object with an auto-generated seed: 2105952511 1753605347 280739490 876793040 1129567796 524571616 0.62652210 0.31846701 0.15984073 0.24458755 0.62160607 0.54857684 Random numbers from a Random object with an auto-generated seed: 440048819 1612271236 259006751 1165477776 87731991 2111514930 0.10708907 0.33531104 0.39700773 0.93209853 0.98891135 0.35572129 */
プラットフォームWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照
構文
例外
解説アプリケーションで複数の異なるシーケンスが必要な場合は、異なるシード値を使用して、このコンストラクタを複数回呼び出します。一意のシード値を生成する方法の 1 つに、シード値を時刻依存させる方法があります。たとえば、シード値の生成をシステム時計によって行います。
しかし、アプリケーションが高速なコンピュータ上で実行された場合には、このコンストラクタが複数回呼び出される間にシステム時計の値が変化せず、Random の複数の異なるインスタンスに対して同じシード値が生成される可能性があります。このような場合には、呼び出しのたびにシード値を変えるためのアルゴリズムを適用します。
たとえば、次に示す C# の式では、ビットごとの補数演算を使用して、システム時刻の値が変化しない場合でも 2 つの異なるシード値を生成します。
使用例シード パラメータを受け取るクラス コンストラクタを使用して Random オブジェクトを作成し、整数および倍精度浮動小数点数のランダムなシーケンスを生成するコード例を次に示します。シード パラメータを受け取るコンストラクタを使用して Random オブジェクトを再度生成すると、同じシーケンスが生成されることがわかります。
' Example of the Random class constructors and Random.NextDouble( ) ' method. Imports System Imports System.Threading Imports Microsoft.VisualBasic Module RandomObjectDemo ' Generate random numbers from the specified Random object. Sub RunIntNDoubleRandoms( randObj As Random ) ' Generate the first six random integers. Dim j As Integer For j = 0 To 5 Console.Write( " {0,10} ", randObj.Next( ) ) Next j Console.WriteLine( ) ' Generate the first six random doubles. For j = 0 To 5 Console.Write( " {0:F8} ", randObj.NextDouble( ) ) Next j Console.WriteLine( ) End Sub ' Create a Random object with the specified seed. Sub FixedSeedRandoms( seed As Integer ) Console.WriteLine( vbCrLf & _ "Random numbers from a Random object with " & _ "seed = {0}:", seed ) Dim fixRand As New Random( seed ) RunIntNDoubleRandoms( fixRand ) End Sub ' Create a random object with a timer-generated seed. Sub AutoSeedRandoms( ) ' Wait to allow the timer to advance. Thread.Sleep( 1 ) Console.WriteLine( vbCrLf & _ "Random numbers from a Random object " & _ "with an auto-generated seed:" ) Dim autoRand As New Random( ) RunIntNDoubleRandoms( autoRand ) End Sub Sub Main( ) Console.WriteLine( _ "This example of the Random class constructors " & _ "and Random.NextDouble( ) " & vbCrLf & _ "generates the following output." & vbCrLf ) Console.WriteLine( "Create Random " & _ "objects, and then generate and display six " & _ "integers and " & vbCrLf & "six doubles from each." ) FixedSeedRandoms( 123 ) FixedSeedRandoms( 123 ) FixedSeedRandoms( 456 ) FixedSeedRandoms( 456 ) AutoSeedRandoms( ) AutoSeedRandoms( ) AutoSeedRandoms( ) End Sub End Module ' This example of the Random class constructors and Random.NextDouble( ) ' generates the following output. ' ' Create Random objects, and then generate and display six integers and ' six doubles from each. ' ' Random numbers from a Random object with seed = 123: ' 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 ' 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 ' ' Random numbers from a Random object with seed = 123: ' 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 ' 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 ' ' Random numbers from a Random object with seed = 456: ' 2044805024 1323311594 1087799997 1907260840 179380355 120870348 ' 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 ' ' Random numbers from a Random object with seed = 456: ' 2044805024 1323311594 1087799997 1907260840 179380355 120870348 ' 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 ' ' Random numbers from a Random object with an auto-generated seed: ' 1920831619 1346865774 2006582766 1968819760 332463652 110770792 ' 0.71326689 0.50383335 0.50446082 0.66312569 0.94517193 0.58059287 ' ' Random numbers from a Random object with an auto-generated seed: ' 254927927 1205531663 1984850027 110020849 1438111494 1697714106 ' 0.19383387 0.52067738 0.74162783 0.35063667 0.31247720 0.38773733 ' ' Random numbers from a Random object with an auto-generated seed: ' 736507882 1064197552 1963117288 398705585 396275689 1137173773 ' 0.67440084 0.53752140 0.97879483 0.03814764 0.67978248 0.19488178
// Example of the Random class constructors and Random.NextDouble( ) // method. using System; using System.Threading; public class RandomObjectDemo { // Generate random numbers from the specified Random object. static void RunIntNDoubleRandoms( Random randObj ) { // Generate the first six random integers. for( int j = 0; j < 6; j++ ) Console.Write( " {0,10} ", randObj.Next( ) ); Console.WriteLine( ); // Generate the first six random doubles. for( int j = 0; j < 6; j++ ) Console.Write( " {0:F8} ", randObj.NextDouble( ) ); Console.WriteLine( ); } // Create a Random object with the specified seed. static void FixedSeedRandoms( int seed ) { Console.WriteLine( "\nRandom numbers from a Random object with " + "seed = {0}:", seed ); Random fixRand = new Random( seed ); RunIntNDoubleRandoms( fixRand ); } // Create a random object with a timer-generated seed. static void AutoSeedRandoms( ) { // Wait to allow the timer to advance. Thread.Sleep( 1 ); Console.WriteLine( "\nRandom numbers from a Random object " + "with an auto-generated seed:" ); Random autoRand = new Random( ); RunIntNDoubleRandoms( autoRand ); } static void Main( ) { Console.WriteLine( "This example of the Random class constructors and " + "Random.NextDouble( ) \n" + "generates the following output.\n" ); Console.WriteLine( "Create Random objects, and then generate and " + "display six integers and \nsix doubles from each."); FixedSeedRandoms( 123 ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 456 ); FixedSeedRandoms( 456 ); AutoSeedRandoms( ); AutoSeedRandoms( ); AutoSeedRandoms( ); } } /* This example of the Random class constructors and Random.NextDouble( ) generates the following output. Create Random objects, and then generate and display six integers and six doubles from each. Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with an auto-generated seed: 380213349 127379247 1969091178 1983029819 1963098450 1648433124 0.08824121 0.41249688 0.36445811 0.05637512 0.62702451 0.49595560 Random numbers from a Random object with an auto-generated seed: 861793304 2133528783 1947358439 124230908 921262645 1087892791 0.56880819 0.42934091 0.60162512 0.74388610 0.99432979 0.30310005 Random numbers from a Random object with an auto-generated seed: 1343373259 1992194672 1925625700 412915644 2026910487 527352458 0.04937517 0.44618494 0.83879212 0.43139707 0.36163507 0.11024451 */
// Example of the Random class constructors and Random::NextDouble( ) // method. using namespace System; using namespace System::Threading; // Generate random numbers from the specified Random object. void RunIntNDoubleRandoms( Random^ randObj ) { // Generate the first six random integers. for ( int j = 0; j < 6; j++ ) Console::Write( " {0,10} ", randObj->Next() ); Console::WriteLine(); // Generate the first six random doubles. for ( int j = 0; j < 6; j++ ) Console::Write( " {0:F8} ", randObj->NextDouble() ); Console::WriteLine(); } // Create a Random object with the specified seed. void FixedSeedRandoms( int seed ) { Console::WriteLine( "\nRandom numbers from a Random object with seed = {0}:", seed ); Random^ fixRand = gcnew Random( seed ); RunIntNDoubleRandoms( fixRand ); } // Create a random object with a timer-generated seed. void AutoSeedRandoms() { // Wait to allow the timer to advance. Thread::Sleep( 1 ); Console::WriteLine( "\nRandom numbers from a Random object " "with an auto-generated seed:" ); Random^ autoRand = gcnew Random; RunIntNDoubleRandoms( autoRand ); } int main() { Console::WriteLine( "This example of the Random class constructors and Random" "::NextDouble( ) \ngenerates the following output.\n" ); Console::WriteLine( "Create Random objects, and then generate and " "display six integers and \nsix doubles from each." ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 123 ); FixedSeedRandoms( 456 ); FixedSeedRandoms( 456 ); AutoSeedRandoms(); AutoSeedRandoms(); AutoSeedRandoms(); } /* This example of the Random class constructors and Random::NextDouble( ) generates the following output. Create Random objects, and then generate and display six integers and six doubles from each. Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with an auto-generated seed: 1624372556 1894939458 302472229 588108304 23919954 1085111949 0.14595512 0.30162298 0.92267372 0.55707657 0.25430079 0.74143239 Random numbers from a Random object with an auto-generated seed: 2105952511 1753605347 280739490 876793040 1129567796 524571616 0.62652210 0.31846701 0.15984073 0.24458755 0.62160607 0.54857684 Random numbers from a Random object with an auto-generated seed: 440048819 1612271236 259006751 1165477776 87731991 2111514930 0.10708907 0.33531104 0.39700773 0.93209853 0.98891135 0.35572129 */
プラットフォームWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照
オーバーロードの一覧| 名前 | 説明 |
|---|---|
| Random () | 時間に応じて決定される既定のシード値を使用し、Random クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
| Random (Int32) | 指定したシード値を使用して Random クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
参照
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
| Next | オーバーロードされます。 乱数を返します。 |
| NextBytes | 指定したバイト配列の要素に乱数を格納します。 |
| NextDouble | 0.0 と 1.0 の間の乱数を返します。 |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
| ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
| Sample | 0.0 と 1.0 の間の乱数を返します。 |
参照擬似乱数ジェネレータを表します。擬似乱数ジェネレータは、乱数についての統計的な要件を満たす数値系列を生成するデバイスです。
Random データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
| Next | オーバーロードされます。 乱数を返します。 |
| NextBytes | 指定したバイト配列の要素に乱数を格納します。 |
| NextDouble | 0.0 と 1.0 の間の乱数を返します。 |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
| ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
| Sample | 0.0 と 1.0 の間の乱数を返します。 |
参照要素が偶然的過程によって選択された標本は、無作為標本 1ないし確率標本 1と呼ばれる。標本抽出単位の完全なリストが利用可能な場合、これは抽出枠 3と呼ばれる。単純無作為抽出 4では、ある割合の抽出単位が抽出枠から無作為に 2選択される。この割合は抽出率 5と呼ばれる。系統抽出標本 6は抽出単位が連続的に番号を付けられている枠から系統的に抽出される 7。標本はn番目、(n+s)番目、(n+2s)番目、・・・といった単位をとることによって選択される。この場合nはsより大きくなく、無作為に選択される。集落抽出法 8では、母集団の要素が個別に抽出されるのではなく、集落 9と呼ばれる集団単位で抽出される。
(Random から転送)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2026/03/05 01:51 UTC 版)
ランダム(英語: Random)とは、事象の発生に法則性(規則性)がなく、予測が不可能な状態である[注釈 1]。ランダムネス(英語: randomness)、無作為性(むさくいせい)ともいう。
事象・記号などのランダムな列には秩序がなく、理解可能なパターンや組み合わせに従わない。個々のランダムな事象は定義上予測不可能であるが、多くの場合、何度も試行した場合の結果の頻度は予測可能である。例えば、2つのサイコロを投げるとき、1回ごとの出目は予測できないが、合計が7になる頻度は4になる頻度の2倍になる。この見方では、ランダム性とは結果の不確実性の尺度であり、確率・情報エントロピーの概念に適用される。
数学、確率、統計の分野では、ランダム性の正式な定義が使用される。統計では、事象空間の起こり得る結果に数値を割り当てたものを確率変数(random variable[注釈 2])という。この関連付けは、事象の確率の識別および計算を容易にする。確率変数の列をランダム系列(random sequence)という。ランダム過程(不規則過程、確率過程)は、結果が決定論的パターンに従わず、確率分布によって記述される進化に従う確率変数の列である。これらの構造と他の構造は、確率論や様々なランダム性の応用に非常に有用である。
ランダム性は、よく定義された統計的特性を示すために統計で最も頻繁に使用される。ランダムな入力(乱数発生器や擬似乱数発生器など)に依存するモンテカルロ法は、計算科学などの科学において重要な技術である[1]。これに対し、準モンテカルロ法では乱数列ではなく一様分布列を使用している。
無作為抽出(random selection)は、ある項目を選択する確率が母集団内におけるその項目の割合と一致している集団から項目を選択する方法である。例えば、赤い石10個と青い石90個を入れた袋に入れた場合、この袋から何らかのランダム選択メカニズムによって石を1個選択した時にそれが赤い石である確率は1/10である。しかし、ランダム選択メカニズムによって実際に10個の石を選択したときに、それが赤1個・青9個であるとは限らない。母集団が識別可能な項目で構成されている状況では、ランダム選択メカニズムは、選択される項目に等しい確率を必要とする。つまり、選択プロセスが、母集団の各メンバー(例えば、研究対象)が選択される確率が同じである場合、選択プロセスはランダムであると言うことができる。
古代の歴史において、偶然性とランダム性の概念は運命の概念と絡み合っていた。古代、多くの人々は運命を決定するためにサイコロを投げたが、これは後に偶然性を利用したゲームに発展した。古代のほとんどの文化では、ランダム性と運命を回避するために様々な方法の占いを行っていた[2][3]。
3000年前の中国人は、おそらく、確率や偶然性を形式化した最も初期の人々だった。ギリシャの哲学者は、長さのランダム性について議論したが、非定量的な形でしかできなかった。16世紀になってイタリアの数学者たちは、様々な偶然性を利用したゲームに関連する確率を公式化し始めた。微積分の発明は、ランダム性の形式化の研究に良い影響を与えた。ジョン・ベンは著書 "The Logic of Chance" の1888年版で、乱数の概念についての章を書いた。その章には、2次元のランダムウォークを構築するのに円周率の数字のランダム性を使うことも含まれていた[4]。
20世紀初頭、確率の数学的基盤への様々なアプローチが導入されたことにより、ランダム性の形式的分析が急速に拡大した。20世紀半ばから後半にかけて、アルゴリズム情報理論のアイデアは、アルゴリズム的ランダム性という概念を介して、この分野に新しい次元をもたらした。
何世紀にもわたって、ランダム性は邪魔なもの・有害なものと見なされてきたが、20世紀にコンピュータ科学者は、ランダム性を意図的に計算に導入することが、より良いアルゴリズムを設計するための効果的なツールになることを認識し始めた。このようなランダム化されたアルゴリズムは、最良の決定論的手法よりも優れている場合もある。
多くの科学の分野がランダム性に関係している。
19世紀、科学者は、熱力学の現象や気体の性質を説明するための統計力学の発展に、分子のランダムな動きの概念を用いた。
量子力学のいくつかの標準解釈によれば、微視的現象は客観的にランダムである[5]。つまり、実験において、因果関係のある全てのパラメータを制御したとしても、結果のいくつかの側面は依然としてランダムに変化する。例えば、制御された環境に単一の不安定な原子が置かれている場合、原子が減衰するのにどれくらいの時間がかかるかを予測することはできない[6]。従って、量子力学では個々の実験の結果を特定するのではなく、確率のみを指定する。隠れた変数理論は、性質が既約のランダム性を含んでいるという見解を拒絶する。この理論では、無作為に見える過程において、ある統計的分布を有する特性が裏で働いて、それぞれの場合に結果を決定すると仮定する。
物性物理学では、“ランダムな系”と言うと、“乱れた系”という意味合いがある。ランダムな系としては、長距離秩序のないガラスやアモルファスがある(短距離秩序はあるので、その意味では完全なランダムではない)。
現代進化論では、観察される生物の多様性は、ランダムな突然変異とそれに続く自然選択に帰する。後者は、突然変異した遺伝子がそれを有する個体に与える生存と再生のための系統的に改善された機会のために、遺伝子プールにいくつかのランダムな変異を保持する。
何人か著者はまた、進化と発達には特定の形のランダム性、すなわち質的に新しい行動の出現を必要とすると主張する。いくつかの予め与えられたものからの可能性の選択の代わりに、このランダム性は新しい可能性の形成に対応する[7][8]。
生物の特性は、決定論的に(例えば、遺伝子および環境の影響下で)、そしてある程度ランダムに生じる。例えば、人の皮膚に現れるそばかすの密度は、遺伝子や光の暴露によって決まるが、個々のそばかすの正確な位置はランダムに見える[9]。
行動に関しては、動物が他者に予測不可能なやり方で行動するために、ランダム性が重要となる。例えば、飛行中の昆虫は方向がランダムに変化する傾向があり、捕食者が軌道を予測するのを困難にする。
確率の数学的理論は、偶然性のある事象の数学的記述を形式化するために生まれた。元々はギャンブルのためであったが、後に物理学と関連づけられた。統計は、経験的な観測の集合の基礎となる確率分布を推測するために使用される。シミュレーションの目的のためには、必要に応じて乱数やそれを生成する手段が必要となる。
アルゴリズム情報理論は、他のトピックの中で、何がランダム系列を構成するかを研究する。基本的な考え方は、あるビット列が、そのビット列を生成できるコンピュータプログラムよりも短い時かつその時に限り、そのビット列がランダムであるということである(コルモゴロフランダム性)。これは、ランダムなビット列は圧縮することができないということを意味する。この分野のパイオニアとしては、アンドレイ・コルモゴロフとその学生であるペール・マルティン=レーフ、レイ・ソロモノフ、グレゴリー・チャイティンらがいる。無限列の概念については、通常、マルティン=レーフの定義を使用する。つまり、無限列は、それが全ての再帰的に列挙可能なヌル集合に耐える時かつその時に限り、ランダムである。ランダム系列の他の概念には、再帰的ランダム性およびシュノアランダム性がある(ただしこれに限定されない)。これらは、再帰的に計算可能なマルチンゲールに基づく。これらのランダム性の概念は一般に異なっていることが宋詠璿によって示された[10]。
ランダム性は、log(2)や円周率(π)などの数値で発生する。πの小数部は無限の数列を構成し、循環的に繰り返されることはない。πなどの数字は正規数であると考えられている。これは、数字が統計的意味でランダムであることを意味する。
πは確かにこのように振る舞うようである。πの小数点以下の最初の60億桁では、0から9までの数字がそれぞれ約6億回現れる。しかし、このような結果はおそらく偶発的なものであっても、基数10であっても、他の数の基底での正規性がはるかに低いことを証明していない[11]。
統計学では、無秩序標本を作成するために一般にランダム性が使用される。これにより、完全にランダムなグループのアンケートで現実的なデータを提供することができる。これを行う一般的な方法には、くじを引く、乱数表を使用するなどがある。乱数表は、乱数の大きな表である。
情報科学では、無関係な、または無意味なデータはノイズとみなされる。ノイズは、統計的にランダム化された時間分布を伴う多数の一時的な外乱から構成される。
通信理論では、信号のランダム性は「ノイズ」と呼ばれ、信号源に起因する変動の成分である「信号」に相対している。
ランダムウォーク仮説では、組織された市場における資産価格は、変化の期待値はゼロであるが実際の価値は正または負となる可能性があるという意味で、ランダムに変動すると考える。より一般的には、資産価格は、一般的な経済環境における様々な予期せぬ出来事の影響を受ける。
日本産業規格では、「集会の要素をランダムな順序に並べる過程。母集団が1からnの自然数から成るとき,n!通りの順序が等しい確率で選ばれるとき,その選ばれた順序はランダムな順序とよばれる。」とランダム化を定義している[12]。
選挙の投票結果が引き分けになった場合に、当選者を決定するための公平な方法として、くじ引きなどのランダム選択が行われることがある[13]。
政治でのランダム性の使用は非常に古い。古代のアテネでは、公職者は希望する市民の中からくじ引きで選出されたため、投票は行われなかった。
日本では室町幕府6代将軍・足利義教がくじ引きによって選出された例として知られる。
過去と未来の全ての出来事を知っている全能の神によって宇宙が創造されたというような、いくつかの宗教における決定論的な考え方と、ランダム性は矛盾するとみなすことができる。宇宙が目的を持っているとするなら、偶然性は不可能であるとみなすことができる。これは、無作為な突然変異の結果であるという、進化に対する宗教的反対の根拠の一つである。
ヒンズー教と仏教の哲学は、業や因果という概念に反映されているように、いかなる出来事もそれ以前の出来事の結果であるとしており、偶然性のある出来事や、最初の出来事というものは存在しない。
いくつかの宗教では、乱数器として認識される手続きが占いに使用される。おみくじは、神の意思を知る手段としてくじを引くものである。
数学的、政治的、社会的、宗教的な用途の大部分で、ランダム性はその本来の公正さとバイアスの排除のために使用される。
システム内で(明らかに)ランダムな挙動の原因となる以下の3つのメカニズムが存在することが、一般に認められている。
計算機による乱数発生器が出現する以前、(統計上重要な)十分な乱数を大量に生成するには、多くの作業が必要だった。結果は時々収集され、乱数表として配布された。
二値数列のランダム性には多くの実用的な尺度がある。その中には、頻度、離散変換、複雑性に基づく、またはこれらを併用した尺度があり、Kak、Phillips、Yuen、Hopkins、Beth & Dai、Mund、Marsaglia & Zamanのテストなどがある[14]。
量子非局所性(Quantum Non-Locality))は、指定された数列の真のランダム性の存在を証明するために使用されている[15]。
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2022/05/14 19:56 UTC 版)
「beatmania IIDX」の記事における「RANDOM+」の解説
スクラッチオブジェも含めた8ラインでシャッフルを行う。コマンド入力後、RANDOMオプションまたはS-RANDOMオプションをオンにすることで適用される(後者では8ラインすべてのノートがノートごとにシャッフルされることになる)。AUTO-SCRATCHや5KEYSとの併用及びEXPERTモードでの使用はできない。
※この「RANDOM+」の解説は、「beatmania IIDX」の解説の一部です。
「RANDOM+」を含む「beatmania IIDX」の記事については、「beatmania IIDX」の概要を参照ください。