当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Random类代码示例

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


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

示例1: Example

Module Example
   Public Sub Main()
      ' Instantiate random number generator using system-supplied value as seed.
      Dim rand As New Random()
      ' Generate and display 5 random byte (integer) values.
      Dim bytes(4) As Byte
      rand.NextBytes(bytes)
      Console.WriteLine("Five random byte values:")
      For Each byteValue As Byte In bytes
         Console.Write("{0, 5}", byteValue)
      Next
      Console.WriteLine()   
      ' Generate and display 5 random integers.
      Console.WriteLine("Five random integer values:")
      For ctr As Integer = 0 To 4
         Console.Write("{0,15:N0}", rand.Next)
      Next                     
      Console.WriteLine()
      ' Generate and display 5 random integers between 0 and 100.'
      Console.WriteLine("Five random integers between 0 and 100:")
      For ctr As Integer = 0 To 4
         Console.Write("{0,8:N0}", rand.Next(101))
      Next                     
      Console.WriteLine()
      ' Generate and display 5 random integers from 50 to 100.
      Console.WriteLine("Five random integers between 50 and 100:")
      For ctr As Integer = 0 To 4
         Console.Write("{0,8:N0}", rand.Next(50, 101))
      Next                     
      Console.WriteLine()
      ' Generate and display 5 random floating point values from 0 to 1.
      Console.WriteLine("Five Doubles.")
      For ctr As Integer = 0 To 4
         Console.Write("{0,8:N3}", rand.NextDouble())
      Next                     
      Console.WriteLine()
      ' Generate and display 5 random floating point values from 0 to 5.
      Console.WriteLine("Five Doubles between 0 and 5.")
      For ctr As Integer = 0 To 4
         Console.Write("{0,8:N3}", rand.NextDouble() * 5)
      Next                     
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:43,代码来源:Random

输出:

Five random byte values:
194  185  239   54  116
Five random integer values:
507,353,531  1,509,532,693  2,125,074,958  1,409,512,757    652,767,128
Five random integers between 0 and 100:
16      78      94      79      52
Five random integers between 50 and 100:
56      66      96      60      65
Five Doubles.
0.943   0.108   0.744   0.563   0.415
Five Doubles between 0 and 5.
2.934   3.130   0.292   1.432   4.369

示例2: Example

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
开发者ID:VB.NET开发者,项目名称:System,代码行数:20,代码来源:Random

输出:

Suggested pet name of the day:
For a male:     Koani
For a female:   Maggie

示例3: modMain

Module modMain

   Public Sub Main()
      Dim bytes1(99), bytes2(99) As Byte
      Dim rnd1 As New Random()
      Dim rnd2 As New Random()
      
      rnd1.NextBytes(bytes1)
      rnd2.NextBytes(bytes2)
      
      Console.WriteLine("First Series:")
      For ctr As Integer = bytes1.GetLowerBound(0) to bytes1.GetUpperBound(0)
         Console.Write("{0, 5}", bytes1(ctr))
         If (ctr + 1) Mod 10 = 0 Then Console.WriteLine()
      Next 
      Console.WriteLine()
      Console.WriteLine("Second Series:")        
      For ctr As Integer = bytes2.GetLowerBound(0) to bytes2.GetUpperBound(0)
         Console.Write("{0, 5}", bytes2(ctr))
         If (ctr + 1) Mod 10 = 0 Then Console.WriteLine()
      Next   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:23,代码来源:Random

输出:

First Series:
97  129  149   54   22  208  120  105   68  177
113  214   30  172   74  218  116  230   89   18
12  112  130  105  116  180  190  200  187  120
7  198  233  158   58   51   50  170   98   23
21    1  113   74  146  245   34  255   96   24
232  255   23    9  167  240  255   44  194   98
18  175  173  204  169  171  236  127  114   23
167  202  132   65  253   11  254   56  214  127
145  191  104  163  143    7  174  224  247   73
52    6  231  255    5  101   83  165  160  231

Second Series:
97  129  149   54   22  208  120  105   68  177
113  214   30  172   74  218  116  230   89   18
12  112  130  105  116  180  190  200  187  120
7  198  233  158   58   51   50  170   98   23
21    1  113   74  146  245   34  255   96   24
232  255   23    9  167  240  255   44  194   98
18  175  173  204  169  171  236  127  114   23
167  202  132   65  253   11  254   56  214  127
145  191  104  163  143    7  174  224  247   73
52    6  231  255    5  101   83  165  160  231

示例4: Example

' 导入命名空间
Imports System.Threading

Module Example
   <ThreadStatic> Dim previous As Double = 0.0
   <ThreadStatic> Dim perThreadCtr As Integer = 0
   <ThreadStatic> Dim perThreadTotal As Double = 0.0  
   Dim source As New CancellationTokenSource()
   Dim countdown As New CountdownEvent(1) 
   Dim randLock As New Object()
   Dim numericLock As New Object()
   Dim rand As New Random()
   Dim totalValue As Double = 0.0
   Dim totalCount As Integer = 0
   
   Public Sub Main()
      Thread.CurrentThread.Name = "Main"

      Dim token As CancellationToken = source.Token 
      For threads As Integer = 1 To 10
         Dim newThread As New Thread(AddressOf GetRandomNumbers)
         newThread.Name = threads.ToString()
         newThread.Start(token)
      Next
      GetRandomNumbers(token)
      
      countdown.Signal()
      ' Make sure all threads have finished.
      countdown.Wait()

      Console.WriteLine()
      Console.WriteLine("Total random numbers generated: {0:N0}", totalCount)
      Console.WriteLine("Total sum of all random numbers: {0:N2}", totalValue)
      Console.WriteLine("Random number mean: {0:N4}", totalValue/totalCount)
   End Sub

   Private Sub GetRandomNumbers(o As Object)
      Dim token As CancellationToken = CType(o, CancellationToken)
      Dim result As Double = 0.0
      countdown.AddCount(1)
         
      Try  
         For ctr As Integer = 1 To 2000000
            ' Make sure there's no corruption of Random.
            token.ThrowIfCancellationRequested()

            SyncLock randLock
               result = rand.NextDouble()
            End SyncLock
            ' Check for corruption of Random instance.
            If result = previous AndAlso result = 0 Then 
               source.Cancel()
            Else 
               previous = result
            End If
            perThreadCtr += 1
            perThreadTotal += result
         Next      
       
         Console.WriteLine("Thread {0} finished execution.", 
                           Thread.CurrentThread.Name)
         Console.WriteLine("Random numbers generated: {0:N0}", perThreadCtr)
         Console.WriteLine("Sum of random numbers: {0:N2}", perThreadTotal)
         Console.WriteLine("Random number mean: {0:N4}", perThreadTotal/perThreadCtr)
         Console.WriteLine()
         
         ' Update overall totals.
         SyncLock numericLock
            totalCount += perThreadCtr
            totalValue += perThreadTotal  
         End SyncLock
      Catch e As OperationCanceledException
         Console.WriteLine("Corruption in Thread {1}", e.GetType().Name, Thread.CurrentThread.Name)
      Finally 
         countdown.Signal()
         source.Dispose()
      End Try
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:79,代码来源:Random

输出:

Thread 6 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,491.05
Random number mean: 0.5002

Thread 10 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,329.64
Random number mean: 0.4997

Thread 4 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,166.89
Random number mean: 0.5001

Thread 8 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,628.37
Random number mean: 0.4998

Thread Main finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,920.89
Random number mean: 0.5000

Thread 3 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,370.45
Random number mean: 0.4997

Thread 7 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,330.92
Random number mean: 0.4997

Thread 9 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,172.79
Random number mean: 0.5001

Thread 5 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,079.43
Random number mean: 0.5000

Thread 1 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,817.91
Random number mean: 0.4999

Thread 2 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,930.63
Random number mean: 0.5000


Total random numbers generated: 22,000,000
Total sum of all random numbers: 10,998,238.98
Random number mean: 0.4999

示例5: Example

' 导入命名空间
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Dim source As New CancellationTokenSource()
   Dim randLock As New Object()
   Dim numericLock As New Object()
   Dim rand As New Random()
   Dim totalValue As Double = 0.0
   Dim totalCount As Integer = 0
   
   Public Sub Main()
      Dim tasks As New List(Of Task)()
      
      For ctr As Integer = 1 To 10
         Dim token As CancellationToken = source.Token 
         Dim taskNo As Integer = ctr
         tasks.Add(Task.Run( 
                   Sub()
                      Dim previous As Double = 0.0
                      Dim taskCtr As Integer = 0
                      Dim taskTotal As Double = 0.0
                      Dim result As Double = 0.0

                      For n As Integer = 1 To 2000000
                         ' Make sure there's no corruption of Random.
                         token.ThrowIfCancellationRequested()
      
                         SyncLock randLock
                           result = rand.NextDouble()
                         End SyncLock
                         ' Check for corruption of Random instance.
                         If result = previous AndAlso result = 0 Then
                           source.Cancel()
                         Else 
                           previous = result
                         End If
                        taskCtr += 1
                        taskTotal += result
                      Next   

                      ' Show result.
                     Console.WriteLine("Task {0} finished execution.", taskNo)
                     Console.WriteLine("Random numbers generated: {0:N0}", taskCtr)
                     Console.WriteLine("Sum of random numbers: {0:N2}", taskTotal)
                     Console.WriteLine("Random number mean: {0:N4}", taskTotal/taskCtr)
                     Console.WriteLine()
                     
                     ' Update overall totals.
                     SyncLock numericLock
                        totalCount += taskCtr
                        totalValue += taskTotal  
                     End SyncLock
                   End Sub, token))
      Next

      Try
         Task.WaitAll(tasks.ToArray())
         Console.WriteLine()
         Console.WriteLine("Total random numbers generated: {0:N0}", totalCount)
         Console.WriteLine("Total sum of all random numbers: {0:N2}", totalValue)
         Console.WriteLine("Random number mean: {0:N4}", totalValue/totalCount)
      Catch e As AggregateException
         For Each inner As Exception In e.InnerExceptions
            Dim canc As TaskCanceledException = TryCast(inner, TaskCanceledException)
            If canc IsNot Nothing Then
               Console.WriteLine("Task #{0} cancelled.", canc.Task.Id)
            Else
               Console.WriteLine("Exception: {0}", inner.GetType().Name)
            End If   
         Next         
      Finally
         source.Dispose()
      End Try
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:78,代码来源:Random

输出:

Task 1 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,502.47
Random number mean: 0.5003

Task 0 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,445.63
Random number mean: 0.5002

Task 2 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,556.04
Random number mean: 0.5003

Task 3 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,178.87
Random number mean: 0.5001

Task 4 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,819.17
Random number mean: 0.4999

Task 5 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,190.58
Random number mean: 0.5001

Task 6 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,720.21
Random number mean: 0.4999

Task 7 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,000.96
Random number mean: 0.4995

Task 8 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,499.33
Random number mean: 0.4997

Task 9 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 1,000,193.25
Random number mean: 0.5001

Task 10 finished execution.
Random numbers generated: 2,000,000
Sum of random numbers: 999,960.82
Random number mean: 0.5000


Total random numbers generated: 22,000,000
Total sum of all random numbers: 11,000,067.33
Random number mean: 0.5000

示例6: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim bytes(19) As Byte
      rnd.NextBytes(bytes)  
      For ctr As Integer = 1 To bytes.Length
         Console.Write("{0,3}   ", bytes(ctr - 1))
         If ctr Mod 10 = 0 Then Console.WriteLine()
      Next 
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:11,代码来源:Random

输出:

141    48   189    66   134   212   211    71   161    56
181   166   220   133     9   252   222    57    62    62

示例7: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      For ctr As Integer = 0 To 9
         Console.Write("{0,3}   ", rnd.Next(-10, 11))
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:8,代码来源:Random

输出:

2     9    -3     2     4    -7    -3    -8    -8     5

示例8: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      For ctr As Integer = 0 To 9
         Console.Write("{0,-19:R}   ", rnd.NextDouble())
         If (ctr + 1) Mod 3 = 0 Then Console.WriteLine()
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:9,代码来源:Random

输出:

0.7911680553998649    0.0903414949264105    0.79776258291572455    
0.615568345233597     0.652644504165577     0.84023809378977776   
0.099662564741290441  0.91341467383942321   0.96018602045261581   
0.74772306473354022

示例9: Example

' 导入命名空间
Imports System.Threading

Module Example
   Public Sub Main()
      Console.WriteLine("Instantiating two random number generators...")
      Dim rnd1 As New Random()
      Thread.Sleep(2000)
      Dim rnd2 As New Random()
      Console.WriteLine()
      
      Console.WriteLine("The first random number generator:")
      For ctr As Integer = 1 To 10
         Console.WriteLine("   {0}", rnd1.Next())
      Next  
      Console.WriteLine()
       
      Console.WriteLine("The second random number generator:")
      For ctr As Integer = 1 To 10
         Console.WriteLine("   {0}", rnd2.Next())
      Next   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:23,代码来源:Random

输出:

Instantiating two random number generators...

The first random number generator:
643164361
1606571630
1725607587
2138048432
496874898
1969147632
2034533749
1840964542
412380298
47518930

The second random number generator:
1251659083
1514185439
1465798544
517841554
1821920222
195154223
1538948391
1548375095
546062716
897797880

示例10: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      For ctr As Integer = 1 To 15
         Console.Write("{0,3}    ", rnd.Next(-10, 11))
         If ctr Mod 5 = 0 Then Console.WriteLine()
      Next   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:9,代码来源:Random

输出:

-2     -5     -1     -2     10
-3      6     -4     -8      3
-7     10      5     -2      4

示例11: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      For ctr As Integer = 1 To 50
         Console.Write("{0,3}    ", rnd.Next(1000, 10000))
         If ctr Mod 10 = 0 Then Console.WriteLine()
      Next   
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:9,代码来源:Random

输出:

9570    8979    5770    1606    3818    4735    8495    7196    7070    2313
5279    6577    5104    5734    4227    3373    7376    6007    8193    5540
7558    3934    3819    7392    1113    7191    6947    4963    9179    7907
3391    6667    7269    1838    7317    1981    5154    7377    3297    5320
9869    8694    2684    4949    2999    3019    2357    5211    9604    2593

示例12: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      For ctr As Integer = 1 To 10
         Console.WriteLine(rnd.NextDouble() - 1)
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:8,代码来源:Random

输出:

-0.930412760437658
-0.164699016215605
-0.9851692803135
-0.43468508843085
-0.177202483255976
-0.776813320245972
-0.0713201854710096
-0.0912875561468711
-0.540621722368813
-0.232211863730201

示例13: Example

Module Example
   Public Sub Main()
      Const ONE_TENTH As Long = 922337203685477581

      Dim rnd As New Random()
      Dim number As Long
      Dim count(9) As Integer
      
      ' Generate 20 million integer values.
      For ctr As Integer = 1 To 20000000
         number = CLng(rnd.NextDouble() * Int64.MaxValue)
         ' Categorize random numbers.
         count(CInt(number \ ONE_TENTH)) += 1
      Next
      ' Display breakdown by range.
      Console.WriteLine("{0,28} {1,32}   {2,7}", "Range", "Count", "Pct.")
      Console.WriteLine()
      For ctr As Integer = 0 To 9
         Console.WriteLine("{0,25:N0}-{1,25:N0}  {2,8:N0}   {3,7:P2}", ctr * ONE_TENTH,
                            If(ctr < 9, ctr * ONE_TENTH + ONE_TENTH - 1, Int64.MaxValue), 
                            count(ctr), count(ctr)/20000000)
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:24,代码来源:Random

输出:

Range                            Count      Pct.

0-  922,337,203,685,477,580  1,996,148    9.98 %
922,337,203,685,477,581-1,844,674,407,370,955,161  2,000,293   10.00 %
1,844,674,407,370,955,162-2,767,011,611,056,432,742  2,000,094   10.00 %
2,767,011,611,056,432,743-3,689,348,814,741,910,323  2,000,159   10.00 %
3,689,348,814,741,910,324-4,611,686,018,427,387,904  1,999,552   10.00 %
4,611,686,018,427,387,905-5,534,023,222,112,865,485  1,998,248    9.99 %
5,534,023,222,112,865,486-6,456,360,425,798,343,066  2,000,696   10.00 %
6,456,360,425,798,343,067-7,378,697,629,483,820,647  2,001,637   10.01 %
7,378,697,629,483,820,648-8,301,034,833,169,298,228  2,002,870   10.01 %
8,301,034,833,169,298,229-9,223,372,036,854,775,807  2,000,303   10.00 %

示例14: Example

Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim lowerBound As Integer = 10
      Dim upperBound As Integer = 11
      Dim range(9) As Integer
      For ctr As Integer = 1 To 1000000
         Dim value As Double = rnd.NextDouble() * (upperBound - lowerBound) + lowerBound
         range(CInt(Math.Truncate((value - lowerBound) * 10))) += 1 
      Next
      
      For ctr As Integer = 0 To 9
         Dim lowerRange As Double = 10 + ctr * .1
         Console.WriteLine("{0:N1} to {1:N1}: {2,8:N0}  ({3,7:P2})", 
                           lowerRange, lowerRange + .1, range(ctr), 
                           range(ctr) / 1000000.0)
      Next 
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:19,代码来源:Random

输出:

10.0 to 10.1:   99,929  ( 9.99 %)
10.1 to 10.2:  100,189  (10.02 %)
10.2 to 10.3:   99,384  ( 9.94 %)
10.3 to 10.4:  100,240  (10.02 %)
10.4 to 10.5:   99,397  ( 9.94 %)
10.5 to 10.6:  100,580  (10.06 %)
10.6 to 10.7:  100,293  (10.03 %)
10.7 to 10.8:  100,135  (10.01 %)
10.8 to 10.9:   99,905  ( 9.99 %)
10.9 to 11.0:   99,948  ( 9.99 %)

示例15: Example

Module Example
   Public Sub Main()
      ' Instantiate the Boolean generator.
      Dim boolGen As New BooleanGenerator()
      Dim totalTrue, totalFalse As Integer 
      
      ' Generate 1,0000 random Booleans, and keep a running total.
      For ctr As Integer = 0 To 9999999
          Dim value As Boolean = boolGen.NextBoolean()
          If value Then
             totalTrue += 1
          Else
             totalFalse += 1
          End If
      Next
      Console.WriteLine("Number of true values:  {0,7:N0} ({1:P3})", 
                        totalTrue, 
                        totalTrue/(totalTrue + totalFalse))
      Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})", 
                        totalFalse, 
                        totalFalse/(totalTrue + totalFalse))
   End Sub                     
End Module

Public Class BooleanGenerator
   Dim rnd As Random
   
   Public Sub New()
      rnd = New Random()
   End Sub

   Public Function NextBoolean() As Boolean
      Return Convert.ToBoolean(rnd.Next(0, 2))
   End Function
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:35,代码来源:Random

输出:

Number of true values:  500,004 (50.000 %)
Number of false values: 499,996 (50.000 %)


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