Last active
December 16, 2015 06:49
-
-
Save Konard/5394546 to your computer and use it in GitHub Desktop.
RandomExtensions is a container of extensions to System.Random class.
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; | |
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