Skip to content

Instantly share code, notes, and snippets.

@subena22jf
Last active May 8, 2021 01:32
Show Gist options
  • Save subena22jf/242618b08ba85dd3d335 to your computer and use it in GitHub Desktop.
Save subena22jf/242618b08ba85dd3d335 to your computer and use it in GitHub Desktop.
c# random string
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.Maths
{
public class Random
{
public int index;
public void init()
{
System.Random rd = new System.Random();
}
public string RandomString(int size, bool lowerCase)
{
System.Random rd = new System.Random();
int x = rd.Next(0, 26);
if(x==1)
{
return RandomString1(size, lowerCase);
}else
if (x == 2)
{
return RandomString2(size, lowerCase);
}else
if (x == 3)
{
return RandomString3(size, lowerCase);
}else
if (x == 4)
{
return RandomString4(size, lowerCase);
}else
if (x == 5)
{
return RandomString5(size, lowerCase);
}
else
if (x == 6)
{
return RandomString6(size, lowerCase);
}
else
if (x == 7)
{
return RandomString7(size, lowerCase);
}
else
if (x == 8)
{
return RandomString8(size, lowerCase);
}
else
if (x == 9)
{
return RandomString9(size, lowerCase);
}
else
if (x == 10)
{
return RandomString10(size, lowerCase);
}
else
if (x == 11)
{
return RandomString11(size, lowerCase);
}
else
if (x == 12)
{
return RandomString12(size, lowerCase);
}
else
if (x == 13)
{
return RandomString13(size, lowerCase);
}
else
if (x == 14)
{
return RandomString14(size, lowerCase);
}
else
if (x == 15)
{
return RandomString15(size, lowerCase);
}
else
if (x == 16)
{
return RandomString16(size, lowerCase);
}
else
if (x == 17)
{
return RandomString17(size, lowerCase);
}
else
if (x == 18)
{
return RandomString18(size, lowerCase);
}
else
if (x == 19)
{
return RandomString19(size, lowerCase);
}
else
if (x == 20)
{
return RandomString20(size, lowerCase);
}
else
if (x == 21)
{
return RandomString21(size, lowerCase);
}
else
if (x == 22)
{
return RandomString22(size, lowerCase);
}
else
if (x == 23)
{
return RandomString23(size, lowerCase);
}
else
if (x == 24)
{
return RandomString24(size, lowerCase);
}
else
if (x == 25)
{
return RandomString25(size, lowerCase);
}
else
{
return RandomString26(size, lowerCase);
}
}
protected string RandomString1(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(1 * random.NextDouble() + 255)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString2(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(2 * random.NextDouble() + 235)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString3(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(3 * random.NextDouble() + 225)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString4(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(4 * random.NextDouble() + 205)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString5(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(5 * random.NextDouble() + 105)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString6(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(6 * random.NextDouble() + 87)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString7(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(7 * random.NextDouble() + 94)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString8(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(8 * random.NextDouble() + 63)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString9(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(9 * random.NextDouble() + 15)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString10(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(10 * random.NextDouble() + 5)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString11(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(11 * random.NextDouble() + 95)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString12(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(12 * random.NextDouble() + 85)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString13(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(13 * random.NextDouble() + 295)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString14(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(14 * random.NextDouble() + 307)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString15(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(15 * random.NextDouble() + 18)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString16(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(16 * random.NextDouble() + 20)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString17(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(17 * random.NextDouble() + 25)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString18(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(18 * random.NextDouble() + 40)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString19(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(19 * random.NextDouble() + 47)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString20(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(20 * random.NextDouble() + 64)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString21(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(21 * random.NextDouble() + 129)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString22(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(22 * random.NextDouble() + 171)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString23(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(23 * random.NextDouble() + 251)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString24(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(24 * random.NextDouble() + 357)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString25(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(25 * random.NextDouble() + 98)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string RandomString26(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
System.Random random = new System.Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 461)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
protected string GetRandomAlphanumericString(int length)
{
const string alphanumericCharacters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
return GetRandomString(length, alphanumericCharacters);
}
protected string GetRandomString(int length, IEnumerable<char> characterSet)
{
if (length < 0)
throw new ArgumentException("length must not be negative", "length");
if (length > int.MaxValue / 8) // 250 million chars ought to be enough for anybody
throw new ArgumentException("length is too big", "length");
if (characterSet == null)
throw new ArgumentNullException("characterSet");
var characterArray = characterSet.Distinct().ToArray();
if (characterArray.Length == 0)
throw new ArgumentException("characterSet must not be empty", "characterSet");
var bytes = new byte[length * 8];
//new RNGCryptoServiceProvider().GetBytes(bytes);
var result = new char[length];
for (int i = 0; i < length; i++)
{
ulong value = BitConverter.ToUInt64(bytes, i * 8);
result[i] = characterArray[value % (uint)characterArray.Length];
}
return new string(result);
}
protected string GetString(string format)
{
System.Random random = new System.Random();
StringBuilder result = new StringBuilder();
for (int formatIndex = 0; formatIndex < format.Length; formatIndex++)
{
switch (format.ToUpper()[formatIndex])
{
case '0': result.Append(getRandomNumeric(random)); break;
case '#': result.Append(getRandomCharacter(random)); break;
default: result.Append(format[formatIndex]); break;
}
}
return result.ToString();
}
protected char getRandomCharacter(System.Random random)
{
string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return chars[random.Next(chars.Length)];
}
protected char getRandomNumeric(System.Random random)
{
string nums = "0123456789";
return nums[random.Next(nums.Length)];
}
protected List<char> getAvailableRandomCharacters(bool includeLowerCase)
{
var integers = Enumerable.Empty<int>();
integers = integers.Concat(Enumerable.Range('A', 26));
integers = integers.Concat(Enumerable.Range('0', 10));
if (includeLowerCase)
integers = integers.Concat(Enumerable.Range('a', 26));
return integers.Select(i => (char)i).ToList();
}
protected IEnumerable<char> GetRandomCharacters(int count, bool includeLowerCase)
{
var characters = getAvailableRandomCharacters(includeLowerCase);
var random = new System.Random();
var result = Enumerable.Range(0, count)
.Select(_ => characters[random.Next(characters.Count)]);
return result;
}
protected string GetRandomSymbols()
{
int randomSymbolsDefaultCount = 8;
return GetRandomSymbols(randomSymbolsDefaultCount);
}
protected string GetRandomSymbols(int count)
{
var index = randomSymbolsIndex;
var result = new string(
Enumerable.Repeat(availableChars, count)
.Select(s => {
index += random.Next(s.Length);
if (index >= s.Length)
index -= s.Length;
return s[index];
})
.ToArray());
randomSymbolsIndex = index;
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment