how to generate random string in c#

using System;
using System.IO;
using System.Linq;

public class Program
{
public static void Main()
{
Random rnm=new Random();
var a=Guid.NewGuid().ToString().Replace("-",string.Empty).ToUpper().Substring(0,32);
var b=rnm.GetHashCode();
var c=rnm.Next(0,1000000000).ToString("D9");
var d=Path.GetRandomFileName().ToUpper();
d = d.Replace(".","");

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var charsize = new char[7];
var random = new Random();
for (int i = 0; i < charsize.Length; i++)
{
charsize[i] = chars[random.Next(chars.Length)];
}
var stringval = new String(charsize);
Console.WriteLine(a);
Console.WriteLine(RandomString(50));
}

private static Random random = new Random();
public static string RandomString(int StringLength)
{
  string LenChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  return new string(Enumerable.Repeat(LenChars, StringLength)
   .Select(s => s[random.Next(s.Length)]).ToArray());
}
}

Post a Comment

0 Comments