├── .gitattributes ├── RandomString4NetTester ├── packages.config ├── app.config ├── Properties │ └── AssemblyInfo.cs ├── RandomString4NetTester.csproj └── Program.cs ├── RandomString4NetTest ├── packages.config ├── Properties │ └── AssemblyInfo.cs ├── RandomString4NetTest.csproj └── RandomString4NetTests.cs ├── RandomString4NetTesterDotNet ├── RandomString4NetTesterDotNet.csproj └── Program.cs ├── LICENSE ├── .github └── workflows │ └── dotnet-CI-workflow.yml ├── RandomString4Net ├── DataSource.cs ├── Exceptions │ ├── InvalidLengthException.cs │ └── UnsupportedSymbolException.cs ├── Types.cs ├── RandomString4Net - Backup.csproj ├── RandomString4Net.csproj └── RandomString.cs ├── RandomString4Net.sln ├── README.md └── .gitignore /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /RandomString4NetTester/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /RandomString4NetTester/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /RandomString4NetTest/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /RandomString4NetTesterDotNet/RandomString4NetTesterDotNet.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /RandomString4NetTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | [assembly: AssemblyTitle("RandomString4NetTest")] 5 | [assembly: AssemblyDescription("Unit tests for the RandomString4Net library")] 6 | [assembly: AssemblyConfiguration("")] 7 | [assembly: AssemblyCompany("Lakhya's Innovation Inc")] 8 | [assembly: AssemblyProduct("RandomString4NetTest")] 9 | [assembly: AssemblyCopyright("Copyright © 2020-2023 Lakhya's Innovation Inc.")] 10 | [assembly: AssemblyTrademark("")] 11 | [assembly: AssemblyCulture("")] 12 | 13 | [assembly: ComVisible(false)] 14 | 15 | [assembly: Guid("12c4f675-e7ce-4b15-889a-ec269aa8e4b3")] 16 | 17 | // [assembly: AssemblyVersion("1.0.*")] 18 | [assembly: AssemblyVersion("1.0.0.0")] 19 | [assembly: AssemblyFileVersion("1.0.0.0")] 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-2025 Lakhya Jyoti Nath 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/dotnet-CI-workflow.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a .NET project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net 3 | 4 | name: build-and-test-workflow 5 | 6 | on: 7 | push: 8 | branches: [ "master" ] 9 | pull_request: 10 | branches: [ "master" ] 11 | 12 | jobs: 13 | build: 14 | runs-on: windows-latest 15 | steps: 16 | - uses: actions/checkout@v4.1.1 17 | 18 | - name: Setup MSBuild 19 | uses: microsoft/setup-msbuild@v1.3.1 20 | 21 | - name: Setup VSTest 22 | uses: darenm/Setup-VSTest@v1.2 23 | 24 | - name: Setup NuGet 25 | uses: NuGet/setup-nuget@v1.2.0 26 | 27 | - name: Restore NuGet packages 28 | run: nuget restore RandomString4Net.sln 29 | 30 | - name: Build RandomString4Net for Release 31 | run: msbuild RandomString4Net.sln -property:Configuration=Release -property:platform="Any CPU" 32 | 33 | - name: Run Tests 34 | run: vstest.console.exe .\RandomString4NetTest\bin\Release\RandomString4NetTest.dll 35 | 36 | - name: Upload coverage reports to Codecov 37 | uses: codecov/codecov-action@v3 38 | env: 39 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} 40 | -------------------------------------------------------------------------------- /RandomString4NetTester/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("RandomString4NetTester")] 9 | [assembly: AssemblyDescription("Console application for testing RandomString4Net")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("Lakhya's Innovation Inc.")] 12 | [assembly: AssemblyProduct("RandomString4NetTester")] 13 | [assembly: AssemblyCopyright("Copyright © 2020-2023 Lakhya's Innovation Inc.")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("0af6f74c-e5b1-4e6a-8609-42208938abd3")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /RandomString4Net/DataSource.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * RandomString4Net - .NET library to generate N random strings of M length from various categories 4 | * Copyright (c) 2020-2025 Lakhya Jyoti Nath 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Website: https://ljnath.com 25 | * Email: ljnath@ljnath.com / lakhya.sci@gmail.com 26 | * 27 | **/ 28 | namespace RandomString4Net 29 | { 30 | /// 31 | /// Class containing the valid inputs for random string generation 32 | /// 33 | internal static class DataSource 34 | { 35 | internal static string Alphabets { get { return "abcdefghijklmnopqrstuvwxyz"; } } 36 | internal static string Numbers { get { return "0123456789"; } } 37 | internal static string Symbols { get { return @"!#$%&'()*+,-./:;<=>?@[\]\\^_`{|}~"""; } } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /RandomString4Net/Exceptions/InvalidLengthException.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * RandomString4Net - .NET library to generate N random strings of M length from various categories 4 | * Copyright (c) 2020-2023 Lakhya Jyoti Nath 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Website: https://ljnath.com 25 | * Email: ljnath@ljnath.com / lakhya.sci@gmail.com 26 | * 27 | **/ 28 | using System; 29 | using System.Runtime.Serialization; 30 | 31 | namespace RandomString4Net.Exceptions 32 | { 33 | [Serializable] 34 | public class InvalidLengthException : Exception 35 | { 36 | public InvalidLengthException() : base() { } 37 | 38 | public InvalidLengthException(string message) : base(message) { } 39 | 40 | public InvalidLengthException(string format, params object[] args) : base(string.Format(format, args)) { } 41 | 42 | public InvalidLengthException(string message, Exception innerException) : base(message, innerException) { } 43 | 44 | public InvalidLengthException(string format, Exception innerException, params object[] args) : base(string.Format(format, args), innerException) { } 45 | 46 | protected InvalidLengthException(SerializationInfo info, StreamingContext context) : base(info, context) { } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /RandomString4Net/Exceptions/UnsupportedSymbolException.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * RandomString4Net - .NET library to generate N random strings of M length from various categories 4 | * Copyright (c) 2020-2023 Lakhya Jyoti Nath 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Website: https://ljnath.com 25 | * Email: ljnath@ljnath.com / lakhya.sci@gmail.com 26 | * 27 | **/ 28 | using System; 29 | using System.Runtime.Serialization; 30 | 31 | namespace RandomString4Net.Exceptions 32 | { 33 | [Serializable] 34 | public class UnsupportedSymbolException : Exception 35 | { 36 | public UnsupportedSymbolException() : base() { } 37 | 38 | public UnsupportedSymbolException(string message) : base(message) { } 39 | 40 | public UnsupportedSymbolException(string format, params object[] args) : base(string.Format(format, args)) { } 41 | 42 | public UnsupportedSymbolException(string message, Exception innerException) : base(message, innerException) { } 43 | 44 | public UnsupportedSymbolException(string format, Exception innerException, params object[] args) : base(string.Format(format, args), innerException) { } 45 | 46 | protected UnsupportedSymbolException(SerializationInfo info, StreamingContext context) : base(info, context) { } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /RandomString4NetTester/RandomString4NetTester.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {0AF6F74C-E5B1-4E6A-8609-42208938ABD3} 8 | Exe 9 | RandomString4NetTester 10 | RandomString4NetTester 11 | v4.0 12 | 512 13 | true 14 | Client 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | {f0a77092-0c76-4151-843c-cd1bd33174b0} 47 | RandomString4Net 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /RandomString4NetTesterDotNet/Program.cs: -------------------------------------------------------------------------------- 1 | // See https://aka.ms/new-console-template for more information 2 | using System.Diagnostics; 3 | using RandomString4Net; 4 | 5 | 6 | // instance of stopwatch to measure execution time 7 | Stopwatch stopwatch = new(); 8 | 9 | stopwatch.Start(); 10 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE); 11 | stopwatch.Stop(); 12 | Console.WriteLine($"Genrated 1 random string, time taken = {stopwatch.ElapsedMilliseconds} ms"); 13 | Console.WriteLine(randomString); 14 | 15 | 16 | for (int i = 1; i <= 3; i++) 17 | { 18 | Console.WriteLine("\n"); 19 | stopwatch.Start(); 20 | List randomStrings = RandomString.GetStrings(Types.NUMBERS, i * 10); 21 | stopwatch.Stop(); 22 | Console.WriteLine($"Generated a list of {stopwatch.ElapsedMilliseconds} random strings of Type NUMBERS, time taken = {i * 10} ms"); 23 | randomStrings.ForEach(str => Console.Write(str + ", ")); 24 | } 25 | 26 | for (int i = 1; i <= 3; i++) 27 | { 28 | Console.WriteLine("\n"); 29 | stopwatch.Start(); 30 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE, count: i * 10, symbolsToInclude: "+-*/", maxLength: 20, forceUnique: true); 31 | stopwatch.Stop(); 32 | Console.WriteLine($"Generated a list of {stopwatch.ElapsedMilliseconds} random strings of type ALPHANUMERIC_MIXEDCASE with custom symbols, time taken = {i * 10} ms"); 33 | randomStrings.ForEach(str => Console.Write(str + ", ")); 34 | 35 | } 36 | 37 | for (int i = 1; i <= 3; i++) 38 | { 39 | Console.WriteLine("\n"); 40 | stopwatch.Start(); 41 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, count: i * 10, symbolsToInclude: "+-*/", maxLength: 20, forceUnique: true, forceOccuranceOfEachType: true); 42 | stopwatch.Stop(); 43 | Console.WriteLine($"Generated a list of {stopwatch.ElapsedMilliseconds} random strings of type ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS with custom symbols and forced occurance of each type, time taken = {i * 10} ms"); 44 | randomStrings.ForEach(str => Console.Write(str + ", ")); 45 | } 46 | 47 | Console.WriteLine("\n\nGenerating 100000 random string check for duplicates for 100 times"); 48 | stopwatch.Start(); 49 | for (int i = 0; i < 100; i++) 50 | { 51 | List randomNumbers = RandomString.GetStrings(Types.NUMBERS, 100000, 10, false, true); 52 | 53 | var anyDuplicate = randomNumbers.GroupBy(x => x).Any(g => g.Count() > 1); 54 | var allUnique = randomNumbers.GroupBy(x => x).All(g => g.Count() == 1); 55 | 56 | Console.WriteLine($"{i + 1}. duplicate = {anyDuplicate} ; unique = {allUnique}"); 57 | } 58 | stopwatch.Stop(); 59 | Console.WriteLine($"Time taken = {stopwatch.ElapsedMilliseconds} ms"); -------------------------------------------------------------------------------- /RandomString4Net.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.7.34221.43 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomString4NetTester", "RandomString4NetTester\RandomString4NetTester.csproj", "{0AF6F74C-E5B1-4E6A-8609-42208938ABD3}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomString4NetTest", "RandomString4NetTest\RandomString4NetTest.csproj", "{12C4F675-E7CE-4B15-889A-EC269AA8E4B3}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RandomString4Net", "RandomString4Net\RandomString4Net.csproj", "{F0A77092-0C76-4151-843C-CD1BD33174B0}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FA42BC52-B3AE-465A-BCCF-EDA5F1FF8804}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomString4NetTesterDotNet", "RandomString4NetTesterDotNet\RandomString4NetTesterDotNet.csproj", "{3F3FA738-BED0-4183-B9B0-32B872B65273}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {0AF6F74C-E5B1-4E6A-8609-42208938ABD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {0AF6F74C-E5B1-4E6A-8609-42208938ABD3}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {0AF6F74C-E5B1-4E6A-8609-42208938ABD3}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {0AF6F74C-E5B1-4E6A-8609-42208938ABD3}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {12C4F675-E7CE-4B15-889A-EC269AA8E4B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {12C4F675-E7CE-4B15-889A-EC269AA8E4B3}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {12C4F675-E7CE-4B15-889A-EC269AA8E4B3}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {12C4F675-E7CE-4B15-889A-EC269AA8E4B3}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {F0A77092-0C76-4151-843C-CD1BD33174B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {F0A77092-0C76-4151-843C-CD1BD33174B0}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {F0A77092-0C76-4151-843C-CD1BD33174B0}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {F0A77092-0C76-4151-843C-CD1BD33174B0}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {3F3FA738-BED0-4183-B9B0-32B872B65273}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {3F3FA738-BED0-4183-B9B0-32B872B65273}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {3F3FA738-BED0-4183-B9B0-32B872B65273}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {3F3FA738-BED0-4183-B9B0-32B872B65273}.Release|Any CPU.Build.0 = Release|Any CPU 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | GlobalSection(ExtensibilityGlobals) = postSolution 43 | SolutionGuid = {AE39D563-4226-411D-B7C4-9B317BE012C1} 44 | EndGlobalSection 45 | EndGlobal 46 | -------------------------------------------------------------------------------- /RandomString4NetTester/Program.cs: -------------------------------------------------------------------------------- 1 | using RandomString4Net; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Diagnostics; 5 | using System.Linq; 6 | 7 | namespace RandomString4NetTester 8 | { 9 | static class Program 10 | { 11 | static void Main() 12 | { 13 | Stopwatch stopwatch = new Stopwatch(); 14 | stopwatch.Start(); 15 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE); 16 | stopwatch.Stop(); 17 | Console.WriteLine($"Genrated 1 random string, time taken = {stopwatch.ElapsedMilliseconds} ms"); 18 | Console.WriteLine(randomString); 19 | 20 | for (int i = 1; i <= 3; i++) 21 | { 22 | Console.WriteLine("\n"); 23 | stopwatch.Start(); 24 | List randomStrings = RandomString.GetStrings(Types.NUMBERS, i * 10); 25 | stopwatch.Stop(); 26 | Console.WriteLine($"Generated a list of {stopwatch.ElapsedMilliseconds} random strings of Type NUMBERS, time taken = {i * 10} ms"); 27 | randomStrings.ForEach(str => Console.Write(str + ", ")); 28 | } 29 | 30 | for (int i = 1; i <= 3; i++) 31 | { 32 | Console.WriteLine("\n"); 33 | stopwatch.Start(); 34 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE, count: i * 10, symbolsToInclude: "+-*/", maxLength: 20, forceUnique: true); 35 | stopwatch.Stop(); 36 | Console.WriteLine($"Generated a list of {stopwatch.ElapsedMilliseconds} random strings of type ALPHANUMERIC_MIXEDCASE with custom symbols, time taken = {i * 10} ms"); 37 | randomStrings.ForEach(str => Console.Write(str + ", ")); 38 | } 39 | 40 | for (int i = 1; i <= 3; i++) 41 | { 42 | Console.WriteLine("\n"); 43 | stopwatch.Start(); 44 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, count: i * 10, symbolsToInclude: "+-*/", maxLength: 20, forceUnique: true, forceOccuranceOfEachType: true); 45 | stopwatch.Stop(); 46 | Console.WriteLine($"Generated a list of {stopwatch.ElapsedMilliseconds} random strings of type ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS with custom symbols and forced occurance of each type, time taken = {i * 10} ms"); 47 | randomStrings.ForEach(str => Console.Write(str + ", ")); 48 | } 49 | 50 | Console.WriteLine("\n\nGenerating 100000 random string check for duplicates for 100 times"); 51 | stopwatch.Start(); 52 | for (int i = 0; i < 100; i++) 53 | { 54 | List randomNumbers = RandomString.GetStrings(Types.NUMBERS, 100000, 10, false, true); 55 | 56 | var anyDuplicate = randomNumbers.GroupBy(x => x).Any(g => g.Count() > 1); 57 | var allUnique = randomNumbers.GroupBy(x => x).All(g => g.Count() == 1); 58 | 59 | Console.WriteLine($"{i + 1}. duplicate = {anyDuplicate} ; unique = {allUnique}"); 60 | } 61 | stopwatch.Stop(); 62 | Console.WriteLine($"Time taken = {stopwatch.ElapsedMilliseconds} ms"); 63 | 64 | Console.WriteLine("\n\nPress any key to exit..."); 65 | Console.ReadKey(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /RandomString4NetTest/RandomString4NetTest.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Debug 7 | AnyCPU 8 | {12C4F675-E7CE-4B15-889A-EC269AA8E4B3} 9 | Library 10 | Properties 11 | RandomString4NetTest 12 | RandomString4NetTest 13 | v4.8 14 | 512 15 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 16 | 15.0 17 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 18 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 19 | False 20 | UnitTest 21 | 22 | 23 | 24 | 25 | 26 | true 27 | full 28 | false 29 | bin\Debug\ 30 | DEBUG;TRACE 31 | prompt 32 | 4 33 | 34 | 35 | pdbonly 36 | true 37 | bin\Release\ 38 | TRACE 39 | prompt 40 | 4 41 | 42 | 43 | true 44 | 45 | 46 | 47 | ..\packages\MSTest.TestFramework.3.1.1\lib\net462\Microsoft.VisualStudio.TestPlatform.TestFramework.dll 48 | 49 | 50 | ..\packages\MSTest.TestFramework.3.1.1\lib\net462\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | {f0a77092-0c76-4151-843c-cd1bd33174b0} 62 | RandomString4Net 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RandomString4Net 2 | ### Version 1.9.0 3 | 4 | Author : Lakhya Jyoti Nath (ljnath)
5 | Date : September 2020 - January 2025
6 | Email : ljnath@ljnath.com
7 | Website : https://www.ljnath.com 8 | 9 | [![Nuget](https://img.shields.io/nuget/v/RandomString4Net)](https://www.nuget.org/packages/RandomString4Net/) 10 | [![Nuget](https://img.shields.io/nuget/dt/RandomString4Net)](https://www.nuget.org/stats/packages/RandomString4Net) 11 | ![GitHub](https://img.shields.io/github/license/ljnath/RandomString4Net) 12 | ![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/ljnath/RandOMString4Net) 13 | ![Codecov](https://img.shields.io/codecov/c/github/ljnath/RandomString4Net) 14 | 15 | 16 | ## Introduction 17 | RandomString4Net is a library developed in .NET Framework to generate N random strings of M length from various categories. It is fast and suports string generation of various length.

18 | It is parameterized to generate both a single or a list of random strings.
19 | Random strings can be of types alphabet and alphanumeric supporting all the cases viz. lower, upper and mixed.
20 | It also supports symbol during the random string generation process. Following are the list of supported symbols 21 |
22 | !#$%&'()*+,-./:;<=>?@[]\^_`{|}~" 23 | 24 | It also allows you to generate random string with only a subset of symbols from the above supported list. It is an ideal use for project like 25 | * password generator 26 | * mobile number generator 27 | * unique-id generator 28 | * unique filename generator 29 | * raw data generator for data processing/parsing projects 30 | * fake name generator 31 | etc. etc. 32 | 33 | 34 | 35 | ## Features 36 | * Supports single or multiple random string generation 37 | * Supports random string generation from alphabet, numbers, symbols or a combination of them 38 | * Supports customized length of randon string 39 | * Supports random length of generated strings with a fixed max length 40 | * Supports true unique random number generation (v1.3.0) 41 | * Built with compilation conditions to take advantage of newer .NET Framework functionalities 42 | * Improved performance in newer .NET Framework ( >2.0) 43 | * Added support to force include strings of each type (version 1.4.0) 44 | * Added support for .NET (>5.0) 45 | * Added support for .NET Core (>2.2) 46 | * Added support for .NET Standard (>2.0) 47 | * Added support for RandomNumberGenerator in .NET 6.0 and higher as RNGCryptoServiceProvider is marked as obsolete 48 | * Added support for .NET 8.0 49 | * Added support for .NET 9.0 50 | * Updated random seed to 4 (https://github.com/jshergal/) 51 | 52 | 53 | ## Supported Types 54 | * **NUMBER** : *0123456789* 55 | * **SYMBOLS** : *!#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 56 | * **ALPHABET_LOWERCASE** : *abcdefghijklmnopqrstuvwxyz* 57 | * **ALPHABET_UPPERCASE** : *ABCDEFGHIJKLMNOPQRSTUVWXYZ* 58 | * **ALPHABET_MIXEDCASE** : *abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ* 59 | * **ALPHANUMERIC_LOWERCASE** : *abcdefghijklmnopqrstuvwxyz0123456789* 60 | * **ALPHANUMERIC_UPPERCASE** : *ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789* 61 | * **ALPHANUMERIC_MIXEDCASE** : *abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789* 62 | * **ALPHABET_LOWERCASE_WITH_SYMBOLS** : *abcdefghijklmnopqrstuvwxyz!#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 63 | * **ALPHABET_UPPERCASE_WITH_SYMBOLS** : *ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 64 | * **ALPHABET_MIXEDCASE_WITH_SYMBOLS** : *abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 65 | * **ALPHANUMERIC_LOWERCASE_WITH_SYMBOLS** : *abcdefghijklmnopqrstuvwxyz0123456789!#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 66 | * **ALPHANUMERIC_UPPERCASE_WITH_SYMBOLS** : *ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 67 | * **ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS** : *abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" !#$%&'()\*+,-./:;<=>?@[]\^_`{|}~"* 68 | 69 | 70 | ## Get Started 71 | ### 1. Install Package 72 | ```ini 73 | PM> Install-Package RandomString4Net 74 | ``` 75 | ### 2. Add reference in your project 76 | 77 | ### 3. Call the methods in your program 78 | ```csharp 79 | using RandomString4Net; 80 | 81 | namespace RandomString4NetTester 82 | { 83 | public void Main() 84 | { 85 | // generating one random string from lowercase alphabets 86 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE); 87 | System.Console.WriteLine(randomString); 88 | 89 | // generating 100 random string from all mixedcase alphabet, numbers and all supported symbols 90 | List randomAlphaNumbericStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, 100); 91 | foreach (string s in randomAlphaNumbericStrings) 92 | System.Console.WriteLine(s); 93 | 94 | // generating 200 random string from uppercase alphabet with custom symbols 95 | List randomAlphabetWithCustomSymbols = RandomString.GetStrings(Types.ALPHABET_UPPERCASE, 200, "/+*-"); 96 | foreach (string s in randomAlphabetWithCustomSymbols) 97 | System.Console.WriteLine(s); 98 | 99 | // generating 1000 true random strings of length 20 from uppercase alphabet with custom symbols 100 | List trueUniqueRandomStrings = RandomString.GetStrings(Types.ALPHABET_UPPERCASE, 1000, 20, false, true); 101 | foreach (string s in trueUniqueRandomStrings) 102 | System.Console.WriteLine(s); 103 | 104 | // generating 100 random string of mixedcase alphanummeric with custom symbols 105 | List randomAlphabetWithCustomSymbols = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, 100, "/+*-", forceOccuranceOfEachType: true); 106 | foreach (string s in randomAlphabetWithCustomSymbols) 107 | System.Console.WriteLine(s); 108 | } 109 | } 110 | ``` 111 | 112 | ## Give a Star! ⭐️ 113 | 114 | If you find this repository useful, please give it a star. 115 | Thanks in advance ! 116 | 117 | ## License 118 | 119 | Copyright © 2020-2025 [Lakhya's Innovation Inc.](https://github.com/ljnath/) under the [MIT License](https://github.com/ljnath/RandomString4Net/blob/master/LICENSE). 120 | -------------------------------------------------------------------------------- /RandomString4Net/Types.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * RandomString4Net - .NET library to generate N random strings of M length from various categories 4 | * Copyright (c) 2020-2023 Lakhya Jyoti Nath 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Website: https://ljnath.com 25 | * Email: ljnath@ljnath.com / lakhya.sci@gmail.com 26 | * 27 | **/ 28 | using System; 29 | 30 | #if NET20 31 | namespace System.Runtime.CompilerServices 32 | { 33 | [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] 34 | public sealed class ExtensionAttribute : Attribute { } 35 | } 36 | #endif 37 | 38 | namespace RandomString4Net 39 | { 40 | /// 41 | /// Enum Types are the types of categories supported by the RandomString4Net library 42 | /// 43 | public enum Types 44 | { 45 | ALPHABET_LOWERCASE, 46 | ALPHABET_LOWERCASE_WITH_SYMBOLS, 47 | ALPHABET_UPPERCASE, 48 | ALPHABET_UPPERCASE_WITH_SYMBOLS, 49 | ALPHABET_MIXEDCASE, 50 | ALPHABET_MIXEDCASE_WITH_SYMBOLS, 51 | ALPHANUMERIC_LOWERCASE, 52 | ALPHANUMERIC_LOWERCASE_WITH_SYMBOLS, 53 | ALPHANUMERIC_UPPERCASE, 54 | ALPHANUMERIC_UPPERCASE_WITH_SYMBOLS, 55 | ALPHANUMERIC_MIXEDCASE, 56 | ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, 57 | NUMBERS 58 | } 59 | public static class TypesMethods 60 | { 61 | /// 62 | /// Returns an array of string corresponding to the enum Types 63 | /// 64 | /// Types for whose equivalent string needs to be constructed 65 | /// Arrray of string equivalent of Enum Types 66 | public static string[] GetString(this Types types) 67 | { 68 | switch (types) 69 | { 70 | case Types.ALPHABET_LOWERCASE: 71 | return new string[] 72 | { 73 | DataSource.Alphabets 74 | }; 75 | 76 | case Types.ALPHABET_LOWERCASE_WITH_SYMBOLS: 77 | return new string[] { 78 | DataSource.Alphabets, 79 | DataSource.Symbols 80 | }; 81 | 82 | case Types.ALPHABET_UPPERCASE: 83 | return new string[] 84 | { 85 | DataSource.Alphabets.ToUpper() 86 | }; 87 | 88 | case Types.ALPHABET_UPPERCASE_WITH_SYMBOLS: 89 | return new string[] 90 | { 91 | DataSource.Alphabets.ToUpper(), 92 | DataSource.Symbols 93 | }; 94 | 95 | case Types.ALPHABET_MIXEDCASE: 96 | return new string[] 97 | { 98 | DataSource.Alphabets, 99 | DataSource.Alphabets.ToUpper() 100 | }; 101 | 102 | case Types.ALPHABET_MIXEDCASE_WITH_SYMBOLS: 103 | return new string[] 104 | { 105 | DataSource.Alphabets, 106 | DataSource.Alphabets.ToUpper(), 107 | DataSource.Symbols 108 | }; 109 | 110 | case Types.ALPHANUMERIC_LOWERCASE: 111 | return new string[] 112 | { 113 | DataSource.Alphabets, 114 | DataSource.Numbers 115 | }; 116 | 117 | case Types.ALPHANUMERIC_LOWERCASE_WITH_SYMBOLS: 118 | return new string[] 119 | { 120 | DataSource.Alphabets, 121 | DataSource.Numbers, 122 | DataSource.Symbols 123 | }; 124 | 125 | case Types.ALPHANUMERIC_UPPERCASE: 126 | return new string[] 127 | { 128 | DataSource.Alphabets.ToUpper(), 129 | DataSource.Numbers, 130 | }; 131 | 132 | case Types.ALPHANUMERIC_UPPERCASE_WITH_SYMBOLS: 133 | return new string[] 134 | { 135 | DataSource.Alphabets.ToUpper(), 136 | DataSource.Numbers, 137 | DataSource.Symbols 138 | }; 139 | 140 | case Types.ALPHANUMERIC_MIXEDCASE: 141 | return new string[] 142 | { 143 | DataSource.Alphabets, 144 | DataSource.Alphabets.ToUpper(), 145 | DataSource.Numbers 146 | }; 147 | 148 | case Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS: 149 | return new string[] 150 | { 151 | DataSource.Alphabets, 152 | DataSource.Alphabets.ToUpper(), 153 | DataSource.Numbers, 154 | DataSource.Symbols 155 | }; 156 | 157 | case Types.NUMBERS: 158 | return new string[] 159 | { 160 | DataSource.Numbers 161 | }; 162 | } 163 | return new string[] { String.Empty }; 164 | } 165 | } 166 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | 14 | # User-specific files (MonoDevelop/Xamarin Studio) 15 | *.userprefs 16 | 17 | # Mono auto generated files 18 | mono_crash.* 19 | 20 | # Build results 21 | [Dd]ebug/ 22 | [Dd]ebugPublic/ 23 | [Rr]elease/ 24 | [Rr]eleases/ 25 | x64/ 26 | x86/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # StyleCop 66 | StyleCopReport.xml 67 | 68 | # Files built by Visual Studio 69 | *_i.c 70 | *_p.c 71 | *_h.h 72 | *.ilk 73 | *.meta 74 | *.obj 75 | *.iobj 76 | *.pch 77 | *.pdb 78 | *.ipdb 79 | *.pgc 80 | *.pgd 81 | *.rsp 82 | *.sbr 83 | *.tlb 84 | *.tli 85 | *.tlh 86 | *.tmp 87 | *.tmp_proj 88 | *_wpftmp.csproj 89 | *.log 90 | *.vspscc 91 | *.vssscc 92 | .builds 93 | *.pidb 94 | *.svclog 95 | *.scc 96 | 97 | # Chutzpah Test files 98 | _Chutzpah* 99 | 100 | # Visual C++ cache files 101 | ipch/ 102 | *.aps 103 | *.ncb 104 | *.opendb 105 | *.opensdf 106 | *.sdf 107 | *.cachefile 108 | *.VC.db 109 | *.VC.VC.opendb 110 | 111 | # Visual Studio profiler 112 | *.psess 113 | *.vsp 114 | *.vspx 115 | *.sap 116 | 117 | # Visual Studio Trace Files 118 | *.e2e 119 | 120 | # TFS 2012 Local Workspace 121 | $tf/ 122 | 123 | # Guidance Automation Toolkit 124 | *.gpState 125 | 126 | # ReSharper is a .NET coding add-in 127 | _ReSharper*/ 128 | *.[Rr]e[Ss]harper 129 | *.DotSettings.user 130 | 131 | # JustCode is a .NET coding add-in 132 | .JustCode 133 | 134 | # TeamCity is a build add-in 135 | _TeamCity* 136 | 137 | # DotCover is a Code Coverage Tool 138 | *.dotCover 139 | 140 | # AxoCover is a Code Coverage Tool 141 | .axoCover/* 142 | !.axoCover/settings.json 143 | 144 | # Visual Studio code coverage results 145 | *.coverage 146 | *.coveragexml 147 | 148 | # NCrunch 149 | _NCrunch_* 150 | .*crunch*.local.xml 151 | nCrunchTemp_* 152 | 153 | # MightyMoose 154 | *.mm.* 155 | AutoTest.Net/ 156 | 157 | # Web workbench (sass) 158 | .sass-cache/ 159 | 160 | # Installshield output folder 161 | [Ee]xpress/ 162 | 163 | # DocProject is a documentation generator add-in 164 | DocProject/buildhelp/ 165 | DocProject/Help/*.HxT 166 | DocProject/Help/*.HxC 167 | DocProject/Help/*.hhc 168 | DocProject/Help/*.hhk 169 | DocProject/Help/*.hhp 170 | DocProject/Help/Html2 171 | DocProject/Help/html 172 | 173 | # Click-Once directory 174 | publish/ 175 | 176 | # Publish Web Output 177 | *.[Pp]ublish.xml 178 | *.azurePubxml 179 | # Note: Comment the next line if you want to checkin your web deploy settings, 180 | # but database connection strings (with potential passwords) will be unencrypted 181 | *.pubxml 182 | *.publishproj 183 | 184 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 185 | # checkin your Azure Web App publish settings, but sensitive information contained 186 | # in these scripts will be unencrypted 187 | PublishScripts/ 188 | 189 | # NuGet Packages 190 | *.nupkg 191 | # NuGet Symbol Packages 192 | *.snupkg 193 | # The packages folder can be ignored because of Package Restore 194 | **/[Pp]ackages/* 195 | # except build/, which is used as an MSBuild target. 196 | !**/[Pp]ackages/build/ 197 | # Uncomment if necessary however generally it will be regenerated when needed 198 | #!**/[Pp]ackages/repositories.config 199 | # NuGet v3's project.json files produces more ignorable files 200 | *.nuget.props 201 | *.nuget.targets 202 | 203 | # Microsoft Azure Build Output 204 | csx/ 205 | *.build.csdef 206 | 207 | # Microsoft Azure Emulator 208 | ecf/ 209 | rcf/ 210 | 211 | # Windows Store app package directories and files 212 | AppPackages/ 213 | BundleArtifacts/ 214 | Package.StoreAssociation.xml 215 | _pkginfo.txt 216 | *.appx 217 | *.appxbundle 218 | *.appxupload 219 | 220 | # Visual Studio cache files 221 | # files ending in .cache can be ignored 222 | *.[Cc]ache 223 | # but keep track of directories ending in .cache 224 | !?*.[Cc]ache/ 225 | 226 | # Others 227 | ClientBin/ 228 | ~$* 229 | *~ 230 | *.dbmdl 231 | *.dbproj.schemaview 232 | *.jfm 233 | *.pfx 234 | *.publishsettings 235 | orleans.codegen.cs 236 | 237 | # Including strong name files can present a security risk 238 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 239 | #*.snk 240 | 241 | # Since there are multiple workflows, uncomment next line to ignore bower_components 242 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 243 | #bower_components/ 244 | 245 | # RIA/Silverlight projects 246 | Generated_Code/ 247 | 248 | # Backup & report files from converting an old project file 249 | # to a newer Visual Studio version. Backup files are not needed, 250 | # because we have git ;-) 251 | _UpgradeReport_Files/ 252 | Backup*/ 253 | UpgradeLog*.XML 254 | UpgradeLog*.htm 255 | ServiceFabricBackup/ 256 | *.rptproj.bak 257 | 258 | # SQL Server files 259 | *.mdf 260 | *.ldf 261 | *.ndf 262 | 263 | # Business Intelligence projects 264 | *.rdl.data 265 | *.bim.layout 266 | *.bim_*.settings 267 | *.rptproj.rsuser 268 | *- [Bb]ackup.rdl 269 | *- [Bb]ackup ([0-9]).rdl 270 | *- [Bb]ackup ([0-9][0-9]).rdl 271 | 272 | # Microsoft Fakes 273 | FakesAssemblies/ 274 | 275 | # GhostDoc plugin setting file 276 | *.GhostDoc.xml 277 | 278 | # Node.js Tools for Visual Studio 279 | .ntvs_analysis.dat 280 | node_modules/ 281 | 282 | # Visual Studio 6 build log 283 | *.plg 284 | 285 | # Visual Studio 6 workspace options file 286 | *.opt 287 | 288 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 289 | *.vbw 290 | 291 | # Visual Studio LightSwitch build output 292 | **/*.HTMLClient/GeneratedArtifacts 293 | **/*.DesktopClient/GeneratedArtifacts 294 | **/*.DesktopClient/ModelManifest.xml 295 | **/*.Server/GeneratedArtifacts 296 | **/*.Server/ModelManifest.xml 297 | _Pvt_Extensions 298 | 299 | # Paket dependency manager 300 | .paket/paket.exe 301 | paket-files/ 302 | 303 | # FAKE - F# Make 304 | .fake/ 305 | 306 | # CodeRush personal settings 307 | .cr/personal 308 | 309 | # Python Tools for Visual Studio (PTVS) 310 | __pycache__/ 311 | *.pyc 312 | 313 | # Cake - Uncomment if you are using it 314 | # tools/** 315 | # !tools/packages.config 316 | 317 | # Tabs Studio 318 | *.tss 319 | 320 | # Telerik's JustMock configuration file 321 | *.jmconfig 322 | 323 | # BizTalk build output 324 | *.btp.cs 325 | *.btm.cs 326 | *.odx.cs 327 | *.xsd.cs 328 | 329 | # OpenCover UI analysis results 330 | OpenCover/ 331 | 332 | # Azure Stream Analytics local run output 333 | ASALocalRun/ 334 | 335 | # MSBuild Binary and Structured Log 336 | *.binlog 337 | 338 | # NVidia Nsight GPU debugger configuration file 339 | *.nvuser 340 | 341 | # MFractors (Xamarin productivity tool) working folder 342 | .mfractor/ 343 | 344 | # Local History for Visual Studio 345 | .localhistory/ 346 | 347 | # BeatPulse healthcheck temp database 348 | healthchecksdb 349 | 350 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 351 | MigrationBackup/ 352 | 353 | # Ionide (cross platform F# VS Code tools) working folder 354 | .ionide/ 355 | *.nuspec 356 | *.nuspec 357 | -------------------------------------------------------------------------------- /RandomString4Net/RandomString4Net - Backup.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net20;net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;netstandard2.0;netstandard2.1;net5.0-windows;net6.0-windows;net7.0-windows;net5.0;net6.0;net7.0 4 | 1.8.0 5 | Lakhya Jyoti Nath 6 | Lakhya's Innovation Inc. 7 | .NET library to generate N random strings of M length from various categories 8 | Copyright © 2020-2023 Lakhya's Innovation Inc. 9 | MIT 10 | https://github.com/ljnath/RandomString4Net 11 | git 12 | https://github.com/ljnath/RandomString4Net.git 13 | randomstring4net ljnath lakhya lakhyajyoti innovation lakhya's inc. lakhyajyotinath randomstring random string C# libray password generator passwordgenerator generation symbols randomsymbols lowercase uppercase mixedcase customsymbols randomnumber number 14 | - Initial release with support for alphabet, alphanumeric with any without symbols for all cases 15 | - Added support for random number generation 16 | - Added support for multiple .NET Framework 17 | - Added support to generate true unique random numbers 18 | - Added performance improvement for .NET Framework > 2.0 19 | - Added support to generate random strings by including strings of each type 20 | - Added support for .NET (>5.0) 21 | - Added support for .NET Core (>2.2) 22 | - Added support for .NET Standard (>2.0) 23 | True 24 | 1.8.0.0 25 | 1.7.0.0 26 | false 27 | False 28 | $(AssemblyName) 29 | README.md 30 | True 31 | True 32 | False 33 | C:\devtools\keys\code_signing_ljnath.pfx 34 | False 35 | 36 | 37 | portable 38 | 39 | 40 | portable 41 | 42 | 43 | portable 44 | 45 | 46 | portable 47 | 48 | 49 | portable 50 | 51 | 52 | portable 53 | 54 | 55 | portable 56 | 57 | 58 | portable 59 | 60 | 61 | portable 62 | 63 | 64 | portable 65 | 66 | 67 | portable 68 | 69 | 70 | portable 71 | 72 | 73 | portable 74 | 75 | 76 | portable 77 | 78 | 79 | portable 80 | 81 | 82 | portable 83 | 84 | 85 | portable 86 | 87 | 88 | portable 89 | 90 | 91 | portable 92 | 93 | 94 | portable 95 | 96 | 97 | portable 98 | 99 | 100 | portable 101 | 102 | 103 | portable 104 | 105 | 106 | portable 107 | 108 | 109 | portable 110 | 111 | 112 | portable 113 | 114 | 115 | portable 116 | 117 | 118 | portable 119 | 120 | 121 | portable 122 | 123 | 124 | portable 125 | 126 | 127 | portable 128 | 129 | 130 | portable 131 | 132 | 133 | portable 134 | 135 | 136 | portable 137 | 138 | 139 | portable 140 | 141 | 142 | portable 143 | 144 | 145 | portable 146 | 147 | 148 | portable 149 | 150 | 151 | portable 152 | 153 | 154 | portable 155 | 156 | 157 | portable 158 | 159 | 160 | portable 161 | 162 | 163 | portable 164 | 165 | 166 | portable 167 | 168 | 169 | portable 170 | 171 | 172 | portable 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | True 182 | \ 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /RandomString4Net/RandomString4Net.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net20; 5 | net35; 6 | net40; 7 | net45; 8 | net451; 9 | net452; 10 | net46; 11 | net461; 12 | net462; 13 | net47; 14 | net471; 15 | net472; 16 | net48; 17 | net481; 18 | netcoreapp2.0; 19 | netcoreapp2.1; 20 | netcoreapp2.2;netcoreapp3.0; 21 | netcoreapp3.1;netstandard2.0; 22 | netstandard2.1; 23 | net5.0; 24 | net5.0-windows; 25 | net6.0; 26 | net6.0-windows; 27 | net7.0; 28 | net7.0-windows; 29 | net8.0; 30 | net8.0-windows; 31 | net8.0-browser; 32 | net9.0; 33 | net9.0-windows; 34 | net9.0-browser; 35 | 36 | 1.9.0 37 | Lakhya Jyoti Nath 38 | Lakhya's Innovation Inc. 39 | .NET library to generate N random strings of M length from 13 categories 40 | Copyright © 2020-2025 Lakhya's Innovation Inc. 41 | MIT 42 | https://github.com/ljnath/RandomString4Net 43 | git 44 | https://github.com/ljnath/RandomString4Net.git 45 | randomstring4net ljnath lakhya lakhyajyoti innovation lakhya's inc. lakhyajyotinath randomstring random string C# libray password generator passwordgenerator generation symbols randomsymbols lowercase uppercase mixedcase customsymbols randomnumber number 46 | 47 | - Initial release with support for alphabet, alphanumeric with any without symbols for all cases 48 | - Added support for random number generation 49 | - Added support for multiple .NET Framework 50 | - Added support to generate true unique random numbers 51 | - Added performance improvement for .NET Framework > 2.0 52 | - Added support to generate random strings by including strings of each type 53 | - Added support for .NET (>5.0) 54 | - Added support for .NET Core (>2.2) 55 | - Added support for .NET Standard (>2.0) 56 | - Added support for RandomNumberGenerator in .NET 6.0 and higher as RNGCryptoServiceProvider is marked as obsolete 57 | - Added support for .NET 8.0 58 | - Added support for .NET 9.0 59 | - Updated random seed to 4 (https://github.com/jshergal/) 60 | 61 | True 62 | 1.9.0.0 63 | 1.9.0.0 64 | false 65 | False 66 | $(AssemblyName) 67 | README.md 68 | True 69 | True 70 | False 71 | C:\devtools\keys\code_signing_ljnath.pfx 72 | False 73 | 74 | 75 | portable 76 | 77 | 78 | portable 79 | 80 | 81 | portable 82 | 83 | 84 | portable 85 | 86 | 87 | portable 88 | 89 | 90 | portable 91 | 92 | 93 | portable 94 | 95 | 96 | portable 97 | 98 | 99 | portable 100 | 101 | 102 | portable 103 | 104 | 105 | portable 106 | 107 | 108 | portable 109 | 110 | 111 | portable 112 | 113 | 114 | portable 115 | 116 | 117 | portable 118 | 119 | 120 | portable 121 | 122 | 123 | portable 124 | 125 | 126 | portable 127 | 128 | 129 | portable 130 | 131 | 132 | portable 133 | 134 | 135 | portable 136 | 137 | 138 | portable 139 | 140 | 141 | portable 142 | 143 | 144 | portable 145 | 146 | 147 | portable 148 | 149 | 150 | portable 151 | 152 | 153 | portable 154 | 155 | 156 | portable 157 | 158 | 159 | portable 160 | 161 | 162 | portable 163 | 164 | 165 | portable 166 | 167 | 168 | portable 169 | 170 | 171 | portable 172 | 173 | 174 | portable 175 | 176 | 177 | portable 178 | 179 | 180 | portable 181 | 182 | 183 | portable 184 | 185 | 186 | portable 187 | 188 | 189 | portable 190 | 191 | 192 | portable 193 | 194 | 195 | portable 196 | 197 | 198 | portable 199 | 200 | 201 | portable 202 | 203 | 204 | portable 205 | 206 | 207 | portable 208 | 209 | 210 | portable 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | True 220 | \ 221 | 222 | 223 | 224 | -------------------------------------------------------------------------------- /RandomString4NetTest/RandomString4NetTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using RandomString4Net; 3 | using RandomString4Net.Exceptions; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text.RegularExpressions; 7 | 8 | namespace RandomString4NetTest 9 | { 10 | [TestClass] 11 | public class RandomString4NetTests 12 | { 13 | [TestMethod] 14 | public void ValidateTypeGetString() 15 | { 16 | string[] numbersAsString = Types.NUMBERS.GetString(); 17 | Assert.AreEqual("0123456789", numbersAsString[0]); 18 | } 19 | 20 | [TestMethod] 21 | public void ValidateSingleRandomString() 22 | { 23 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE); 24 | Assert.IsNotNull(randomString); 25 | } 26 | 27 | [TestMethod] 28 | public void ValidateSingleRandomStringWithSymbols() 29 | { 30 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE, "+*-/"); 31 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[a-z+*-/]+$")); 32 | } 33 | 34 | [TestMethod] 35 | [ExpectedException(typeof(UnsupportedSymbolException))] 36 | public void ValidateSingleRandomStringWithBadSymbols() 37 | { 38 | RandomString.GetString(Types.ALPHABET_LOWERCASE, "+*-/₹"); 39 | } 40 | 41 | [TestMethod] 42 | [ExpectedException(typeof(InvalidLengthException))] 43 | public void ValidateSingleRandomStringOfInvalidLength() 44 | { 45 | RandomString.GetString(Types.ALPHABET_LOWERCASE, 0); 46 | } 47 | 48 | [TestMethod] 49 | public void ValidateMultipleRandomStrings() 50 | { 51 | List randomStrings = RandomString.GetStrings(Types.ALPHABET_LOWERCASE, 5); 52 | Assert.IsTrue(randomStrings.Count == 5); 53 | } 54 | 55 | [TestMethod] 56 | [ExpectedException(typeof(InvalidLengthException))] 57 | public void ValidateMultipleRandomStringsOfBadCount() 58 | { 59 | RandomString.GetStrings(Types.ALPHABET_LOWERCASE, 0); 60 | } 61 | 62 | [TestMethod] 63 | public void ValidateMultipleRandomStringsWithSymbols() 64 | { 65 | List randomStrings = RandomString.GetStrings(Types.ALPHABET_UPPERCASE, 2, "+*-/"); 66 | Regex compiledRegex = new Regex(@"^[A-Z+*-/]+$", RegexOptions.Compiled); 67 | foreach (string randomString in randomStrings) 68 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 69 | } 70 | 71 | [TestMethod] 72 | public void ValidateLowercaseAlphabet() 73 | { 74 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE); 75 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[a-z]+$")); 76 | } 77 | 78 | [TestMethod] 79 | public void ValidateUppercaseAlphabet() 80 | { 81 | string randomString = RandomString.GetString(Types.ALPHABET_UPPERCASE); 82 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[A-Z]+$")); 83 | } 84 | 85 | [TestMethod] 86 | public void ValidateMixedcaseAlphabet() 87 | { 88 | string randomString = RandomString.GetString(Types.ALPHABET_MIXEDCASE); 89 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[A-Za-z]+$")); 90 | } 91 | 92 | [TestMethod] 93 | public void ValidateLowercaseAlphabetWithCustomSymbols() 94 | { 95 | string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE, "*&^%$#@!"); 96 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[a-z*&^%$#@!]+$")); 97 | } 98 | 99 | [TestMethod] 100 | public void ValidateUppercaseAlphabetWithCustomSymbols() 101 | { 102 | string randomString = RandomString.GetString(Types.ALPHABET_UPPERCASE); 103 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[A-Z*&^%$#@!]+$")); 104 | } 105 | 106 | [TestMethod] 107 | public void ValidateMixedcaseAlphabetWithCustomSymbols() 108 | { 109 | string randomString = RandomString.GetString(Types.ALPHABET_MIXEDCASE); 110 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[a-zA-Z*&^%$#@!]+$")); 111 | } 112 | 113 | [TestMethod] 114 | public void ValidateLowercaseAlphaNumericWithSymbols() 115 | { 116 | string randomString = RandomString.GetString(Types.ALPHANUMERIC_LOWERCASE_WITH_SYMBOLS); 117 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[0-9a-z!#$%&'()*+,-./:;<=>?@[\]\\^_`{|}~""]+$")); 118 | } 119 | 120 | [TestMethod] 121 | public void ValidateUppercaseAlphaNumericWithSymbols() 122 | { 123 | string randomString = RandomString.GetString(Types.ALPHANUMERIC_UPPERCASE_WITH_SYMBOLS, 100); 124 | System.Console.WriteLine(randomString); 125 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[0-9A-Z!#$%&'()*+,-./:;<=>?@[\]\\^_`{|}~""]+$")); 126 | } 127 | 128 | [TestMethod] 129 | public void ValidateMixedcaseAlphaNumericWithSymbols() 130 | { 131 | string randomString = RandomString.GetString(Types.ALPHABET_MIXEDCASE_WITH_SYMBOLS); 132 | Assert.IsTrue(Regex.IsMatch(randomString, @"^[0-9a-zA-Z!#$%&'()*+,-./:;<=>?@[\]\\^_`{|}~""]+$")); 133 | } 134 | 135 | [TestMethod] 136 | public void ValidateRandomNumbers() 137 | { 138 | List randomStrings = RandomString.GetStrings(Types.NUMBERS, 10); 139 | Regex compiledRegex = new Regex("^[0-9]+$", RegexOptions.Compiled); 140 | foreach (string randomString in randomStrings) 141 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 142 | } 143 | 144 | [TestMethod] 145 | public void ValidateRandomNumbersOfMaxLength() 146 | { 147 | List randomStrings = RandomString.GetStrings(Types.NUMBERS, 10, 20); 148 | Regex compiledRegex = new Regex("^[0-9]{20}$", RegexOptions.Compiled); 149 | foreach (string randomString in randomStrings) 150 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 151 | } 152 | 153 | [TestMethod] 154 | public void ValidateRandomNumbersOfRandomLength() 155 | { 156 | List randomStrings = RandomString.GetStrings(Types.NUMBERS, 10, 15, true); 157 | Regex compiledRegex = new Regex("^[0-9]{1,15}$", RegexOptions.Compiled); 158 | foreach (string randomString in randomStrings) 159 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 160 | } 161 | 162 | [TestMethod] 163 | public void ValidateTrueRandomness() 164 | { 165 | for (int i = 0; i < 100; i++) 166 | { 167 | List randomStrings = RandomString.GetStrings(Types.NUMBERS, 100000, 10, false, true); 168 | var anyDuplicate = randomStrings.GroupBy(x => x).Any(g => g.Count() > 1); 169 | Assert.IsFalse(anyDuplicate); 170 | } 171 | } 172 | 173 | [TestMethod] 174 | public void ValidateForceOccuranceForAlphabetWithCustomSymbols() 175 | { 176 | Regex compiledRegex = new Regex("^[a-z*&^%$#@!]{20}$", RegexOptions.Compiled); 177 | for (int i = 0; i < 100; i++) 178 | { 179 | List randomStrings = RandomString.GetStrings(Types.ALPHABET_LOWERCASE_WITH_SYMBOLS, symbolsToInclude: "*&^%$#@!", count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 180 | foreach (string randomString in randomStrings) 181 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 182 | } 183 | } 184 | 185 | [TestMethod] 186 | public void ValidateForceOccuranceForAlphabetUppercaseWithCustomSymbols() 187 | { 188 | Regex compiledRegex = new Regex("^[A-Z*&^%$#@!]{20}$", RegexOptions.Compiled); 189 | for (int i = 0; i < 100; i++) 190 | { 191 | List randomStrings = RandomString.GetStrings(Types.ALPHABET_UPPERCASE_WITH_SYMBOLS, symbolsToInclude: "*&^%$#@!", count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 192 | foreach (string randomString in randomStrings) 193 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 194 | } 195 | } 196 | 197 | [TestMethod] 198 | public void ValidateForceOccuranceForAlphaNumericLowercase() 199 | { 200 | Regex compiledRegex = new Regex("^[a-z0-9]{20}$", RegexOptions.Compiled); 201 | for (int i = 0; i < 100; i++) 202 | { 203 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_LOWERCASE, count:1000, maxLength:20, forceUnique:false, forceOccuranceOfEachType:true); 204 | foreach (string randomString in randomStrings) 205 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 206 | } 207 | } 208 | 209 | [TestMethod] 210 | public void ValidateForceOccuranceForAlphaNumericLowercaseWithCustomSymbols() 211 | { 212 | Regex compiledRegex = new Regex("^[a-z0-9*&^%$#@!]{20}$", RegexOptions.Compiled); 213 | for (int i = 0; i < 100; i++) 214 | { 215 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_LOWERCASE_WITH_SYMBOLS, symbolsToInclude:"*&^%$#@!", count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 216 | foreach (string randomString in randomStrings) 217 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 218 | } 219 | } 220 | 221 | [TestMethod] 222 | public void ValidateForceOccuranceForAlphaNumericUppercase() 223 | { 224 | Regex compiledRegex = new Regex("^[A-Z0-9]{20}$", RegexOptions.Compiled); 225 | for (int i = 0; i < 100; i++) 226 | { 227 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_UPPERCASE, count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 228 | foreach (string randomString in randomStrings) 229 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 230 | } 231 | } 232 | 233 | [TestMethod] 234 | public void ValidateForceOccuranceForAlphaNumericUppercaseWithCustomSymbols() 235 | { 236 | Regex compiledRegex = new Regex("^[A-Z0-9*&^%$#@!]{20}$", RegexOptions.Compiled); 237 | for (int i = 0; i < 100; i++) 238 | { 239 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_UPPERCASE_WITH_SYMBOLS, symbolsToInclude: "*&^%$#@!", count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 240 | foreach (string randomString in randomStrings) 241 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 242 | } 243 | } 244 | 245 | [TestMethod] 246 | public void ValidateForceOccuranceForAlphaNumericMixedcase() 247 | { 248 | Regex compiledRegex = new Regex("^[a-zA-Z0-9]{20}$", RegexOptions.Compiled); 249 | for (int i = 0; i < 100; i++) 250 | { 251 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE, count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 252 | foreach (string randomString in randomStrings) 253 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 254 | } 255 | } 256 | 257 | [TestMethod] 258 | public void ValidateForceOccuranceForAlphaNumericMixedcaseWithCustomSymbols() 259 | { 260 | Regex compiledRegex = new Regex("^[a-zA-Z0-9*&^%$#@!]{20}$", RegexOptions.Compiled); 261 | for (int i = 0; i < 100; i++) 262 | { 263 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, symbolsToInclude: "*&^%$#@!", count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 264 | foreach (string randomString in randomStrings) 265 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 266 | } 267 | } 268 | 269 | [TestMethod] 270 | public void ValidateForceOccuranceForAlphaNumericMixedcaseWithoutCustomSymbols() 271 | { 272 | Regex compiledRegex = new Regex(@"^[0-9a-zA-Z!#$%&'()*+,-.:;<=>?@[\]/\\|^_`{}~""]{20}$", RegexOptions.Compiled); 273 | for (int i = 0; i < 100; i++) 274 | { 275 | List randomStrings = RandomString.GetStrings(Types.ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS, count: 1000, maxLength: 20, forceUnique: false, forceOccuranceOfEachType: true); 276 | foreach (string randomString in randomStrings) 277 | Assert.IsTrue(compiledRegex.IsMatch(randomString)); 278 | } 279 | } 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /RandomString4Net/RandomString.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * RandomString4Net - .NET library to generate N random strings of M length from various categories 4 | * Copyright (c) 2020-2025 Lakhya Jyoti Nath 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | * 24 | * Website: https://ljnath.com 25 | * Email: ljnath@ljnath.com / lakhya.sci@gmail.com 26 | * 27 | **/ 28 | using RandomString4Net.Exceptions; 29 | using System; 30 | using System.Collections.Generic; 31 | using System.Security.Cryptography; 32 | using System.Text; 33 | using System.Text.RegularExpressions; 34 | 35 | #if ! NET20 36 | using System.Linq; 37 | #endif 38 | 39 | namespace RandomString4Net 40 | { 41 | /// 42 | /// Main class of the library containing the publically exposed methods and internal logic for random number generation 43 | /// 44 | public static class RandomString 45 | { 46 | /// 47 | /// Generates a random string of input type types having a maximum string length of maxLength 48 | /// 49 | /// Type of RandomString4Net.Types is the type of input string for random string generation 50 | /// Maximum length of a random string to be generated; default is 10 51 | /// Boolean choice if the length of the generated random string should be random as well 52 | /// Boolean choice to indicate if string of each sub-type should present in the generated random string 53 | /// A newly generated random string 54 | /// Thown if count or maxLenght is less then or equal to 0 55 | public static string GetString(Types types, int maxLength = 10, bool randomLength = false, bool forceOccuranceOfEachType = false) 56 | { 57 | return GetRandomStrings(types.GetString(), 1, maxLength, randomLength, false, forceOccuranceOfEachType)[0]; 58 | } 59 | 60 | /// 61 | /// Generates a random string of input type types having a maximum string length of maxLength including symbols specified as symbolsToInclude 62 | /// 63 | /// Type of RandomString4Net.Types is the type of input string for random string generation 64 | /// Subset of symbols from the list of supported symbols, specified as a string 65 | /// Maximum length of a random string to be generated; default is 10 66 | /// Boolean choice if the length of the generated random string should be random as well 67 | /// Boolean choice to indicate if string of each sub-type should present in the generated random string 68 | /// A newly generated random string 69 | /// Thown when the input subset of string is not present in the list of supported symbols 70 | /// Thown if count or maxLenght is less then or equal to 0 71 | public static string GetString(Types types, string symbolsToInclude, int maxLength = 10, bool randomLength = false, bool forceOccuranceOfEachType = false) 72 | { 73 | #if NET20 74 | List inputStrings = new List(); 75 | #else 76 | HashSet inputStrings = new HashSet(); 77 | #endif 78 | foreach (String tempString in types.GetString()) 79 | // excluding symbols as custom symbols are specified 80 | if (!tempString.Contains("@")) 81 | inputStrings.Add(tempString); 82 | 83 | ValidateSymbols(symbolsToInclude); 84 | inputStrings.Add(symbolsToInclude); 85 | 86 | return GetRandomStrings(inputStrings.ToArray(), 1, maxLength, randomLength, false, forceOccuranceOfEachType)[0]; 87 | } 88 | 89 | /// 90 | /// Generates a list of random strings of input type types having a maximum string length of maxLength 91 | /// 92 | /// Type of RandomString4Net.Types is the type of input string for random string generation 93 | /// Number of random strings to be generated 94 | /// Maximum length of a random string to be generated; default is 10 95 | /// Boolean choice if the length of the generated random string should be random as well 96 | /// Boolean choice to force generation of only unique numbers, count may not be met if this is set to true 97 | /// Boolean choice to indicate if string of each sub-type should present in the generated random string 98 | /// A list of random strings 99 | /// Thown if count or maxLenght is less then or equal to 0 100 | public static List GetStrings(Types types, int count, int maxLength = 10, bool randomLength = false, bool forceUnique = false, bool forceOccuranceOfEachType = false) 101 | { 102 | return GetRandomStrings(types.GetString(), count, maxLength, randomLength, forceUnique, forceOccuranceOfEachType); 103 | } 104 | 105 | /// 106 | /// Generates a list of random strings of input type types having a maximum string length of maxLength 107 | /// 108 | /// Type of RandomString4Net.Types is the type of input string for random string generation 109 | /// Number of random strings to be generated 110 | /// Subset of symbols from the list of supported symbols, specified as a string 111 | /// Maximum length of a random string to be generated; default is 10 112 | /// Boolean choice if the length of the generated random string should be random as well 113 | /// Boolean choice to force generation of only unique numbers, count may not be met if this is set to true 114 | /// Boolean choice to indicate if string of each sub-type should present in the generated random string 115 | /// A list of random strings 116 | /// Thown when the input subset of string is not present in the list of supported symbols 117 | /// Thown if count or maxLenght is less then or equal to 0 118 | public static List GetStrings(Types types, int count, string symbolsToInclude, int maxLength = 10, bool randomLength = false, bool forceUnique = false, bool forceOccuranceOfEachType = false) 119 | { 120 | #if NET20 121 | List inputStrings = new List(); 122 | #else 123 | HashSet inputStrings = new HashSet(); 124 | #endif 125 | foreach (String tempString in types.GetString()) 126 | // excluding symbols as custom symbols are specified 127 | if (!tempString.Contains("@")) 128 | inputStrings.Add(tempString); 129 | 130 | ValidateSymbols(symbolsToInclude); 131 | inputStrings.Add(symbolsToInclude); 132 | 133 | return GetRandomStrings(inputStrings.ToArray(), count, maxLength, randomLength, forceUnique, forceOccuranceOfEachType); 134 | } 135 | 136 | /// 137 | /// Checks if all the symbols specified in inputSybols are present in the list of supported symbols 138 | /// 139 | /// String of symbols for validation 140 | /// Thown when the input symbols are not present in the list of supported symbols 141 | private static void ValidateSymbols(string inputSymbols) 142 | { 143 | if (!Regex.IsMatch(inputSymbols, string.Format(@"^[{0}]+$", DataSource.Symbols))) 144 | throw new UnsupportedSymbolException(string.Format("Input symbols should be a subset of {0}", DataSource.Symbols)); 145 | } 146 | 147 | /// 148 | /// Method responsible for generating random strings 149 | /// 150 | /// String whose characters are to be used for generating random strings 151 | /// Number of random string to generate 152 | /// Maximum length of random string 153 | /// Boolean choice if the length of the generated random string should be random as well 154 | /// Boolean choice to force generation of only unique numbers, count may not be met if this is set to true 155 | /// Boolean choice to indicate if string of each sub-type should present in the generated random string 156 | /// A list of random strings 157 | /// Thown if count or maxLenght is less then or equal to 0 158 | private static List GetRandomStrings(string[] inputStrings, int count, int maxLength, bool randomLength, bool forceUnique, bool forceOccuranceOfEachType) 159 | { 160 | if (count <= 0 || maxLength <= 0) 161 | throw new InvalidLengthException("Number and length of random strings should be a non-zero postive numbver"); 162 | 163 | byte[] randomSeeds = new byte[4]; 164 | 165 | #if NET6_0_OR_GREATER 166 | RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create(); 167 | randomNumberGenerator.GetBytes(randomSeeds); 168 | #else 169 | // generating a random seed for the Random() 170 | RNGCryptoServiceProvider rngCryptoServiceProvider = new RNGCryptoServiceProvider(); 171 | rngCryptoServiceProvider.GetBytes(randomSeeds); 172 | #endif 173 | 174 | // creating an instance of Random() using the random seed value 175 | Random random = new Random(BitConverter.ToInt32(randomSeeds, 0)); 176 | 177 | if (!forceOccuranceOfEachType) 178 | return getRandomStrings(random, String.Join("", inputStrings), count, maxLength, randomLength, forceUnique); 179 | else 180 | return getRandomStrings(random, inputStrings, count, maxLength, randomLength, forceUnique); 181 | } 182 | 183 | private static List getRandomStrings(Random randomInstance, string inputString, int count, int maxLength, bool randomLength, bool forceUnique) 184 | { 185 | #if NET20 186 | List randomStrings = new List(); 187 | #else 188 | HashSet randomStrings = new HashSet(); 189 | #endif 190 | 191 | int inputStringLength = inputString.Length; 192 | int outputStringLength; 193 | 194 | for (int i = 0; i < count; i++) 195 | { 196 | outputStringLength = randomLength ? randomInstance.Next(1, maxLength) : maxLength; 197 | StringBuilder currentRandomString = new StringBuilder(); 198 | 199 | for (int j = 0; j < outputStringLength; j++) 200 | currentRandomString.Append(inputString[randomInstance.Next(inputStringLength)]); 201 | 202 | if (forceUnique && randomStrings.Contains(currentRandomString.ToString())) 203 | continue; 204 | 205 | randomStrings.Add(currentRandomString.ToString()); 206 | } 207 | 208 | #if NET20 209 | return randomStrings; 210 | #else 211 | List finalRandomStrings = randomStrings.ToList(); 212 | return finalRandomStrings; 213 | #endif 214 | } 215 | 216 | private static List getRandomStrings(Random randomInstance, string[] inputStrings, int count, int maxLength, bool randomLength, bool forceUnique) 217 | { 218 | #if NET20 219 | List randomStrings = new List(); 220 | #else 221 | HashSet randomStrings = new HashSet(); 222 | #endif 223 | 224 | int inputTypeIndex, inputStringLength, outputStringLength; 225 | 226 | while (randomStrings.Count < count) 227 | { 228 | outputStringLength = randomLength ? randomInstance.Next(1, maxLength) : maxLength; 229 | StringBuilder currentRandomString = new StringBuilder(); 230 | 231 | for (int j = 0; j < outputStringLength; j++) 232 | { 233 | inputTypeIndex = randomInstance.Next(0, inputStrings.Length); 234 | 235 | inputStringLength = inputStrings[inputTypeIndex].Length; 236 | currentRandomString.Append(inputStrings[inputTypeIndex][randomInstance.Next(inputStringLength)]); 237 | } 238 | 239 | if (forceUnique && randomStrings.Contains(currentRandomString.ToString())) 240 | continue; 241 | randomStrings.Add(currentRandomString.ToString()); 242 | } 243 | 244 | #if NET20 245 | return randomStrings; 246 | #else 247 | List finalRandomStrings = randomStrings.ToList(); 248 | return finalRandomStrings; 249 | #endif 250 | } 251 | } 252 | } 253 | --------------------------------------------------------------------------------