Skip to content

Native C# library for generating plausible and valid random data, from simple strings to human data and credit cards.

License

Notifications You must be signed in to change notification settings

haath/Chance.NET

Repository files navigation

Chance.js port into native C# targetting .NET Standard.

This project aims to mimic the original chance.js api as closely as possible while adapting it to C# types and practices. For example using enums for options and returning regular objects instead of json or strings.

Installation

Install from NuGet

Usage

Simply instantiate a new generator with a new time-dependant seed.

Chance chance = new Chance();

Or provide a seed.

Chance chance = new Chance(42);

Which can also be a string.

Chance chance = new Chance("string seed");

Due to entropy limitations in the underlying System.Random, new generators that are instantiated during the same tick will inherit the same seed from the system clock. For this reason, when creating multiple Chance instances together, it is recommended that you use a single instance as a generator.

Chance generator = new Chance();

for (...)
{
	Chance chance = generator.New();
}

The default Chance instance relies on the underlying System.Radom which is not thread-safe by default. To share the same generator between multiple threads use the ConcurrentChance class instead.

Chance chance = new ConcurrentChance(seed);

API

Almost every function that is implemented in Chance.js. Apart from those, you will find new additions listed below.

Locations

Location loc = chance.Location();

You can also generate random locations that are within a given range in meters from a central location. The generated point is also mathematically set to be uniformally distributed in the given area.

double centerLatitude  = 55.753765;
double centerLongitude = 37.620641;
double range           = 100;

// This point will be somewhere around the Red Square in Moscow
Location point = chance.Location(centerLatitude, centerLongitude, range);

Credit Cards

CreditCardType ccType = chance.CreditCardType();

ccType.Type;           // MasterCard
ccType.Prefix;         // "51"
ccType.SpacingPattern; // "4-4-4-4"
CreditCard cc = chance.CreditCard();

cc.Number;           // "4246023945944476"
cc.NumberFormatted;  // "4246 0239 4594 4476"
cc.ExpirationMonth;  // June
cc.ExpirationYear;   // 2020
cc.ExpirationString; // 06/20

People

Person p = chance.Person(gender: Gender.Male);

p.Gender;    // Male
p.FirstName; // John
p.SSN;       // "501940398"

Generating objects

You can generate objects of your own types by setting the appropriate attributes and using the Chance.Object<T>() method.

public class Book
{
	[Year]
	public int PublishingYear;
		
	[Person]
	public Person Author;

	[String(10)]
	public string Title;
	
	[Paragraph(sentences: 6)]
	public string Summary;
}
Book book = chance.Object<Book>();

Credits


Buy Me a Coffee at ko-fi.com

About

Native C# library for generating plausible and valid random data, from simple strings to human data and credit cards.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages