Random Number Generation

  • Thread starter Thread starter GraemeFBaillie
  • Start date Start date
G

GraemeFBaillie

Guest
I'm writing a jukebox in Visual Studio vb.net. I'm attempting to utilise random number generators to swap the image of the record label being played (which works fine)

Dim r As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
' Returns a random number, between the optional Low and High parameters
Return r.Next(Low, High + 1)

End Function

gRecordLabelImage = gDefaultLabelImage(GetRandomNumber(0, gDefaultLabelImageCount - 1))



However, when trying to utilise this code to get the next 5 records to be played (when implementing random play), the code calls work first time through but give me the same 5 random numbers on the next call.

For LoopCounter = 1 To 5
Counter = GetRandomNumber(1,TracksDataSet.Count - 1)
gTrackFileName = TracksDataSet(Counter).PlayListTrackFileName
Next

I've tried RNGCryptoService Provider



Dim Provider As New RNGCryptoServiceProvider()
Public Function GenerateRandom(ByRef MaxValue As Integer) As Integer
Dim result As Integer
Using Provider
Dim data As Byte() = New Byte(3) {}
Provider.GetBytes(data)
result = (BitConverter.ToUInt32(data, 0) Mod MaxValue) + 1
End Using
Return Result
End Function
For LoopCounter = 1 to 5
Counter = GenerateRandom(TracksDataSet.Count)
gTrackFileName = TracksDataSet(Counter).PlayListTrackFileName
Next

But this does exactly the same. Please note though that the 5 repeated random numbers are different every time code is started, they just repeat while code is running.



I've worn myself out in Google and web forums - they all say the same - the code should work, so what is my problem. Any Help gratefully accepted



thanks - Graeme Baillie

Continue reading...
 

Similar threads

Back
Top