vb System.Random.Next类(方法)实例源码

下面列出了vb System.Random.Next 类(方法)源码代码实例,从而了解它的用法。

作者:VB.NET开发    项目:Syste   
Module Example
   Public Sub Main()
      Console.Write("Number of random numbers to generate: ")
      Dim line As String = Console.ReadLine()
      Dim numbers As UInteger = 0
      Dim rnd As New Random()
      
      If Not UInt32.TryParse(line, numbers) Then numbers = 10
      
      For ctr As UInteger = 1 To numbers  
         Console.WriteLine("{0,15:N0}", rnd.Next())
      Next
   End Sub
End Module
' The example displays output like the following when asked to generate
' 15 random numbers:
'       Number of random numbers to generate: 15
'         1,733,189,596
'           566,518,090
'         1,166,108,546
'         1,931,426,514
'         1,341,108,291
'         1,012,698,049
'           890,578,409
'         1,377,589,722
'         2,108,384,181
'         1,532,939,448
'           762,207,767
'           815,074,920
'         1,521,208,785
'         1,950,436,671
'         1,266,596,666

作者:VB.NET开发    项目:Syste   
' 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

作者:VB.NET开发    项目:Syste   
' Example of the Random.Next( ) methods.
Module  RandomNextDemo

    ' Generate random numbers with no bounds specified.
    Sub NoBoundsRandoms( seed As Integer )

        Console.WriteLine( vbCrLf & _
            "Random object, seed = {0}, no bounds:", seed )
        Dim randObj As New Random( seed )
            
        ' Generate six random integers from 0 to int.MaxValue.
        Dim j As Integer
        For j = 0 To 5
            Console.Write( "{0,11} ", randObj.Next( ) )
        Next j
        Console.WriteLine( )
    End Sub 
        
    ' Generate random numbers with an upper bound specified.
    Sub UpperBoundRandoms( seed As Integer, upper As Integer )

        Console.WriteLine( vbCrLf & _
            "Random object, seed = {0}, upper bound = {1}:", _
            seed, upper )
        Dim randObj As New Random( seed )
            
        ' Generate six random integers from 0 to the upper bound.
        Dim j As Integer
        For j = 0 To 5
            Console.Write( "{0,11} ", randObj.Next( upper ) )
        Next j
        Console.WriteLine( )
    End Sub 
        
    ' Generate random numbers with both bounds specified.
    Sub BothBoundsRandoms( _
        seed As Integer, lower As Integer, upper As Integer )

        Console.WriteLine( vbCrLf & _
            "Random object, seed = {0}, lower = {1}, " & _
            "upper = {2}:", seed, lower, upper )
        Dim randObj As New Random( seed )
            
        ' Generate six random integers from the lower to 
        ' upper bounds.
        Dim j As Integer
        For j = 0 To 5
            Console.Write( "{0,11} ", _
                randObj.Next( lower, upper ) )
        Next j
        Console.WriteLine( )
    End Sub 
        
    Sub Main( )
        Console.WriteLine( _
            "This example of the Random.Next( ) methods" & _ 
            vbCrLf & "generates the following output." & vbCrLf )
        Console.WriteLine( _
            "Create Random objects all with the same seed " & _
            "and generate" & vbCrLf & "sequences of numbers " & _
            "with different bounds. Note the effect " & vbCrLf & _
            "that the various combinations " & _
            "of bounds have on the sequences." )
            
        NoBoundsRandoms( 234 )

        UpperBoundRandoms( 234, Int32.MaxValue )
        UpperBoundRandoms( 234, 2000000000 )
        UpperBoundRandoms( 234, 200000000 )

        BothBoundsRandoms( 234, 0, Int32.MaxValue )
        BothBoundsRandoms( 234, Int32.MinValue, Int32.MaxValue )
        BothBoundsRandoms( 234, -2000000000, 2000000000 )
        BothBoundsRandoms( 234, -200000000, 200000000 )
        BothBoundsRandoms( 234, -2000, 2000 )
    End Sub 
End Module 

' This example of the Random.Next( ) methods

作者:VB.NET开发    项目:Syste   
Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim malePetNames() As String = { "Rufus", "Bear", "Dakota", "Fido", 
                                    "Vanya", "Samuel", "Koani", "Volodya", 
                                    "Prince", "Yiska" }
      Dim femalePetNames() As String = { "Maggie", "Penny", "Saya", "Princess", 
                                         "Abby", "Laila", "Sadie", "Olivia", 
                                         "Starlight", "Talla" }                                      
      
      ' Generate random indexes for pet names.
      Dim mIndex As Integer = rnd.Next(malePetNames.Length)
      Dim fIndex As Integer = rnd.Next(femalePetNames.Length)
      
      ' Display the result.
      Console.WriteLine("Suggested pet name of the day: ")
      Console.WriteLine("   For a male:     {0}", malePetNames(mIndex))
      Console.WriteLine("   For a female:   {0}", femalePetNames(fIndex))
   End Sub
End Module

作者:VB.NET开发    项目:Syste   
Module Example
   Public Sub Main()
      Dim rnd As New Random()

      Console.WriteLine("20 random integers from -100 to 100:")
      For ctr As Integer = 1 To 20
         Console.Write("{0,6}", rnd.Next(-100, 101))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next
      Console.WriteLine()
      
      Console.WriteLine("20 random integers from 1000 to 10000:")      
      For ctr As Integer = 1 To 20
         Console.Write("{0,8}", rnd.Next(1000, 10001))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next
      Console.WriteLine()
      
      Console.WriteLine("20 random integers from 1 to 10:")
      For ctr As Integer = 1 To 20
         Console.Write("{0,6}", rnd.Next(1, 11))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next
   End Sub
End Module

作者:VB.NET开发    项目:Syste   
Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim malePetNames() As String = { "Rufus", "Bear", "Dakota", "Fido", 
                                    "Vanya", "Samuel", "Koani", "Volodya", 
                                    "Prince", "Yiska" }
      Dim femalePetNames() As String = { "Maggie", "Penny", "Saya", "Princess", 
                                         "Abby", "Laila", "Sadie", "Olivia", 
                                         "Starlight", "Talla" }                                      
      
      ' Generate random indexes for pet names.
      Dim mIndex As Integer = rnd.Next(0, malePetNames.Length)
      Dim fIndex As Integer = rnd.Next(0, femalePetNames.Length)
      
      ' Display the result.
      Console.WriteLine("Suggested pet name of the day: ")
      Console.WriteLine("   For a male:     {0}", malePetNames(mIndex))
      Console.WriteLine("   For a female:   {0}", femalePetNames(fIndex))
   End Sub
End Module

作者:VB程序    项目:Syste   
' 导入命名空间
Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())

        Dim friends() As String = {"1", "2", "3","4", "5"}


        Dim upperBound As Integer = friends.Length

        Dim random As New System.Random()

        Dim n As Integer
        For n = 1 To 10
            Dim index As Integer = random.Next(upperBound)
            Console.WriteLine(index & ": " & friends(index))
        Next
    End Sub
    

End Class


问题


面经


文章

微信
公众号

扫码关注公众号