Last active
September 13, 2024 20:39
-
-
Save karenpayneoregon/223a7f1aea36ba2eaf38def3df40ba8c to your computer and use it in GitHub Desktop.
Random
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
public static class Helpers | |
{ | |
public static string RandomNumberString(int length) => | |
RandomNumberGenerator.GetString( | |
choices: new string('a'.To('z').Concat('0'.To('9')).ToArray()), | |
length: length | |
); | |
public static T[] RandomElements<T>(this T[] source, int count) => | |
RandomNumberGenerator.GetItems<T>(source,count); | |
public static IEnumerable<char> To(this char start, char end) => | |
Enumerable.Range(start, end - start + 1).Select(i => (char)i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Helpers | |
{ | |
public static string RandomNumberString(int length) => | |
RandomNumberGenerator.GetString( | |
choices: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | |
length: length | |
); | |
public static T[] RandomElements<T>(this T[] source, int count) => | |
RandomNumberGenerator.GetItems<T>(source,count); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Debug.WriteLine(Helpers.RandomNumberString(20)); | |
string[] names = ["John", "Karen", "Adam", "Kevin", "Mary", "Ben", "Terry"]; | |
var result = names.RandomElements(3); | |
Debug.WriteLine(string.Join(", ", result)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment