-1

I have a chat system and i want to put a "random string generator".

In my chat i have to write "%random%" and it is replaces with a random string.

I have a problem though, if i type "%random%%random%%random%" for example, it will generate the same string 3 times.

• Here is my function:

Public Function getRandomString(ByVal len As Integer) As String
    Randomize()

    Dim stringMap as string = "abcdefghijklmnopqrstuwvxyz0123456789"
    Dim rndString As String = ""
    Dim rnd As New Random()

    For i As Integer = 0 To len - 1
        Randomize()
        rndString &= stringMap.Substring(rnd.Next(0, stringMap.Length), 1)
    Next

    Return rndString
End Function

• And here is my function call:

    Dim msg As String = "Random string: %random%%random%%random%"
    msg = msg.Replace("%random%", getRandomString(8))
    MsgBox(msg)

The output for example: Random string: 5z15if725z15if725z15if72

I guess this is because it keeps the 1st return value in memory and pastes it, how can i fix that ?

Do i have to make a string.replace function myself ? Thanks

Riptide
  • 385
  • 7
  • 17
  • 1
    Old story, never call Randomize inside a loop. See this C# but it is the same http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – Steve Dec 16 '14 at 18:02
  • I'm afraid you'll have to create your own replace. You can take a look at [Regex](http://msdn.microsoft.com/en-us/library/System.Text.RegularExpressions.Regex.Replace(v=vs.110).aspx) it might offer what you want. – the_lotus Dec 16 '14 at 18:27
  • Steve: Thanks for the tip, i tried to remove/move the randomize() calls plus doing it with the "Lock" method but i can't create curly brackets blocks like c# the_lotus: Do you really think so ? That's problematic – Riptide Dec 16 '14 at 19:03

2 Answers2

1

Oh no! You shouldn't call Randomize() here at all! Random is used in combination with the Rnd() function of VB. Creating a new Random object is enough here.
The reason you are getting the same results every time is because you are creating a new Random every time. You should reuse the same object to get different results.

'Create the object once
Private Shared rnd As New Random()

Public Function getRandomString(ByVal len As Integer) As String
    Dim stringMap as string = "abcdefghijklmnopqrstuwvxyz0123456789"
    Dim rndString As String = ""

    For i As Integer = 0 To len - 1
        rndString &= stringMap.Substring(rnd.Next(0, stringMap.Length), 1)
    Next

    Return rndString
End Function

EDIT: I realize that in addition to the above changes, you need to call the getRandomString function for every "%random%". String.Replace only calls the function once and pastes the result everywhere. With Regex, you could do something like this:

msg = new Regex("%random%").Replace(input, Function (match) getRandomString(8))
Simon Farshid
  • 2,636
  • 1
  • 22
  • 31
  • Unfortunately, i tried this, but it's not working, same results, help appreciated though – Riptide Dec 16 '14 at 19:01
  • Still the same results.. Damn you clock :p Is it impossible to randomize() somewhere to change the seed ? – Riptide Dec 16 '14 at 19:08
  • Updated my answer, the issue was unrelated to my latest fix. – Simon Farshid Dec 16 '14 at 19:15
  • I think i got it, it's what i had in mind, but there's an exception, if i write: msg = New Regex("%random%").Replace(msg, Function(match) return getRandomString(8)) It's throwing an exception and telling that the replace method can't be called with these arguments (kinda hard to translate it from fr to en) if i write this instead msg = New Regex("%random%").Replace(msg, getRandomString(8)) same string appear 3 times, like the string.replace method. – Riptide Dec 16 '14 at 19:39
0

An easy way to do it is to find the first occurrence of "%random%", replace that, then repeat as necessary.

Written as a console application:

Option Infer On

Module Module1

    Dim rand As New Random

    Public Function getRandomString(ByVal len As Integer) As String

        Dim stringMap As String = "abcdefghijklmnopqrstuwvxyz0123456789"
        Dim rndString As String = ""

        For i As Integer = 0 To len - 1
            rndString &= stringMap.Substring(rand.Next(0, stringMap.Length), 1)
        Next

        Return rndString

    End Function

    Function ReplaceRandoms(s As String) As String
        Dim stringToReplace = "%random%"
        Dim r = s.IndexOf(stringToReplace)
        While r >= 0
            s = s.Remove(r, stringToReplace.Length).Insert(r, getRandomString(stringToReplace.Length))
            r = s.IndexOf(stringToReplace)
        End While

        Return s

    End Function

    Sub Main()
        Dim msg As String = "Random string: %random%%random%%random%"
        msg = ReplaceRandoms(msg)
        Console.WriteLine(msg)

        Console.ReadLine()

    End Sub

End Module
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84