How to randonly read a single line from a text file using

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 288
The below program will do so

Imports System.IO
Imports System.Linq

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
Dim fileName As String = "D:\Input.txt"

Console.WriteLine(File.ReadLines(fileName).ToList()(GetRandomNumbers(1, 20)))
End Sub

'generate random numbers
Private Shared ReadOnly random As New Random()
Private Shared ReadOnly [syncLock] As New Object()
Private Shared Function GetRandomNumbers(minVal As Integer, maxVal As Integer) As Integer
SyncLock [syncLock]
Return random.[Next](minVal, maxVal)
End SyncLock
End Function
End Class
End Namespace

Comments or Responses

Login to post response