Trying to create a string of random values

I am trying to create a string of random length consisting of random chars.

For this, I have this code:

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 1000; i++)
        {
            MyString test = new MyString();

            test.Random();
            Console.WriteLine(test.Value);
        }
        Console.ReadLine();
    }
}

public class MyString
{
    private string value = string.Empty;
    private Random r = new Random();

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    public void Random()
    {
        int length = (r.Next() % (100)) + 1;
        for(int i = 0; i < length; i++)
        {
            value = value + RandomChar();
        }  
    }

    public char RandomChar()
    {
        // 32 to 126
        int c = (r.Next() % 95) + 32;
        char ch = (char)c;
        return ch;
    }
}

Now let's look at the output part:

alt text

As you can see, the output is far from random; it contains many repeating lines. How is this possible and how to solve it?

+3
source share
2 answers

, Random , MyString. Random, , ( ). , , .

, Random .

+11

http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx

string randomName = Path.GetRandomFileName();
randomName = randomName.Replace(".", string.Empty);

// take substring...
0

Source: https://habr.com/ru/post/1761421/


All Articles