RandomString Function (a gift for all :))-VBForums
Results 1 to 6 of 6

Thread: RandomString Function (a gift for all :))

  1. #1

    Thread Starter
    Addicted Member Programmation's Avatar
    Join Date
    Nov 2009
    Posts
    161

    RandomString Function (a gift for all :))

    Hello everyone

    I had created a simple function enables you to get a Random String consisting of a capital or small characters as you like (The function return capital and small characters as default) and with the ability of specify the count of characters which return (the function will return a five characters as default).

    This is my function as a gift for all:

    Code:
    'Author: Programmation(OmarNegm)
    Public Function RandomString(Optional ByVal CharactersCount As Integer = 5, Optional ByVal LCaseString As Boolean = False, Optional ByVal UCaseString As Boolean = False) As String
    Dim Chrs As String, LstStr As String
    Dim I As Integer, X As Integer, Y As Integer
    '---------------------------
    Chrs = "abcdefghijklmnopqrstuvwxyz"
    If LCaseString = True Then
         Chrs = Chrs
    ElseIf UCaseString = True Then
         Chrs = UCase(Chrs)
    Else
         Chrs = LCase(Chrs) & UCase(Chrs)
    End If
    '---------------------------
    I = 0
    X = Len(Chrs)
    LstStr = ""
    Do Until I = CharactersCount
       Y = Round(Rnd * (X))
         If Not Y = 0 Then
           If LstStr = "" Then
             LstStr = Mid$(Chrs, Y, 1)
             I = I + 1
            Else
             LstStr = LstStr & Mid$(Chrs, Y, 1)
             I = I + 1
           End If
         End If
    Loop
    '---------------------------
    RandomString = LstStr
    End Function
    Using function:

    Copy the above code into an Module then you can calling the function from any where in your project as the following:

    All of the following codes will return a five small character:

    Code:
    Text1.Text = RandomString(, True)
    Code:
    Text1.Text = RandomString(, True, False)
    Code:
    Text1.Text = RandomString(5, True, False)
    All of the following codes will return a 10 capital character:

    Code:
    Text1.Text = RandomString(10, , True)
    Code:
    Text1.Text = RandomString(10, False, True)
    All of the following codes will return a 20 small and capital characters:

    Code:
    Text1.Text = RandomString(20)
    Code:
    Text1.Text = RandomString(20, False, False)
    Use what you want, and if you have any suggestion please tell me that

    I hope that be useful for you, You can share it to any one (but please do not clear the first line in the code )

    Thanks
    Last edited by Programmation; Nov 17th, 2010 at 04:38 PM.
    Just Do It!

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,640

    Re: RandomString Function (a gift for all :))

    Nice

    But I think, this thread is more suited in the CodeBank

    Here's a tutorial for random numbers: http://www.vbforums.com/showthread.php?t=281172

    The following will give you a value between 1 and the value X:
    Code:
    Y=Int(Rnd * X)+1

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: RandomString Function (a gift for all :))

    Moved Into The Codebank
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    Re: RandomString Function (a gift for all :))

    You should enclose your code in either CODE tags or HIGHLIGHT (for VB highlighting) tags. You have it in PHP tags.

  5. #5

    Thread Starter
    Addicted Member Programmation's Avatar
    Join Date
    Nov 2009
    Posts
    161

    Re: RandomString Function (a gift for all :))

    Quote Originally Posted by danecook21 View Post
    You should enclose your code in either CODE tags or HIGHLIGHT (for VB highlighting) tags. You have it in PHP tags.
    Thanks for your comment
    Just Do It!

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    16,964

    Re: RandomString Function (a gift for all :))

    As mentioned by akhileshbc in post #2, using the Rnd() function more efficiently, your loop is reduced to this:
    Code:
    For I = 1 To CharactersCount
        LstStr = LstStr & Mid$(Chrs, Int(Rnd * X) + 1, 1)
    Next
    There are several more efficiency tweaks that could be made, but that would be nitpicking
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width