Random string generator based on format/rules?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Random string generator based on format/rules?

  1. #1
    Join Date
    Dec 2007
    Posts
    3

    Random string generator based on format/rules?

    Hey guys, for my VB course in college I have to make an application that can output random characters based on certain rules. I've been taking VB for about a semester now, and I'm familiar with most of the basics (If...Then...Else, For...Next, Select Case, Function, Sub, etc.) but this is the first assignment I've come to that I'm having trouble with.

    Basically I made a simple GUI with a button and a read-only textbox. Basically, all I want to do is add the code I need to the button's event handler to make it output a random string in the textbox.

    However, for extra marks I'm trying to make it so that the random string will output according to a format I specify.

    For example, what would I need to do (or what code would I need) to make it so that when a user clicks the button it will output the string in the a format such as:
    number, letter, number, letter, number

    So, when they click the button, the textbox will display:
    5B3Q9

    And have it so that each time the user clicks the button, the numbers and letters are random and different?

    Thanks a lot!

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX

    We won't write the code for you, but we will help you write the code for yourself.

    What do you have so far that isn't working?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Dec 2007
    Posts
    3
    Well, right now all I have so far is an If statement that says if a checkbox is selected (.Checked = True), then outputTextBox.Text = "5B3Q9"
    See I thought maybe I could just make it to randomly pick a string out of a list when clicked, but I realize now that wouldn't be very efficient.

    The reason I'm here is because I really have no idea where to start as far as randomizing a string selection for an output textbox =/

  4. #4
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Randomly pick a string out of what list?

    Where/what is the list you have the contains existing strings that you would like to randomly choose from?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  5. #5
    Join Date
    Dec 2007
    Posts
    3
    Well, I don't actually have one. But I figured there might be a way to just type a list of possible "random" strings that it could output and then it when the user clicked the button it would just select one and display it. But that's not exactly the same, and I don't know how I would go about doing that either.

  6. #6
    Join Date
    Nov 2007
    Posts
    43
    Use the "Rnd" code thing to generate a random number from 0 to 1.
    You can then use that (Like this, "Text1.text = int(rnd * 10)") Which will create a number from 0 to 9.
    Int(Rnd * 26 + 1) creates a number from 1 to 26. *HintCoughAlphabet*

  7. #7
    Join Date
    Nov 2007
    Posts
    43
    Also, I forgot to add, you should make a timer that has an interval of 1 and make the code inside of it just say Rnd. Even though VB says it is a random number, it is from a list that is always the same. This way it will accually be random.

  8. #8
    Join Date
    Nov 2007
    Posts
    43
    Create a form with a textbox named "Text1", a timer named "Timer1", and a command button named "Command1". The open up the code window and delete all of the stuff inside and paste in this code:
    Code:
    Dim A As Variant
    
    Private Sub Command1_Click()
    Text1.Text = Int(Rnd * 10)
    A = Int(Rnd * 26 + 1)
    Letter A
    Text1.Text = Text1.Text + A
    Text1.Text = Text1.Text + Trim(Str(Int(Rnd * 10)))
    A = Int(Rnd * 26 + 1)
    Letter A
    Text1.Text = Text1.Text + A
    Text1.Text = Text1.Text + Trim(Str(Int(Rnd * 10)))
    End Sub
    
    Public Sub Letter(A As Variant)
    If A = 1 Then A = "A"
    If A = 2 Then A = "B"
    If A = 3 Then A = "C"
    If A = 4 Then A = "D"
    If A = 5 Then A = "E"
    If A = 6 Then A = "F"
    If A = 7 Then A = "G"
    If A = 8 Then A = "H"
    If A = 9 Then A = "I"
    If A = 10 Then A = "J"
    If A = 11 Then A = "K"
    If A = 12 Then A = "L"
    If A = 13 Then A = "M"
    If A = 14 Then A = "N"
    If A = 15 Then A = "O"
    If A = 16 Then A = "P"
    If A = 17 Then A = "Q"
    If A = 18 Then A = "R"
    If A = 19 Then A = "S"
    If A = 20 Then A = "T"
    If A = 21 Then A = "U"
    If A = 22 Then A = "V"
    If A = 23 Then A = "W"
    If A = 24 Then A = "X"
    If A = 25 Then A = "Y"
    If A = 26 Then A = "Z"
    End Sub
    
    Private Sub Timer1_Timer()
    Rnd
    End Sub

Similar Threads

  1. Submitting Infopath to a .net Web Service
    By athomas42 in forum .NET
    Replies: 1
    Last Post: 06-25-2007, 04:54 PM
  2. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  3. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  4. Replies: 0
    Last Post: 06-22-2000, 07:30 AM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center