You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usingDryFish.ILib.Random;// User provides their own arraystring[]names={"an","binh","chi"};int[]numbers={1,2,3,4,5};// Random from user's arraystringrandomName=ILibRandom.IRandomFromArray(names);// returns "binh"intrandomNumber=ILibRandom.IRandomFromArray(numbers);// returns 3// Random numbersintdice=ILibRandom.IRandomInt(1,6);// returns 4longbigNum=ILibRandom.IRandomLong(1000,9999);// returns 5432doublepercent=ILibRandom.IRandomDouble(0.0,1.0);// returns 0.73decimalprice=ILibRandom.IRandomDecimal(0m,100m,2);// returns 42.50boolisHeads=ILibRandom.IRandomBool();// returns true// Random characterscharupper=ILibRandom.IRandomUppercase();// returns 'X'charlower=ILibRandom.IRandomLowercase();// returns 'm'charalphabet=ILibRandom.IRandomAlphabet('A','Z');// returns 'G'// Random colorsstringhexColor=ILibRandom.IRandomHexColor();// returns "#FF5733"stringconsoleColor=ILibRandom.IRandomConsoleColor();// returns "cyan"// Random GUIDstringguid=ILibRandom.IRandomGuid();// returns "a1b2c3d4-..."// Random from Listvarcities=newList<string>{"Hanoi","Saigon","Danang"};stringcity=ILibRandom.IRandomItem(cities);// returns "Saigon"// Random EnumenumStatus{Active,Inactive,Pending}Statusstatus=ILibRandom.IRandomEnum<Status>();// returns InactiveStatusstatusExcludingPending=ILibRandom.IRandomEnum(Status.Pending);// returns Active or Inactive
π API Reference
Array Methods
Method
Description
Example
Throws
IRandomFromArray(string[] array)
Random element from string array
ILibRandom.IRandomFromArray(names)
ArgumentException if null/empty
IRandomFromArray<T>(T[] array)
Random element from generic array
ILibRandom.IRandomFromArray(numbers)
ArgumentException if null/empty
Number Methods
Method
Description
Example
Throws
IRandomInt(int min, int max)
Random integer (inclusive)
ILibRandom.IRandomInt(1, 10)
ArgumentException if min > max
IRandomInt()
Random integer (0-100)
ILibRandom.IRandomInt()
-
IRandomLong(long min, long max)
Random long (inclusive)
ILibRandom.IRandomLong(1000, 9999)
ArgumentException if min > max
IRandomDouble(double min, double max)
Random double
ILibRandom.IRandomDouble(0.5, 1.5)
ArgumentException if min > max
IRandomBool()
Random boolean
ILibRandom.IRandomBool()
-
Decimal Methods
Method
Description
Example
Throws
IRandomDecimal(decimal min, decimal max)
Random decimal
ILibRandom.IRandomDecimal(0m, 100m)
ArgumentException if min > max
IRandomDecimal(decimal min, decimal max, int precision)
Random decimal with precision
ILibRandom.IRandomDecimal(0m, 100m, 2)
ArgumentException if min > max or precision invalid (0-28)
Character Methods
Method
Description
Example
Throws
IRandomChar(char min, char max)
Random character
ILibRandom.IRandomChar('A', 'Z')
ArgumentException if min > max
IRandomAlphabet(char min, char max)
Random letter only (A-Z or a-z)
ILibRandom.IRandomAlphabet('A', 'Z')
ArgumentException if invalid range
IRandomUppercase()
Random uppercase letter (A-Z)
ILibRandom.IRandomUppercase()
-
IRandomLowercase()
Random lowercase letter (a-z)
ILibRandom.IRandomLowercase()
-
Collection Methods
Method
Description
Example
Throws
IRandomItem<T>(IList<T> list)
Random item from list
ILibRandom.IRandomItem(myList)
ArgumentException if null/empty
Enum Methods
Method
Description
Example
Throws
IRandomEnum<T>() where T : Enum
Random enum value
ILibRandom.IRandomEnum<Status>()
ArgumentException if enum empty
IRandomEnum<T>(T exclude) where T : Enum
Random enum value excluding one
ILibRandom.IRandomEnum(Status.Pending)
ArgumentException if all excluded
Color Methods
Method
Description
Example
Throws
IRandomHexColor()
Random hex color (#RRGGBB)
ILibRandom.IRandomHexColor()
-
IRandomConsoleColor()
Random console color name
ILibRandom.IRandomConsoleColor()
-
Utility Methods
Method
Description
Example
Throws
IRandomGuid()
Random GUID string
ILibRandom.IRandomGuid()
-
π‘ Examples
Basic Usage with User-Provided Arrays
usingDryFish.ILib.Random;// User provides their own arraysstring[]names={"An","Binh","Chi","Dung"};stringwinner=ILibRandom.IRandomFromArray(names);Console.WriteLine($"Winner: {winner}");int[]scores={95,87,76,91,88};intrandomScore=ILibRandom.IRandomFromArray(scores);Console.WriteLine($"Random score: {randomScore}");
// Random price with 2 decimal placesdecimalprice=ILibRandom.IRandomDecimal(0m,100m,2);Console.WriteLine($"Price: ${price:F2}");// Random temperature with 1 decimal placedecimaltemperature=ILibRandom.IRandomDecimal(-10m,40m,1);Console.WriteLine($"Temperature: {temperature:F1}Β°C");
Random Enum Values
enumUserRole{Admin,Moderator,User,Guest}// Random roleUserRolerole=ILibRandom.IRandomEnum<UserRole>();Console.WriteLine($"Selected role: {role}");// Random role excluding AdminUserRolenonAdminRole=ILibRandom.IRandomEnum(UserRole.Admin);Console.WriteLine($"Non-admin role: {nonAdminRole}");
// Generate a random personstring[]firstNames={"An","Binh","Chi"};string[]lastNames={"Nguyen","Tran","Le"};stringfirstName=ILibRandom.IRandomFromArray(firstNames);stringlastName=ILibRandom.IRandomFromArray(lastNames);intage=ILibRandom.IRandomInt(18,65);decimalsalary=ILibRandom.IRandomDecimal(1000m,5000m,2);boolisActive=ILibRandom.IRandomBool();varperson=new{Name=$"{firstName}{lastName}",Age=age,Salary=salary,IsActive=isActive};
β‘ Performance Optimizations
Enum Caching: Enum values are cached after first use for zero allocations on subsequent calls
Byte Buffer Reuse: Thread-static byte buffers for efficient random generation
.NET 6+ Optimizations: Uses Random.Shared and stackalloc for maximum performance
No Reflection Overhead: Optimized for high-frequency calls