Skip to content

Instantly share code, notes, and snippets.

@Konard
Last active December 16, 2015 06:49
Show Gist options
  • Save Konard/5394546 to your computer and use it in GitHub Desktop.
Save Konard/5394546 to your computer and use it in GitHub Desktop.
RandomExtensions is a container of extensions to System.Random class.
using System;
namespace Konard.Helpers
{
public static class RandomExtensions
{
public static string NextString(this Random rnd, int length)
{
if (length <= 0)
return String.Empty;
return rnd.NextBytes(length).ConvertToString();
}
public static byte[] NextBytes(this Random rnd, int length)
{
if (length <= 0)
return new byte[0];
byte[] randomBytes = new byte[length];
rnd.NextBytes(randomBytes);
return randomBytes;
}
public static ulong NextUInt64(this Random rnd)
{
return rnd.NextUInt64(UInt64.MaxValue);
}
public static ulong NextUInt64(this Random rnd, ulong maxValue)
{
return rnd.NextUInt64(UInt64.MinValue, maxValue);
}
public static ulong NextUInt64(this Random rnd, ulong minValue, ulong maxValue)
{
if (minValue >= maxValue)
return minValue;
return (ulong)(rnd.NextDouble() * (maxValue - minValue)) + minValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment