0

i tried to create random string contains both int and string. Below is the classes of how i get random string and int.

private int RandomNumber1(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }
    private int RandomNumber2(int min, int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }
    private string RandomStringSatu(int size, bool uppercase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();

        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (uppercase)
        return builder.ToString().ToUpper();
        return builder.ToString();
    }

    private string RandomStringDua(int size, bool uppercase)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();

        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        if (uppercase)
            return builder.ToString().ToUpper();
        return builder.ToString();
    }

And here is the way i set up those classes to get a random string.

StringBuilder sb = new StringBuilder();
                sb.Append(RandomStringSatu(1, true));
                sb.Append(RandomNumber1(1, 9));
                sb.Append(RandomStringDua(1, true));
                sb.Append(RandomNumber2(1, 9));

                string rdmKode = sb.ToString();

this is the result that i get : Result

On the picture you can see that the first two caracters has same value with the last two caracter.

Now, the question is what should i do, if i want to get different caracter. So, the output should be looks like "D2B1"

Thanks

Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
Fikri Hailal
  • 123
  • 2
  • 4
  • 16

3 Answers3

1

If you move your Random random = new Random(); line from RandomNumber1 and RandomNumber2 to global to outside of methods, than use that rundom field in that RandomNumber1 and RandomNumber2 methods result will be differente.

Thanx Jamaxack

Jamaxack
  • 2,400
  • 2
  • 24
  • 42
0

Using an answer from this stackoverflow question

You are looking to seed when you create a new instance.

Random random = new Random(Guid.NewGuid().GetHashCode());
Community
  • 1
  • 1
  • Thanks... This is work.. – Fikri Hailal Nov 05 '15 at 04:24
  • 2
    @FikriHailal it works, but it is not a good solution. – phoog Nov 05 '15 at 04:40
  • @phoog: can you tell me another solution ? – Fikri Hailal Nov 05 '15 at 10:55
  • @FikriHailal yes. Use a single instance of the random class instead of creating a new one each time you need a random number. The class is not thread safe, so if you have multiple threads you'll need to take that into consideration. It appears that you do not have multiple threads, however. The most likely solution would be to make the random instance a private field of the class that defines `RandomNumber1` etc., but you could also pass it as an argument to the method. The answers to the linked questions mostly use the private field approach. Jon Skeet's is thread safe, if you need that. – phoog Nov 05 '15 at 14:03
0

Provide different seed values when you create instances of Random() to generate different values. Check documentation here, https://msdn.microsoft.com/en-us/library/ctssatww%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Nemo
  • 3,285
  • 1
  • 28
  • 22