Random.Sample メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



次に示すのは、Random からクラスを派生させ、Sample メソッドをオーバーライドしてランダムな数値の分布を生成するコード例です。この分布は、基本クラスの Sample メソッドによって生成される一様な分布とは異なります。
' Example of the Random.Sample( ) method. Imports System Imports Microsoft.VisualBasic Module RandomSampleDemo ' This derived class converts the uniformly distributed random ' numbers generated by base.Sample( ) to another distribution. Public Class RandomProportional Inherits Random ' Sample generates a distribution proportional to the value ' of the random numbers, in the range [ 0.0, 1.0 ). Protected Overrides Function Sample( ) As Double Return Math.Sqrt( MyBase.Sample( ) ) End Function End Class Sub Main( ) Const rows As Integer = 4, cols As Integer = 6 Const runCount As Integer = 1000000 Const distGroupCount As Integer = 10 Const intGroupSize As Double = _ ( CDbl( Integer.MaxValue ) + 1.0 ) / _ CDbl( distGroupCount ) Dim randObj As New RandomProportional( ) Dim intCounts( distGroupCount ) As Integer Dim realCounts( distGroupCount ) As Integer Dim i As Integer, j As Integer Console.WriteLine( "This example of Random.Sample( ) " & _ "generates the following output." ) Console.WriteLine( vbCrLf & _ "The derived RandomProportional class overrides " & _ "the Sample method to " & vbCrLf & _ "generate random numbers in the range " & _ "[0.0, 1.0). The distribution " & vbCrLf & _ "of the numbers is proportional to the number " & _ "values. For example, " & vbCrLf & _ "numbers are generated in the vicinity of 0.75 " & _ "with three times " & vbCrLf & "the " & _ "probability of those generated near 0.25." ) Console.WriteLine( vbCrLf & _ "Random doubles generated with the NextDouble( ) " & _ "method:" & vbCrLf ) ' Generate and display [rows * cols] random doubles. For i = 0 To rows - 1 For j = 0 To cols - 1 Console.Write( "{0,12:F8}", randObj.NextDouble( ) ) Next j Console.WriteLine( ) Next i Console.WriteLine( vbCrLf & _ "Random integers generated with the Next( ) " & _ "method:" & vbCrLf ) ' Generate and display [rows * cols] random integers. For i = 0 To rows - 1 For j = 0 To cols - 1 Console.Write( "{0,12}", randObj.Next( ) ) Next j Console.WriteLine( ) Next i Console.WriteLine( vbCrLf & _ "To demonstrate the proportional distribution, " & _ "{0:N0} random " & vbCrLf & _ "integers and doubles are grouped into {1} " & _ "equal value ranges. This " & vbCrLf & _ "is the count of values in each range:" & vbCrLf, _ runCount, distGroupCount ) Console.WriteLine( "{0,21}{1,10}{2,20}{3,10}", _ "Integer Range", "Count", "Double Range", "Count" ) Console.WriteLine( "{0,21}{1,10}{2,20}{3,10}", _ "-------------", "-----", "------------", "-----" ) ' Generate random integers and doubles, and then count ' them by group. For i = 0 To runCount - 1 intCounts( Fix( CDbl( randObj.Next( ) ) / _ intGroupSize ) ) += 1 realCounts( Fix( randObj.NextDouble( ) * _ CDbl( distGroupCount ) ) ) += 1 Next i ' Display the count of each group. For i = 0 To distGroupCount - 1 Console.WriteLine( _ "{0,10}-{1,10}{2,10:N0}{3,12:N5}-{4,7:N5}{5,10:N0}", _ Fix( CDbl( i ) * intGroupSize ), _ Fix( CDbl( i + 1 ) * intGroupSize - 1.0 ), _ intCounts( i ), _ CDbl( i ) / CDbl( distGroupCount), _ CDbl( i + 1 ) / CDbl( distGroupCount ), _ realCounts( i ) ) Next i End Sub End Module ' This example of Random.Sample( ) generates the following output. ' ' The derived RandomProportional class overrides the Sample method to ' generate random numbers in the range [0.0, 1.0). The distribution ' of the numbers is proportional to the number values. For example, ' numbers are generated in the vicinity of 0.75 with three times ' the probability of those generated near 0.25. ' ' Random doubles generated with the NextDouble( ) method: ' ' 0.79518674 0.68830640 0.19325108 0.87495776 0.94214344 0.43960633 ' 0.80547067 0.18420252 0.67677629 0.20395900 0.14804654 0.76552601 ' 0.32595198 0.99375793 0.92964386 0.49341930 0.93379207 0.97324697 ' 0.26255379 0.87768855 0.99630002 0.60385777 0.94307202 0.60923691 ' ' Random integers generated with the Next( ) method: ' ' 1903578996 1413274413 1420485191 1091348055 1706557467 2039662942 ' 1192987687 1795987411 1719118883 1433622886 856129071 840337946 ' 1829524044 1709600961 1795736148 1667275347 935419628 1077895508 ' 669189095 251371582 1826628229 1848954211 1218382751 1938456460 ' ' To demonstrate the proportional distribution, 1,000,000 random ' integers and doubles are grouped into 10 equal value ranges. This ' is the count of values in each range: ' ' Integer Range Count Double Range Count ' ------------- ----- ------------ ----- ' 0- 214748363 9,852 0.00000-0.10000 10,113 ' 214748364- 429496728 29,921 0.10000-0.20000 30,213 ' 429496729- 644245093 50,243 0.20000-0.30000 49,963 ' 644245094- 858993458 69,747 0.30000-0.40000 70,304 ' 858993459-1073741823 90,743 0.40000-0.50000 90,075 ' 1073741824-1288490187 110,004 0.50000-0.60000 109,749 ' 1288490188-1503238552 129,605 0.60000-0.70000 129,941 ' 1503238553-1717986917 150,184 0.70000-0.80000 149,703 ' 1717986918-1932735282 169,624 0.80000-0.90000 169,923 ' 1932735283-2147483647 190,077 0.90000-1.00000 190,016
// Example of the Random.Sample( ) method. using System; // This derived class converts the uniformly distributed random // numbers generated by base.Sample( ) to another distribution. public class RandomProportional : Random { // Sample generates a distribution proportional to the value // of the random numbers, in the range [ 0.0, 1.0 ). protected override double Sample( ) { return Math.Sqrt( base.Sample( ) ); } } public class RandomSampleDemo { static void Main( ) { const int rows = 4, cols = 6; const int runCount = 1000000; const int distGroupCount = 10; const double intGroupSize = ( (double)int.MaxValue + 1.0 ) / (double)distGroupCount; RandomProportional randObj = new RandomProportional( ); int[ ] intCounts = new int[ distGroupCount ]; int[ ] realCounts = new int[ distGroupCount ]; Console.WriteLine( "This example of Random.Sample( ) " + "generates the following output." ); Console.WriteLine( "\nThe derived RandomProportional class overrides " + "the Sample method to \ngenerate random numbers " + "in the range [0.0, 1.0). The distribution \nof " + "the numbers is proportional to the number values. " + "For example, \nnumbers are generated in the " + "vicinity of 0.75 with three times the \n" + "probability of those generated near 0.25." ); Console.WriteLine( "\nRandom doubles generated with the NextDouble( ) " + "method:\n" ); // Generate and display [rows * cols] random doubles. for( int i = 0; i < rows; i++ ) { for( int j = 0; j < cols; j++ ) Console.Write( "{0,12:F8}", randObj.NextDouble( ) ); Console.WriteLine( ); } Console.WriteLine( "\nRandom integers generated with the Next( ) " + "method:\n" ); // Generate and display [rows * cols] random integers. for( int i = 0; i < rows; i++ ) { for( int j = 0; j < cols; j++ ) Console.Write( "{0,12}", randObj.Next( ) ); Console.WriteLine( ); } Console.WriteLine( "\nTo demonstrate the proportional distribution, " + "{0:N0} random \nintegers and doubles are grouped " + "into {1} equal value ranges. This \n" + "is the count of values in each range:\n" , runCount, distGroupCount ); Console.WriteLine( "{0,21}{1,10}{2,20}{3,10}", "Integer Range", "Count", "Double Range", "Count" ); Console.WriteLine( "{0,21}{1,10}{2,20}{3,10}", "-------------", "-----", "------------", "-----" ); // Generate random integers and doubles, and then count // them by group. for( int i = 0; i < runCount; i++ ) { intCounts[ (int)( (double)randObj.Next( ) / intGroupSize ) ]++; realCounts[ (int)( randObj.NextDouble( ) * (double)distGroupCount ) ]++; } // Display the count of each group. for( int i = 0; i < distGroupCount; i++ ) Console.WriteLine( "{0,10}-{1,10}{2,10:N0}{3,12:N5}-{4,7:N5}{5,10:N0}", (int)( (double)i * intGroupSize ), (int)( (double)( i + 1 ) * intGroupSize - 1.0 ), intCounts[ i ], ( (double)i ) / (double)distGroupCount, ( (double)( i + 1 ) ) / (double)distGroupCount, realCounts[ i ] ); } } /* This example of Random.Sample( ) generates the following output. The derived RandomProportional class overrides the Sample method to generate random numbers in the range [0.0, 1.0). The distribution of the numbers is proportional to the number values. For example, numbers are generated in the vicinity of 0.75 with three times the probability of those generated near 0.25. Random doubles generated with the NextDouble( ) method: 0.70274545 0.71861388 0.68071795 0.84034066 0.93354743 0.85663774 0.81804688 0.35177836 0.59208519 0.96031602 0.80442745 0.68718948 0.98765094 0.88136820 0.40016694 0.78735843 0.30468930 0.60884722 0.99724610 0.64792400 0.87542366 0.86193142 0.88573527 0.67682807 Random integers generated with the Next( ) method: 2143307129 1985560852 1491542209 1624708626 545912171 2144440214 1605065299 1294719830 1191410879 1120886902 1915435155 1514194175 1795364867 1695595242 1754564804 1407165303 2026939619 1965958920 1531822446 1145720706 1458838319 1924643339 804498107 445927707 To demonstrate the proportional distribution, 1,000,000 random integers and doubles are grouped into 10 equal value ranges. This is the count of values in each range: Integer Range Count Double Range Count ------------- ----- ------------ ----- 0- 214748363 9,916 0.00000-0.10000 10,014 214748364- 429496728 29,978 0.10000-0.20000 29,965 429496729- 644245093 50,204 0.20000-0.30000 49,975 644245094- 858993458 69,870 0.30000-0.40000 70,150 858993459-1073741823 89,875 0.40000-0.50000 90,180 1073741824-1288490187 110,448 0.50000-0.60000 109,995 1288490188-1503238552 130,290 0.60000-0.70000 130,218 1503238553-1717986917 149,652 0.70000-0.80000 149,300 1717986918-1932735282 170,367 0.80000-0.90000 169,737 1932735283-2147483647 189,400 0.90000-1.00000 190,466 */
// Example of the Random::Sample( ) method. using namespace System; // This derived class converts the uniformly distributed random // numbers generated by Random::Sample( ) to another distribution. ref class RandomProportional: public Random { protected: // Sample generates a distribution proportional to the value // of the random numbers, in the range [ 0.0, 1.0 ). virtual double Sample() override { return Math::Sqrt( Random::Sample() ); } }; int main() { const int rows = 4,cols = 6; const int runCount = 1000000; const int distGroupCount = 10; const double intGroupSize = ((double)Int32::MaxValue + 1.0) / (double)distGroupCount; RandomProportional^ randObj = gcnew RandomProportional; array<Int32>^intCounts = gcnew array<Int32>(distGroupCount); array<Int32>^realCounts = gcnew array<Int32>(distGroupCount); Console::WriteLine( "This example of Random::Sample( ) " "generates the following output." ); Console::WriteLine( "\nThe derived RandomProportional class overrides " "the Sample method to \ngenerate random numbers " "in the range [0.0, 1.0). The distribution \nof " "the numbers is proportional to the number values. " "For example, \nnumbers are generated in the " "vicinity of 0.75 with three times the \n" "probability of those generated near 0.25." ); Console::WriteLine( "\nRandom doubles generated with the NextDouble( ) " "method:\n" ); // Generate and display [rows * cols] random doubles. for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < cols; j++ ) Console::Write( "{0,12:F8}", randObj->NextDouble() ); Console::WriteLine(); } Console::WriteLine( "\nRandom integers generated with the Next( ) " "method:\n" ); // Generate and display [rows * cols] random integers. for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < cols; j++ ) Console::Write( "{0,12}", randObj->Next() ); Console::WriteLine(); } Console::WriteLine( "\nTo demonstrate the proportional distribution, " "{0:N0} random \nintegers and doubles are grouped " "into {1} equal value ranges. This \n" "is the count of values in each range:\n", runCount, distGroupCount ); Console::WriteLine( "{0,21}{1,10}{2,20}{3,10}", "Integer Range", "Count", "Double Range", "Count" ); Console::WriteLine( "{0,21}{1,10}{2,20}{3,10}", "-------------", "-----", "------------", "-----" ); // Generate random integers and doubles, and then count // them by group. for ( int i = 0; i < runCount; i++ ) { intCounts[ (int)((double)randObj->Next() / intGroupSize) ]++; realCounts[ (int)(randObj->NextDouble() * (double)distGroupCount) ]++; } // Display the count of each group. for ( int i = 0; i < distGroupCount; i++ ) Console::WriteLine( "{0,10}-{1,10}{2,10:N0}{3,12:N5}-{4,7:N5}{5,10:N0}", (int)((double)i * intGroupSize), (int)((double)(i + 1) * intGroupSize - 1.0), intCounts[ i ], ((double)i) / (double)distGroupCount, ((double)(i + 1)) / (double)distGroupCount, realCounts[ i ] ); } /* This example of Random::Sample( ) generates the following output. The derived RandomProportional class overrides the Sample method to generate random numbers in the range [0.0, 1.0). The distribution of the numbers is proportional to the number values. For example, numbers are generated in the vicinity of 0.75 with three times the probability of those generated near 0.25. Random doubles generated with the NextDouble( ) method: 0.90754836 0.90324018 0.53861915 0.83566028 0.44578707 0.62163577 0.86498862 0.93616774 0.32436787 0.58532497 0.71350292 0.59563486 0.60962757 0.82088684 0.88599136 0.48414132 0.41008009 0.21823831 0.79440201 0.89928828 0.93307960 0.94641322 0.37148990 0.71451612 Random integers generated with the Next( ) method: 1517769790 462406172 2069039901 2021289775 1384348700 231007353 870275497 1597105824 2056133305 1952619691 2016467783 2090386391 1792965387 959068365 1939110818 846901181 1940820549 1375295268 1327509272 1241610292 2040733733 1588387399 2017439082 1175907328 To demonstrate the proportional distribution, 1,000,000 random integers and doubles are grouped into 10 equal value ranges. This is the count of values in each range: Integer Range Count Double Range Count ------------- ----- ------------ ----- 0- 214748363 9,930 0.00000-0.10000 10,052 214748364- 429496728 30,047 0.10000-0.20000 30,261 429496729- 644245093 49,764 0.20000-0.30000 50,154 644245094- 858993458 70,366 0.30000-0.40000 70,391 858993459-1073741823 90,028 0.40000-0.50000 89,683 1073741824-1288490187 110,111 0.50000-0.60000 110,406 1288490188-1503238552 129,261 0.60000-0.70000 129,410 1503238553-1717986917 150,266 0.70000-0.80000 149,900 1717986918-1932735282 169,875 0.80000-0.90000 170,101 1932735283-2147483647 190,352 0.90000-1.00000 189,642 */

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からRandom.Sample メソッドを検索する場合は、下記のリンクをクリックしてください。

- Random.Sample メソッドのページへのリンク