Random.Next() with no parameters
   Private Sub Rnd1()
        Dim rnd As New Random
        Dim sdx As String
        Dim i1 As Integer
        For xx = 1 To 5
            i1 = rnd.Next
            sdx = String.Format("{0,5}{1,15}", xx, i1)
            Console.WriteLine(sdx)
        Next
    End Sub
Output:
    1     1342868831
    2     1657067681
    3     2097100327
    4       86754970
    5      621041085

   

You have no control over the random numbers returned with Random.Next(). The are usually large and unconstrained.

Random.Next() with one parameter
  Private Sub Rnd2()
        Dim rnd As New Random
        Dim sdx As String
        Dim i1 As Integer
        For xx = 1 To 10
            i1 = rnd.Next(100) ' random numbers less than 100
            sdx = String.Format("{0,3}{1,12}", xx, i1)
            Console.WriteLine(sdx)
        Next
    End Sub

Output:
  1          47
  2          22
  3          52
  4          65
  5          40
  6          75
  7          19
  8           4
  9           2
 10          26
  
 

With Random.Next(intMax) you will get random numbers less than the parameter passed in.

Random.Next() with two parameters
  Private Sub Rnd3()
        ' simulate throwing two die 5 times
        Dim rnd1 As New Random(27) ' die #1 random seed value = 27
        Dim rnd2 As New Random(99) ' die #2 random seed value = 99 
        Dim sdx As String
        Dim i1, i2 As Integer
        For xx = 1 To 5
            i1 = rnd1.Next(1, 7) ' die #1 random number from 1 to 6
            i2 = rnd2.Next(1, 7) ' die #2 random number from 1 to 6
            sdx = String.Format("{0,-3} die #1 {1,-5} die #2 {2,-5}", xx, i1, i2)
            Console.WriteLine(sdx)
        Next
    End Sub

Output:
1   die #1 5     die #2 3    
2   die #1 5     die #2 6    
3   die #1 4     die #2 6    
4   die #1 2     die #2 5    
5   die #1 3     die #2 6    

 

Here you have full control (for integers) Parameter one is the minimum random number returned. Parameter two is one more than the maximum.

Random.Double

 Private Sub RndDbl()
        Dim rnd As New Random
        Dim sdx As String
        Dim db As Double
        For xx = 1 To 5
            db = rnd.NextDouble
            sdx = String.Format("{0,3}{1,12:F4}", xx, db)
            Console.WriteLine(sdx)
        Next
    End Sub

Output:
  1      0.7631
  2      0.9457
  3      0.5641
  4      0.6660
  5      0.9759


Random.Double returns a type double between zero and one

Random.NextBytes(byte array)

 Private Sub RndByte()
        Dim byt(4) As Byte ' five bytes long
        Dim rnd1 As New Random
        Dim sdx As String
        rnd1.NextBytes(byt) ' fill byt array with 5 random bytes (0 to 4)
        For xx = 0 To 4
            sdx = String.Format("{0,-5} {1,-5}", xx, byt(xx))
            Console.WriteLine(sdx)
        Next
    End Sub

Output:
0     157  
1     24   
2     124  
3     251  
4     227  


 

Declare an array of bytes. NextBytes will fill it with random bytes.

11/11/2015
TheLonelyProgrammer.Net
Alone in a sea of non-programmers
Home Random Regular expressions Create control at runtime Text files: Read & Write