views:

292

answers:

3

Hi there. I've build a random string generator but I'm having a problem whereby if I call the function multiple times say in a Page_Load method, the function returns the same string twice.

here's the code

Public Class CustomStrings
    ''' <summary>'
    ''' Generates a Random String'
    ''' </summary>'
    ''' <param name="n">number of characters the method should generate</param>'
    ''' <param name="UseSpecial">should the method include special characters? IE: # ,$, !, etc.</param>'
    ''' <param name="SpecialOnly">should the method include only the special characters and excludes alpha numeric</param>'
    ''' <returns>a random string n characters long</returns>'
    Public Function GenerateRandom(ByVal n As Integer, Optional ByVal UseSpecial As Boolean = True, Optional ByVal SpecialOnly As Boolean = False) As String

        Dim chars As String() ' a character array to use when generating a random string'
        Dim ichars As Integer = 74 'number of characters to use out of the chars string'
        Dim schars As Integer = 0 ' number of characters to skip out of the characters string'

        chars = { _
         "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", _
         "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", "!", "@", "#", "$", _
         "%", "^", "&", "*", "(", ")", _
         "-", "+"}


        If Not UseSpecial Then ichars = 62 ' only use the alpha numeric characters out of "char"'
        If SpecialOnly Then schars = 62 : ichars = 74 ' skip the alpha numeric characters out of "char"'

        Dim rnd As New Random()
        Dim random As String = String.Empty
        Dim i As Integer = 0
        While i < n
            random += chars(rnd.[Next](schars, ichars))
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        rnd = Nothing
        Return random
    End Function
End Class

but if I call something like this

    Dim rnd1 As New CustomStrings
    Dim rnd2 As New CustomStrings

    Dim str1 As String = rnd1.GenerateRandom(5) 
    Dim str2 As String = rnd2.GenerateRandom(5) 

    rnd1 = Nothing
    rnd2 = Nothing

the response will be something like this

g*3Jq
g*3Jq

and the second time I call it, it will be

3QM0$
3QM0$

What am I missing? I'd like every random string to be generated as unique.

+6  A: 

The reason for this is that when you construct an instance of the Random class, it seeds itself from the clock, but the accuracy of this clock is not good enough to produce a new seed on every call, if you call it in rapid succession.

In other words, this:

Random r = new Random();
int i = r.Next(1000);
r = new Random();
int j = r.Next(1000);

has a very high probability of producing the same values in i and j.

What you need to do is to:

  • Create and cache the Random instance, so that it is the same instance used for every call (but unfortunately the class isn't thread-safe, so at least keep a cached copy per thread)
  • Seed it with something that changes for each call (which is quite a bit harder, because seeding it with a sequential value will product predictable random numbers)

Here's a sample program that creates a separate Random instance per thread, and seeds those instances from a global random-object. Again, this might produce predictable sequences.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SO2755146
{
    public class Program
    {
        public static void Main()
        {
            List<Task> tasks = new List<Task>();
            for (int index = 0; index < 1000; index++)
                tasks.Add(Task.Factory.StartNew(() => Console.Out.WriteLine(RNG.Instance.Next(1000))));
            Task.WaitAll(tasks.ToArray());
        }
    }

    public static class RNG
    {
        private static Random _GlobalSeed = new Random();
        private static object _GlobalSeedLock = new object();

        [ThreadStatic]
        private static Random _Instance;

        public static Random Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (_GlobalSeedLock)
                    {
                        _Instance = new Random(_GlobalSeed.Next());
                    }
                }
                return _Instance;
            }
        }
    }
}

If you just want to seed each random instance from the clock, but at least produce random sequences per thread, you can simplify it like this:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SO2755146
{
    public class Program
    {
        public static void Main()
        {
            List<Task> tasks = new List<Task>();
            for (int index = 0; index < 1000; index++)
                tasks.Add(Task.Factory.StartNew(() => Console.Out.WriteLine(RNG.Instance.Next(1000))));
            Task.WaitAll(tasks.ToArray());
        }
    }

    public static class RNG
    {
        [ThreadStatic]
        private static Random _Instance;

        public static Random Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new Random();

                return _Instance;
            }
        }
    }
}

This might make two threads started very close to each other to be seeded with the same value, so there's a tradeoff.

Lasse V. Karlsen
I thought that might be the case. I don't care if it's "predictable" but I would like to know what you mean by "seed". would it be enough to use `System.Threading.Thread.Sleep(10)` or something along those lines?
rockinthesixstring
That would slow down your program, do you really want to do that?
Lasse V. Karlsen
Can't you wrap that function in a class, keeping the *Random* object as a field?
Matteo Italia
I don't want to slow it down, but more so I don't want to have identical random strings. What would be the best way to avoid both? I'm totally stumped.
rockinthesixstring
@Matteo, the function IS in a class... it's actually in a ClassLibrary whereby I initiate a new instance of the object every time I call it (edited above). It made no difference. Sleeping the calls did the trick, but as @Lasse pointed out, it does slow things down.
rockinthesixstring
one option you gave was to `Seed it with something that changes for each call`. Is this possible by seeding it with a GUID? If so, how?
rockinthesixstring
No, you can only seed it with an Int32 number.
Lasse V. Karlsen
I see your edits above, but unfortunately I don't know how to implement them into my scenario... sorry.
rockinthesixstring
Well, then you should make it global or static or something like that, so to preserve the state of the Random object.
Matteo Italia
Ok, got it. I removed `Dim rnd As New Random()` and created a property called `rnd` in it's stead. The property checks to see if there is an existing instance of `_rnd`, and if there is not, it generates a `New Random()` and saves it to `_rnd` and then returns `_rnd`
rockinthesixstring
But note that the `Random` class isn't thread-safe, so multiple simultaneous requests might corrupt the instance.
Lasse V. Karlsen
Right. This won't be called frequent enough for that to matter, so I'm not going to stress about it. What I'm trying to do is generate an encryption string and salt for content encryption. The string and the salt gets stored in a config file, and the encrypted data gets stored in the database.
rockinthesixstring
A: 

I made a couple of changes and it seems fine to me. I don't like using keywords as variable names. Note that I moved the random statement:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim myCS As New CustomStrings
    Dim l As New List(Of String)

    For x As Integer = 1 To 10
        Dim s As String = myCS.GenerateRandom(5)
        l.Add(s)
    Next

    For x As Integer = 0 To l.Count - 1
        Debug.WriteLine(l(x))
    Next
    'Debug output
    'YGXiV
    'rfLmP
    'OVUW9
    '$uaMt
    '^RsPz
    'k&91k
    '(n2uN
    'ldbQQ
    'zYlP!
    '30kNt
End Sub

Public Class CustomStrings

    Private myRnd As New Random()
    Public Function GenerateRandom(ByVal n As Integer, _
                                   Optional ByVal UseSpecial As Boolean = True, _
                                   Optional ByVal SpecialOnly As Boolean = False) As String

        Dim ichars As Integer = 74 'number of characters to use out of the chars string'
        Dim schars As Integer = 0 ' number of characters to skip out of the characters string'

        Dim chars() As Char = New Char() {"A"c, "B"c, "C"c, "D"c, "E"c, "F"c, _
                                          "G"c, "H"c, "I"c, "J"c, "K"c, "L"c, _
                                          "M"c, "N"c, "O"c, "P"c, "Q"c, "R"c, _
                                          "S"c, "T"c, "U"c, "V"c, "W"c, "X"c, _
                                          "Y"c, "Z"c, "0"c, "1"c, "2"c, "3"c, _
                                          "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, _
                                          "a"c, "b"c, "c"c, "d"c, "e"c, "f"c, _
                                          "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, _
                                          "m"c, "n"c, "o"c, "p"c, "q"c, "r"c, _
                                          "s"c, "t"c, "u"c, "v"c, "w"c, "x"c, _
                                          "y"c, "z"c, "!"c, "@"c, "#"c, "$"c, _
                                          "%"c, "^"c, "&"c, "*"c, "("c, ")"c, _
                                          "-"c, "+"c}


        If Not UseSpecial Then ichars = 62 ' only use the alpha numeric characters out of "char"'
        If SpecialOnly Then schars = 62 : ichars = 74 ' skip the alpha numeric characters out of "char"'

        Dim rndStr As String = String.Empty
        Dim i As Integer = 0
        While i < n
            rndStr += chars(Me.myRnd.Next(schars, ichars))
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        Return rndStr
    End Function
End Class

End Class
dbasnett
+1  A: 

Unique Seed Number Approach

To prevent against using the same seed value, in order to stop generating the same random sequence, you can create a random seed by boiling down a GUID (implicitly randomized) to an int value using a function like so:

Private Function GetNewSeed() As Integer
    Dim arrBytes As Byte() = Guid.NewGuid().ToByteArray()  '16 bytes
    Dim seedNum As Integer = 0
    ' Boil GUID down 4 bytes at a time (size of int) and merge into Integer value
    For i As Integer = 0 To arrBytes.Length - 1 Step 4
        seedNum = seedNum Xor BitConverter.ToInt32(arrBytes, i)
    Next
    Return seedNum
End Function

Use the returned int to seed your random number generator.

The problem is now solved by using the custom function GetNewSeed.

Dim rnd1 As New Random( GetNewSeed )

This takes care of the root of the problem which is the seed value.

John K
except that the way that I am using `GenerateRandom` is to actually generate a string of a specific length. `GenerateRandom(5)` creates a string that is 5 characters long.
rockinthesixstring
Caching the Random instance still seems to be the best way to do this
rockinthesixstring
@rockinthesixstring: I corrected the second code snippet. Agreed caching is good. Additionally piling on a seed value solution helps ensure random number generators created inside different scopes or assemblies (or not visible to each other) don't experience the same problem of same sequences by relying on clock - i.e. multithreaded app, not that your specific case needs to consider this right now.
John K
Yes that seems to work great and I 'think' we're thread safe with it too. Good show!
rockinthesixstring