-1

How can I generate Random strings which are saved into a array? In the language c#

So I would like to generate Random Names (strings) which are saved into an array

I only know, that I can generate Random Numbers with the Random Method ( Iam learning programming now and I dont have much experience)

Thanks for your help.

2
  • Please, start from the definitions, e.g. what name is. Are Tim, tiM, 123Tim, Tim_123, 123, 123+456 valid names? If not why? Commented Apr 18, 2020 at 7:09
  • The duplicate (stackoverflow.com/questions/1344221/…) shows how to generate random strings, hopefully you should already know how to set an element of an array to value you created based on info from duplicate. If that is not what you are asking - edit question to clarify and show what you tried Commented Apr 18, 2020 at 7:47

1 Answer 1

-1
  int length = 7;

      // creating a StringBuilder object()
      StringBuilder str_build = new StringBuilder();  
      Random random = new Random();  

      char letter;  

      for (int i = 0; i < length; i++)
      {
        double flt = random.NextDouble();
        int shift = Convert.ToInt32(Math.Floor(25 * flt));
        letter = Convert.ToChar(shift + 65);
        str_build.Append(letter);  
      }  

With this code you can achieve that. Basically you create random number and convert it into character

Sign up to request clarification or add additional context in comments.

2 Comments

letter = (char) (random.Next('a', 'z' - 'a' + 1)); no need in double, Floor and Convert
string randomName = string.Concat(Enumerable.Range(0, length).Select(x => (char) (random.Next('a', 'z' - 'a' + 1))));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.