Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 756435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:14:40+00:00 2026-05-14T15:14:40+00:00

I’ve build a random string generator but I’m having a problem whereby if I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:14:40+00:00Added an answer on May 14, 2026 at 3:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
We're building an app, our first using Rails 3, and we're having to build
I want to construct a data frame in an Rcpp function, but when I
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.