• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET Random.Sample方法代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.Random.Sample方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Random.Sample方法的具体用法?VB.NET Random.Sample怎么用?VB.NET Random.Sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Random的用法示例。



在下文中一共展示了Random.Sample方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: RandomProportional

' This derived class converts the uniformly distributed random 
' numbers generated by base.Sample( ) to another distribution.
Public Class RandomProportional
   Inherits Random

   ' The Sample method 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
   
   Public Overrides Function [Next]() As Integer
      Return Sample() * Integer.MaxValue
   End Function 
End Class 

Module RandomSampleDemo
    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( 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 their numeric " & _
            "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
开发者ID:VB.NET开发者,项目名称:System,代码行数:99,代码来源:Random.Sample

输出:

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 their numeric 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.28377004  0.75920598  0.33430371  0.66720626  0.97080243  0.27353772
0.17787962  0.54618410  0.08145080  0.56286100  0.99002910  0.64898614
0.27673277  0.99455281  0.93778966  0.76162002  0.70533771  0.44375798
0.55939883  0.87383136  0.66465779  0.77392566  0.42393411  0.82409159

Random integers generated with the Next( ) method:

1364479914  1230312341  1657373812  1526222928   988564704   700078020
1801013705  1541517421  1146312560   338318389  1558995993  2027260859
884520932  1320070465   570200106  1027684711   943035246  2088689333
630809089  1705728475  2140787648  2097858166  1863010875  1386804198

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,892     0.00000-0.10000     9,928
214748364- 429496728    30,341     0.10000-0.20000    30,101
429496729- 644245093    49,958     0.20000-0.30000    49,964
644245094- 858993458    70,099     0.30000-0.40000    70,213
858993459-1073741823    90,801     0.40000-0.50000    89,553
1073741824-1288490187   109,699     0.50000-0.60000   109,427
1288490188-1503238552   129,438     0.60000-0.70000   130,339
1503238553-1717986917   149,886     0.70000-0.80000   150,000
1717986918-1932735282   170,338     0.80000-0.90000   170,128
1932735283-2147483647   189,548     0.90000-1.00000   190,347



注:本文中的System.Random.Sample方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET Single.Equals方法代码示例发布时间:2022-05-24
下一篇:
VB.NET Object.Equals方法代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap