Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Last active September 13, 2024 20:39
Show Gist options
  • Save karenpayneoregon/223a7f1aea36ba2eaf38def3df40ba8c to your computer and use it in GitHub Desktop.
Save karenpayneoregon/223a7f1aea36ba2eaf38def3df40ba8c to your computer and use it in GitHub Desktop.
Random
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);
}
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);
}
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