davidxc davidxc - 2 months ago 6
Vb.net Question

My randomized Array list is not working

I have the following VB code. I'm trying to get it to randomize a quote to be printed to the page on refresh, but it's not doing it for me.

Is there something obviously wrong?

<%


Dim quotes As New ArrayList, quoters As New ArrayList
quotes.Add("The Warehouse excites me every single time"): quoters.Add("- Samantha Jones")
quotes.Add("Everyone is just so friendly"): quoters.Add("- Bianca Emit")
quotes.Add("A great place to explore new things!"): quoters.Add("- Adrian Soft")
quotes.Add("I absolutely LOVE The Warehouse!!!!!!!!!!!!!"): quoters.Add("- Adam Jenkins")
quotes.Add("I look forward to it every week!"): quoters.Add("- Mary Binge")


Randomize
Dim quoteNumber As Byte = CInt * (quotes.Count - 1)


Response.Write("""" & quotes(quoteNumber) & """<br />" & vbCrLf)
Response.Write(quoters(quoteNumber))

%>

Answer

Randomize just initialises the random number generator. You need to call Rnd() to generate a random number between 0 and 1. Then multiply by the range you need. ie count - 1

Dim quoteNumber as Integer = CInt( Rnd() * (quotes.Count - 1) )
Comments