mirror of
https://github.com/tonytins/Lorem.NET.git
synced 2025-03-15 04:32:23 +00:00
commit
e5d1fac607
8 changed files with 130 additions and 111 deletions
|
@ -4,146 +4,156 @@ using System.Linq;
|
|||
|
||||
namespace LoremNET
|
||||
{
|
||||
public partial class Lorem
|
||||
{
|
||||
public static bool Chance(int successes, int attempts)
|
||||
{
|
||||
var number = Number(1, attempts);
|
||||
public partial class Lorem
|
||||
{
|
||||
public static bool Chance(int successes, int attempts)
|
||||
{
|
||||
var number = Number(1, attempts);
|
||||
|
||||
return number <= successes;
|
||||
}
|
||||
return number <= successes;
|
||||
}
|
||||
|
||||
public static T Random<T>(T[] items)
|
||||
{
|
||||
var index = RandomHelper.Instance.Next(items.Length);
|
||||
public static T Random<T>(T[] items)
|
||||
{
|
||||
var index = RandomHelper.Instance.Next(items.Length);
|
||||
|
||||
return items[index];
|
||||
}
|
||||
return items[index];
|
||||
}
|
||||
|
||||
/* 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);
|
||||
public static TEnum RandomEnum<TEnum>()
|
||||
{
|
||||
if (typeof(TEnum).IsEnum)
|
||||
{
|
||||
var v = Enum.GetValues(typeof(TEnum));
|
||||
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)
|
||||
{
|
||||
return DateTime(new System.DateTime(startYear, startMonth, startDay), System.DateTime.Now);
|
||||
}
|
||||
#region DateTime
|
||||
|
||||
public static DateTime DateTime(DateTime min)
|
||||
{
|
||||
return DateTime(min, System.DateTime.Now);
|
||||
}
|
||||
public static DateTime DateTime(int startYear = 1950, int startMonth = 1, int startDay = 1)
|
||||
{
|
||||
return DateTime(new System.DateTime(startYear, startMonth, startDay), System.DateTime.Now);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
|
||||
return min + newSpan;
|
||||
}
|
||||
public static DateTime DateTime(DateTime min)
|
||||
{
|
||||
return DateTime(min, System.DateTime.Now);
|
||||
}
|
||||
|
||||
#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()
|
||||
{
|
||||
return string.Format("{0}@{1}.com", Lorem.Words(1, false), Lorem.Words(1, false));
|
||||
}
|
||||
#endregion
|
||||
|
||||
public static string Words(int wordCount, bool uppercaseFirstLetter = true, bool includePunctuation = false)
|
||||
{
|
||||
return Words(wordCount, wordCount, uppercaseFirstLetter, includePunctuation);
|
||||
}
|
||||
#region Text
|
||||
|
||||
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 Email()
|
||||
{
|
||||
return string.Format("{0}@{1}.com", Lorem.Words(1, false), Lorem.Words(1, false));
|
||||
}
|
||||
|
||||
if (uppercaseFirstLetter)
|
||||
{
|
||||
source = source.UppercaseFirst();
|
||||
}
|
||||
public static string Words(int wordCount, bool uppercaseFirstLetter = true, bool includePunctuation = false)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return Sentence(wordCount, wordCount);
|
||||
}
|
||||
if (uppercaseFirstLetter)
|
||||
{
|
||||
source = source.UppercaseFirst();
|
||||
}
|
||||
|
||||
public static string Sentence(int wordCountMin, int wordCountMax)
|
||||
{
|
||||
return string.Format("{0}.", Words(wordCountMin, wordCountMax, true, true)).Replace(",.", ".").Remove("..");
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
public static string Paragraph(int wordCount, int sentenceCount)
|
||||
{
|
||||
return Paragraph(wordCount, wordCount, sentenceCount, sentenceCount);
|
||||
}
|
||||
public static string Sentence(int wordCount)
|
||||
{
|
||||
return Sentence(wordCount, wordCount);
|
||||
}
|
||||
|
||||
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCount)
|
||||
{
|
||||
return Paragraph(wordCountMin, wordCountMax, sentenceCount, sentenceCount);
|
||||
}
|
||||
public static string Sentence(int wordCountMin, int wordCountMax)
|
||||
{
|
||||
return string.Format("{0}.", Words(wordCountMin, wordCountMax, true, true)).Replace(",.", ".").Remove("..");
|
||||
}
|
||||
|
||||
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax)
|
||||
{
|
||||
var source = string.Join(" ", Enumerable.Range(0, RandomHelper.Instance.Next(sentenceCountMin, sentenceCountMax)).Select(x => Sentence(wordCountMin, wordCountMax)));
|
||||
public static string Paragraph(int wordCount, int sentenceCount)
|
||||
{
|
||||
return Paragraph(wordCount, wordCount, sentenceCount, sentenceCount);
|
||||
}
|
||||
|
||||
//remove traililng space
|
||||
return source.Remove(source.Length - 1);
|
||||
}
|
||||
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCount)
|
||||
{
|
||||
return Paragraph(wordCountMin, wordCountMax, sentenceCount, sentenceCount);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> Paragraphs(int wordCount, int sentenceCount, int paragraphCount)
|
||||
{
|
||||
return Paragraphs(wordCount, wordCount, sentenceCount, sentenceCount, paragraphCount, paragraphCount);
|
||||
}
|
||||
public static string Paragraph(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return Paragraphs(wordCountMin, wordCountMax, sentenceCount, sentenceCount, paragraphCount, paragraphCount);
|
||||
}
|
||||
//remove traililng space
|
||||
return source.Remove(source.Length - 1);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCountMin, int sentenceCountMax, int paragraphCount)
|
||||
{
|
||||
return Paragraphs(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax, paragraphCount, paragraphCount);
|
||||
}
|
||||
public static IEnumerable<string> Paragraphs(int wordCount, int sentenceCount, int 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)
|
||||
{
|
||||
return Enumerable.Range(0, RandomHelper.Instance.Next(paragraphCountMin, paragraphCountMax)).Select(p => Paragraph(wordCountMin, wordCountMax, sentenceCountMin, sentenceCountMax)).ToArray();
|
||||
}
|
||||
public static IEnumerable<string> Paragraphs(int wordCountMin, int wordCountMax, int sentenceCount, int paragraphCount)
|
||||
{
|
||||
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 */
|
||||
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;
|
||||
}
|
||||
#region Color
|
||||
|
||||
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\obj\Release\LoremNET.dll
|
||||
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.
|
@ -49,6 +49,10 @@ bool isTruth = LoremNET.Lorem.Chance(55, 100);
|
|||
// this will randomly select an item from the group:
|
||||
string[] numbers = GetNumbers();
|
||||
string number = LoremNET.Lorem.Random(numbers);
|
||||
|
||||
// this will randomly select a value from the enum:
|
||||
MyEnum value = LoremNET.Lorem.RandomEnum<MyEnum>();
|
||||
|
||||
```
|
||||
|
||||
### RandomHelper
|
||||
|
@ -74,4 +78,4 @@ namespace LoremNET
|
|||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
|
Loading…
Add table
Reference in a new issue