Results 1 to 4 of 4

Thread: Help with random in a url.

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    2

    Question Help with random in a url.

    Hello vb forums this is my first post here,
    So,I am trying to make a program that gets random images from http://prntscr.com/.
    My main concept is: Name:  uuOtrVg.jpg
Views: 395
Size:  11.9 KB
    you click the random button and it will generate a random string of 5 numbers/lowercase letters that will be added after http://prntscr.com/ like http://prntscr.com/58whco and then the image will show up in the web browser.

    the code I have now:
    Code:
    Public Class Form1
    
        Public Function RandomString(ByVal length As Integer) As String
            Dim sb As New System.Text.StringBuilder
            Dim chars() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", _
                   "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
            Dim upperBound As Integer = UBound(chars)
    
            For x As Integer = 5 To length
                sb.Append(chars(Int(Rnd() * upperBound)))
            Next
    
            Return sb.ToString
    
        End Function
    
    
        Private Sub FButton2_Click(sender As Object, e As EventArgs) Handles FButton2.Click
    
        End Sub
    
        Private Sub FButton1_Click(sender As Object, e As EventArgs) Handles FButton1.Click
    
    
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    
            Me.Close()
    
        End Sub
    End Class
    Thanks in advance.

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    2

    Re: Help with random in a url.

    Just found that:
    Code:
    Public Function CheckURL(ByVal urltocheck As String)
            Dim url As New System.Uri("http://" & urltocheck)
            Dim req As System.Net.WebRequest
            req = System.Net.WebRequest.Create(url)
            Dim resp As System.Net.WebResponse
            Try
                resp = req.GetResponse()
                resp.Close()
                req = Nothing
                Return True
            Catch ex As Exception
                req = Nothing
                Return False
            End Try
        End Function
    in the code bank maybe I can add it to check if the url exist to make it so it will skip unused urls?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Help with random in a url.

    Yeah, you might as well. You should also stop using Rnd in favor of the Random object that was introduced in .NET.
    My usual boring signature: Nothing

  4. #4
    Lively Member ShadowTzu's Avatar
    Join Date
    Oct 2014
    Location
    France
    Posts
    68

    Re: Help with random in a url.

    I agree with previous comment and I add just a little update:

    Code:
    Private mRandom As New Random
    Private mchars As String = "abcdefghijklmnopqrstuvwxyz1234567890"
    Public Function RandomString(ByVal length As Integer) As String
    Dim sb As New System.Text.StringBuilder
    For x As Integer = 0 To Math.Max(4, length - 1)
        sb.Append(mchars(mRandom.Next(mchars.Length)))
    Next
    
    Return sb.ToString
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width