Results 1 to 2 of 2

Thread: Randomize A Number String

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Randomize A Number String

    Say I have a number string like "853517".

    I would like to randomize this string and come up with a completely different 6 digit number.

    Please don't ask me to post what I've done so far because I have no clue where or how to start.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2006
    Location
    TeXaS
    Posts
    497

    Re: Randomize A Number String

    i just wrote this one, hope it meets your needs. if anyone has a smaller or faster version, feel free to post it.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        
        Dim St As String
        
        St = "853517"
        MsgBox RandomizeString(St)
    
    End Sub
    Private Function RandomizeString(ByVal MyString As String) As String
    
        Dim i As Long
        Dim Ran As Long
        Dim NewString As String
        Dim Ln As Long
        
        Randomize
        
        Ln = Len(MyString) 'i used a variable to simplify having to call this repeatedly
        NewString = Space$(Ln) 'make a buffer to store the new string
        
        For i = Ln To 1 Step -1
            Ran = Int(i * Rnd) + 1 'return a random position
            Mid$(NewString, Ln - i + 1, 1) = Mid$(MyString, Ran, 1)
            Mid$(MyString, Ran, 1) = Mid$(MyString, i, 1)
        Next i
        
        RandomizeString = NewString
    
    End Function
    Last edited by Billy Conner; Feb 27th, 2007 at 11:35 AM. Reason: cannot get the code tag to work anymore :P

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