user1346419
user1346419

Reputation: 21

String Randomizer

A friend asked me to make a character string randomizer for a game she plays. I got the code working but now she wants to have the characters seperated every third character with a dash (or a space), but I can't get it to work. Any help will be appreciated.

            For x As Integer = 1 To 10
                  strb.Append(chars(Int(Rnd() * UpperBound)))
          If x Mod 2 = 0 Then 
          strb.Append("-")

The characters need to be separated, but how do I prevent the added dash at the end of the randomized characters?

Thanks guys (and girls) for your help it's working

Upvotes: 2

Views: 353

Answers (4)

Kratz
Kratz

Reputation: 4340

If I'm understanding you correctly, change the if statement from If x Mod 2 = 0 Then to If x Mod 2 = 0 And x <> 10 Then. That way it wont append the dash if it is on the last element.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460288

Since i haven't used those old VB function yet, i'll show you how to do it with VB.NET which could also be converted to C# easily:

Private Function RandomString(rnd As Random, size As Integer) As String
    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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
    Dim builder As New System.Text.StringBuilder()
    For i As Integer = 0 To size - 1
        builder.Append(chars(rnd.Next(0, chars.Length)))
    Next

    Return builder.ToString()
End Function

Let us generate a random string with 5 chars:

Dim rnd = New Random()
Dim rndString = RandomString(rnd, 5)

Note that i'm passing the random instance since. If i would create it in the method and call it in a loop, it would generate the same strings since the Random would use the same time as seed.

Edit: I've only just seen that you need to separate the result. You could use an overloaded version:

Private Overloads Function RandomString(rnd As Random, size As Integer, separator As String, everyNChar As Int32) As String
    Dim builder = 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", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
    For i As Integer = 0 To size - 1
        If i > 0 AndAlso i Mod everyNChar = 0 Then
            builder.Append(chars(rnd.Next(0, chars.Length))).Append(separator)
        Else
            builder.Append(chars(rnd.Next(0, chars.Length)))
        End If
    Next

    Return builder.ToString()
End Function

Note that this will not count the separators.

Upvotes: 1

AYK
AYK

Reputation: 3322

You can use Regular Expressions to replace strings. It will be simple to change the regex and manage change requests :)

just replace TextBox1.Text = rndstring by :

TextBox1.Text = Regex.Replace(rndstring, "[a-zA-Z0-9]{3}", "$& ")

Note that you need to add Imports System.Text.RegularExpressions if not already done.

EDIT : I have given the above solution to insert a " " (whitespace) after every third character. If you want a hyphen (-) just change the second regex to "$&-"

Upvotes: 0

Oded
Oded

Reputation: 499352

Just append a dash (or whatever) every third character:

For x As Integer = 1 To 5
  strb.Append(chars(Int(Rnd() * UpperBound)))
  If x Mod 3 = 0 Then      
    strb.Append("-")
  End If
Next

Upvotes: 2

Related Questions