mirror of
https://github.com/tonytins/Lorem.NET.git
synced 2025-03-22 07:32:23 +00:00
Added RandomEnum<TEnum>()
This commit is contained in:
parent
0f898fa17f
commit
d579653036
7 changed files with 125 additions and 110 deletions
|
@ -4,146 +4,156 @@ using System.Linq;
|
||||||
|
|
||||||
namespace LoremNET
|
namespace LoremNET
|
||||||
{
|
{
|
||||||
public partial class Lorem
|
public partial class Lorem
|
||||||
{
|
{
|
||||||
public static bool Chance(int successes, int attempts)
|
public static bool Chance(int successes, int attempts)
|
||||||
{
|
{
|
||||||
var number = Number(1, attempts);
|
var number = Number(1, attempts);
|
||||||
|
|
||||||
return number <= successes;
|
return number <= successes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static T Random<T>(T[] items)
|
public static T Random<T>(T[] items)
|
||||||
{
|
{
|
||||||
var index = RandomHelper.Instance.Next(items.Length);
|
var index = RandomHelper.Instance.Next(items.Length);
|
||||||
|
|
||||||
return items[index];
|
return items[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* http://stackoverflow.com/a/6651661/234132 */
|
public static TEnum RandomEnum<TEnum>()
|
||||||
public static long Number(long min, long max)
|
{
|
||||||
{
|
if (typeof(TEnum).IsEnum)
|
||||||
byte[] buf = new byte[8];
|
{
|
||||||
RandomHelper.Instance.NextBytes(buf);
|
var v = Enum.GetValues(typeof(TEnum));
|
||||||
long longRand = BitConverter.ToInt64(buf, 0);
|
return (TEnum)v.GetValue(LoremNET.RandomHelper.Instance.Next(v.Length));
|
||||||
|
}
|
||||||
|
else throw new ArgumentException("Generic type must be an enum.");
|
||||||
|
}
|
||||||
|
|
||||||
return (Math.Abs(longRand % ((max + 1) - min)) + min);
|
/* http://stackoverflow.com/a/6651661/234132 */
|
||||||
}
|
public static long Number(long min, long max)
|
||||||
|
{
|
||||||
|
byte[] buf = new byte[8];
|
||||||
|
RandomHelper.Instance.NextBytes(buf);
|
||||||
|
long longRand = BitConverter.ToInt64(buf, 0);
|
||||||
|
|
||||||
#region DateTime
|
return (Math.Abs(longRand % ((max + 1) - min)) + min);
|
||||||
|
}
|
||||||
|
|
||||||
public static DateTime DateTime(int startYear = 1950, int startMonth = 1, int startDay = 1)
|
#region DateTime
|
||||||
{
|
|
||||||
return DateTime(new System.DateTime(startYear, startMonth, startDay), System.DateTime.Now);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static DateTime DateTime(DateTime min)
|
public static DateTime DateTime(int startYear = 1950, int startMonth = 1, int startDay = 1)
|
||||||
{
|
{
|
||||||
return DateTime(min, System.DateTime.Now);
|
return DateTime(new System.DateTime(startYear, startMonth, startDay), System.DateTime.Now);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* http://stackoverflow.com/a/1483677/234132 */
|
public static DateTime DateTime(DateTime min)
|
||||||
public static DateTime DateTime(DateTime min, DateTime max)
|
{
|
||||||
{
|
return DateTime(min, System.DateTime.Now);
|
||||||
TimeSpan timeSpan = max - min;
|
}
|
||||||
TimeSpan newSpan = new TimeSpan(0, RandomHelper.Instance.Next(0, (int)timeSpan.TotalMinutes), 0);
|
|
||||||
|
|
||||||
return min + newSpan;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
/* http://stackoverflow.com/a/1483677/234132 */
|
||||||
|
public static DateTime DateTime(DateTime min, DateTime max)
|
||||||
|
{
|
||||||
|
TimeSpan timeSpan = max - min;
|
||||||
|
TimeSpan newSpan = new TimeSpan(0, RandomHelper.Instance.Next(0, (int)timeSpan.TotalMinutes), 0);
|
||||||
|
|
||||||
#region Text
|
return min + newSpan;
|
||||||
|
}
|
||||||
|
|
||||||
public static string Email()
|
#endregion
|
||||||
{
|
|
||||||
return string.Format("{0}@{1}.com", Lorem.Words(1, false), Lorem.Words(1, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Words(int wordCount, bool uppercaseFirstLetter = true, bool includePunctuation = false)
|
#region Text
|
||||||
{
|
|
||||||
return Words(wordCount, wordCount, uppercaseFirstLetter, includePunctuation);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Words(int wordCountMin, int wordCountMax, bool uppercaseFirstLetter = true, bool includePunctuation = false)
|
public static string Email()
|
||||||
{
|
{
|
||||||
var source = string.Join(" ", Source.WordList(includePunctuation).Take(RandomHelper.Instance.Next(wordCountMin, wordCountMax)));
|
return string.Format("{0}@{1}.com", Lorem.Words(1, false), Lorem.Words(1, false));
|
||||||
|
}
|
||||||
|
|
||||||
if (uppercaseFirstLetter)
|
public static string Words(int wordCount, bool uppercaseFirstLetter = true, bool includePunctuation = false)
|
||||||
{
|
{
|
||||||
source = source.UppercaseFirst();
|
return Words(wordCount, wordCount, uppercaseFirstLetter, includePunctuation);
|
||||||
}
|
}
|
||||||
|
|
||||||
return source;
|
public static string Words(int wordCountMin, int wordCountMax, bool uppercaseFirstLetter = true, bool includePunctuation = false)
|
||||||
}
|
{
|
||||||
|
var source = string.Join(" ", Source.WordList(includePunctuation).Take(RandomHelper.Instance.Next(wordCountMin, wordCountMax)));
|
||||||
|
|
||||||
public static string Sentence(int wordCount)
|
if (uppercaseFirstLetter)
|
||||||
{
|
{
|
||||||
return Sentence(wordCount, wordCount);
|
source = source.UppercaseFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Sentence(int wordCountMin, int wordCountMax)
|
return source;
|
||||||
{
|
}
|
||||||
return string.Format("{0}.", Words(wordCountMin, wordCountMax, true, true)).Replace(",.", ".").Remove("..");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Paragraph(int wordCount, int sentenceCount)
|
public static string Sentence(int wordCount)
|
||||||
{
|
{
|
||||||
return Paragraph(wordCount, wordCount, sentenceCount, sentenceCount);
|
return Sentence(wordCount, wordCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCount)
|
public static string Sentence(int wordCountMin, int wordCountMax)
|
||||||
{
|
{
|
||||||
return Paragraph(wordCountMin, wordCountMax, sentenceCount, sentenceCount);
|
return string.Format("{0}.", Words(wordCountMin, wordCountMax, true, true)).Replace(",.", ".").Remove("..");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax)
|
public static string Paragraph(int wordCount, int sentenceCount)
|
||||||
{
|
{
|
||||||
var source = string.Join(" ", Enumerable.Range(0, RandomHelper.Instance.Next(sentenceCountMin, sentenceCountMax)).Select(x => Sentence(wordCountMin, wordCountMax)));
|
return Paragraph(wordCount, wordCount, sentenceCount, sentenceCount);
|
||||||
|
}
|
||||||
|
|
||||||
//remove traililng space
|
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCount)
|
||||||
return source.Remove(source.Length - 1);
|
{
|
||||||
}
|
return Paragraph(wordCountMin, wordCountMax, sentenceCount, sentenceCount);
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<string> Paragraphs(int wordCount, int sentenceCount, int paragraphCount)
|
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax)
|
||||||
{
|
{
|
||||||
return Paragraphs(wordCount, wordCount, sentenceCount, sentenceCount, paragraphCount, paragraphCount);
|
var source = string.Join(" ", Enumerable.Range(0, RandomHelper.Instance.Next(sentenceCountMin, sentenceCountMax)).Select(x => Sentence(wordCountMin, wordCountMax)));
|
||||||
}
|
|
||||||
|
|
||||||
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCount, int paragraphCount)
|
//remove traililng space
|
||||||
{
|
return source.Remove(source.Length - 1);
|
||||||
return Paragraphs(wordCountMin, wordCountMax, sentenceCount, sentenceCount, paragraphCount, paragraphCount);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCount)
|
public static IEnumerable<string> Paragraphs(int wordCount, int sentenceCount, int paragraphCount)
|
||||||
{
|
{
|
||||||
return Paragraphs(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax, paragraphCount, paragraphCount);
|
return Paragraphs(wordCount, wordCount, sentenceCount, sentenceCount, paragraphCount, paragraphCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCountMin, int paragraphCountMax)
|
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCount, int paragraphCount)
|
||||||
{
|
{
|
||||||
return Enumerable.Range(0, RandomHelper.Instance.Next(paragraphCountMin, paragraphCountMax)).Select(p => Paragraph(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax)).ToArray();
|
return Paragraphs(wordCountMin, wordCountMax, sentenceCount, sentenceCount, paragraphCount, paragraphCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCount)
|
||||||
|
{
|
||||||
|
return Paragraphs(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax, paragraphCount, paragraphCount);
|
||||||
|
}
|
||||||
|
|
||||||
#region Color
|
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCountMin, int paragraphCountMax)
|
||||||
|
{
|
||||||
|
return Enumerable.Range(0, RandomHelper.Instance.Next(paragraphCountMin, paragraphCountMax)).Select(p => Paragraph(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax)).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
/* http://stackoverflow.com/a/1054087/234132 */
|
#endregion
|
||||||
public static string HexNumber(int digits)
|
|
||||||
{
|
|
||||||
byte[] buffer = new byte[digits / 2];
|
|
||||||
RandomHelper.Instance.NextBytes(buffer);
|
|
||||||
string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray());
|
|
||||||
|
|
||||||
if (digits % 2 == 0)
|
#region Color
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result + RandomHelper.Instance.Next(16).ToString("X");
|
/* http://stackoverflow.com/a/1054087/234132 */
|
||||||
}
|
public static string HexNumber(int digits)
|
||||||
|
{
|
||||||
|
byte[] buffer = new byte[digits / 2];
|
||||||
|
RandomHelper.Instance.NextBytes(buffer);
|
||||||
|
string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray());
|
||||||
|
|
||||||
#endregion
|
if (digits % 2 == 0)
|
||||||
}
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result + RandomHelper.Instance.Next(16).ToString("X");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -2,3 +2,8 @@ C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\bin\Release\LoremNET.dll
|
||||||
C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\bin\Release\LoremNET.pdb
|
C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\bin\Release\LoremNET.pdb
|
||||||
C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Release\LoremNET.dll
|
C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Release\LoremNET.dll
|
||||||
C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Release\LoremNET.pdb
|
C:\AJ\Personal\GitHub\Lorem.NET\Lorem.NET\obj\Release\LoremNET.pdb
|
||||||
|
D:\Repositories\Lorem.NET\Lorem.NET\obj\Release\Lorem.NET.csprojResolveAssemblyReference.cache
|
||||||
|
D:\Repositories\Lorem.NET\Lorem.NET\obj\Release\LoremNET.dll
|
||||||
|
D:\Repositories\Lorem.NET\Lorem.NET\obj\Release\LoremNET.pdb
|
||||||
|
D:\Repositories\Lorem.NET\Lorem.NET\bin\Release\LoremNET.dll
|
||||||
|
D:\Repositories\Lorem.NET\Lorem.NET\bin\Release\LoremNET.pdb
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Add table
Reference in a new issue