Results 1 to 12 of 12

Thread: Call a random string?

Threaded View

  1. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,389

    Re: Call a random string?

    I kind of need a method to "eliminate" a certain string I randomed
    Using the examples in the prior post I would go this route:
    Code:
    'Declare a new list of strings
        Dim strings As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Add text string1 - string25
            For i As Integer = 1 To 25 Step 1
                strings.Add("String" & i.ToString)
            Next
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Declare a new random and integer
            Dim r As New Random
    
            'The integer will equal a random number inbetween 0 and the number of number of items in the list(of string) - 1
            'So long as there are items in the list(of string)
            If strings.Count > 0 Then
                Dim int As Integer = r.Next(0, strings.Count - 1)
            Else
                MessageBox.Show("There is nothing left in the list")
            End If
    
            'The label's text will be the item number that is randomly chose above
            Label1.Text = strings.Item(Int)
    
            'Remove the string from the list so that it will never be used again
            strings.Remove(Label1.Text)
    
        End Sub
    The reason I would use a list(of string) is because the item can be removed fairly easily by removing the item that has label1's text.

    Edit - this uses the random at the form scope as suggested by shaggy and mentioned by JMc in other post:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        'Declare a new list of strings
        Dim strings As New List(Of String)
        Dim r As New Random
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Add text string1 - string25
            For i As Integer = 1 To 25 Step 1
                strings.Add("String" & i.ToString)
            Next
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Declare a new random and integer
    
            'The integer will equal a random number inbetween 0 and the number of number of items in the list(of string) - 1
            'So long as there are items in the list(of string)
            Dim int As Integer
            If strings.Count > 0 Then
                int = r.Next(0, strings.Count - 1)
            Else
                MessageBox.Show("There is nothing left in the list")
            End If
    
            'The label's text will be the item number that is randomly chose above
            Label1.Text = strings.Item(Int)
    
            'Remove the string from the list so that it will never be used again
            strings.Remove(Label1.Text)
    
        End Sub
    
    End Class
    Last edited by dday9; Aug 20th, 2012 at 11:31 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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