Sinisterly
[RELEASE] Random String Generator - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework)
+--- Thread: [RELEASE] Random String Generator (/Thread-RELEASE-Random-String-Generator)



[RELEASE] Random String Generator - hybris.softwares - 09-16-2016

Hello folks,
Today i'm going to present you a nice class that you can use in any project you want.

Its purpose is to generate random strings with a normal entropy range. To do this it reads method names, class names and namespaces from all loaded libraries.

Code:
 public class RandomStringGenerator    {        private Random Random;        private List<string> names;        public RandomStringGenerator(Random random)        {            Random = random;            LoadStrings();        }        public RandomStringGenerator() : this(new Random())        {                    }        public string GetRandomString(int len)        {            StringBuilder sb = new StringBuilder();            while(sb.Length < len)            {                sb.Append(names[Random.Next(0, names.Count)] + names[Random.Next(0, names.Count)]);            }            return sb.ToString();        }        private void LoadStrings()        {            names = new List<string>();            foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())            {                foreach(Type t in assembly.GetExportedTypes())                {                    names.Add(t.Namespace);                    names.Add(t.Name);                    foreach(MethodInfo m in t.GetMethods())                    {                        names.Add(m.Name);                    }                }            }        }    }

A simple usage :

Code:
RandomStringGenerator rsg = new RandomStringGenerator(); string randomString = rsg.GetRandomString(10); Console.WriteLine(randomString);

Here there are some example outputs from the generator :

Code:
add_LostFocusget_RecreatingHandle add_Clickget_EventLog get_DisplayMemberToolStripItemEventArgs get_DomainManagerget_ReadMethods get_TypeIdset_Name Equalsget_SourceColumn set_GripMarginGetHashCode set_RightToLeftIXmlSchemaInfo set_AnyAttributeget_UnhandledAttributes GetTypeadd_DragEnter

Enjoy!Cool


RE: [RELEASE] Random String Generator - Killpot - 09-16-2016

You should use Type.GetMembers() instead of GetMethods(), as it will also include variables.


RE: [RELEASE] Random String Generator - hybris.softwares - 09-16-2016

(09-16-2016, 08:43 PM)Killpot Wrote: You should use Type.GetMembers() instead of GetMethods(), as it will also include variables.

Yeah, you are right, i've wrote it in little time, ahahah.

Will update thread Cool


RE: [RELEASE] Random String Generator - Bish0pQ - 09-16-2016

Looks nice man. Thanks for sharing.

Here is my code in VB.NET for generating random strings (not exactly the same but still handy for people who are interested in this thing).

Spoiler:
Code:
Module StringGenerator Sub Main() '## Variables and arrays Dim intStringsToGenerate As Double Dim rndRandom As New Random Dim arrLetters() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} '## Ask for input Console.WriteLine("How many strings do you want to generate?") Console.Write("Number of strings:") '## Parse the console input to integer intStringsToGenerate = Integer.Parse(Console.ReadLine()) '## Write the strings to console For i = 0 To intStringsToGenerate - 1 Dim strCurrent As String = String.Empty Dim intLength As Integer = Integer.Parse(rndRandom.Next(3, 15)) '## Add the random letters to the string For z = 0 To intLength strCurrent = strCurrent & arrLetters(rndRandom.Next(0, 26)).ToString Next '## Write the generated string to the console Console.WriteLine(strCurrent) Next End Sub End Module