DEV Community

Cover image for The easiest way to generate random strings in C# using RandomString4Net library
Lakhya Jyoti Nath
Lakhya Jyoti Nath

Posted on • Updated on • Originally published at Medium

The easiest way to generate random strings in C# using RandomString4Net library

Introduction

Random data is a fundamental component in a wide range of applications and business scenarios, whether it involves creating random passwords for registered users, generating random usernames, or assigning random filenames. Virtually all programming languages support this functionality by providing mechanisms for generating random numbers, which, in turn, can be used to create random strings. C# is no exception to this, offering the ‘Random’ class that enables developers to generate random numbers.

For most developers, the "Random" class is sufficient to meet their random data generation needs. However, when it comes to generating random data of various data types, developers often find themselves writing extensive boilerplate code.

To simplify and expedite this process, developers can take advantage of the open-source C# library known as RandomString4Net. This library streamlines the creation of random data by handling all the cumbersome boilerplate code, offering straightforward yet robust methods like GetString() and GetStrings() for obtaining either a single random content item or a list of random content.

What types are supported ?

RandomString4Net offers support for generating random strings across a broad spectrum of data types. Here are the types it supports:

  • NUMBER : 0123456789
  • SYMBOLS : !#$%&'()*+,-./:;<=>?@[]^_`{|}~"
  • ALPHABET_LOWERCASE : abcdefghijklmnopqrstuvwxyz
  • ALPHABET_UPPERCASE : ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • ALPHABET_MIXEDCASE : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  • ALPHANUMERIC_LOWERCASE : abcdefghijklmnopqrstuvwxyz0123456789
  • ALPHANUMERIC_UPPERCASE : ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
  • ALPHANUMERIC_MIXEDCASE : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
  • ALPHABET_LOWERCASE_WITH_SYMBOLS : abcdefghijklmnopqrstuvwxyz!#$%&'()*+,-./:;<=>?@[]^_`{|}~"
  • ALPHABET_UPPERCASE_WITH_SYMBOLS : ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+,-./:;<=>?@[]^_`{|}~"
  • ALPHABET_MIXEDCASE_WITH_SYMBOLS : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+,-./:;<=>?@[]^_`{|}~"
  • ALPHANUMERIC_LOWERCASE_WITH_SYMBOLS : abcdefghijklmnopqrstuvwxyz0123456789!#$%&'()*+,-./:;<=>?@[]^_`{|}~"
  • ALPHANUMERIC_UPPERCASE_WITH_SYMBOLS : ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'()*+,-./:;<=>?@[]^_`{|}~"
  • ALPHANUMERIC_MIXEDCASE_WITH_SYMBOLS : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" !#$%&'()*+,-./:;<=>?@[]^_`{|}~"

How to install ?

RandomString4Net is conveniently available as a NuGet package, making it easy to incorporate into your C# projects. You can install it either through the NuGet user interface or by utilizing the package manager with the following command:


Install-Package RandomString4Net

How to use ?

  1. Add Reference: Ensure you have added the RandomString4Net library as a reference in your C# project. You can do this through the NuGet package manager or by manually including the library in your project.

  2. Using Statement: In your C# code file, include the using directive for the RandomString4Net namespace. This allows you to access the library’s functionality without specifying the full namespace path each time.


using RandomString4Net;

  1. Call Public Methods: You can now call the public methods provided by the RandomString4Net library to generate random data based on your specific requirements. For example, if you want to create a random alphanumeric string, you can use the GetString method like this:


// this will generate a single random string with lowercase alphabet of length 15 characters
string randomString = RandomString.GetString(Types.ALPHABET_LOWERCASE, 15);

You can replace Types.ALPHABET_LOWERCASE with any valid type explained above. To generate multiple random strings, you can use the GetStrings method provided by the RandomString4Net library. Here's how you can use it:


// this will generate a 10 random strings with mixedcase alphabet and numbers of max length 20 characters
List<string> randomAlphaNumbericStrings = RandomString.GetString(Types.ALPHANUMERIC_MIXEDCASE, 10, 20);

Both the GetString and GetStrings methods in the RandomString4Net library provide a range of parameters that enable developers to fine-tune the generated random strings to suit their specific requirements. Here are the parameters supported by these methods:

  • Types types : type of random string(s) to be generated.
  • int count : number of random strings to be generated.
  • string symbolsToInclude : combination of custom symbols as strings that needs to be included in the geneareted * * * * ransom string. This is not depended on the types and it can be used with any type.
  • int maxLength (optional) : maximum number of characters in a single random string. The default value is 10.
  • bool randomLength (optional) : if length of random strings should also be random which maximum length defined by * * * maxLength. The default value is false.
  • bool forceUnique (optional) : to force that all the generated random strings are unique, this may result in generating less number of random strings than specified in the count parameter due to max possibility number of random strings . The default value is false.
  • bool forceOccuranceOfEachType (optional) : to force inclusion of each sub-types in the generated random string. For example when type is ‘ALPHABET_MIXEDCASE_WITH_SYMBOLS’ , this boolean flag will make sure that the generated random string contains character of lowercase alphabet, uppercase alphabet and symbols. The default value is false.

Example

Reference

Top comments (0)