Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/haath/chance.net

Native C# library for generating plausible and valid random data, from simple strings to human data and credit cards.
https://github.com/haath/chance.net

chance nuget random seed

Last synced: 6 days ago
JSON representation

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

Awesome Lists containing this project

README

        
















[Chance.js](http://chancejs.com) 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](https://www.nuget.org/packages/Chance.NET/)

## Usage

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

```csharp
Chance chance = new Chance();
```

Or provide a seed.

```csharp
Chance chance = new Chance(42);
```

Which can also be a string.

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

Due to entropy limitations in the underlying `System.Random`, new generators that are instantiated
during the same [tick](https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx)
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.

```csharp
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.

```csharp
Chance chance = new ConcurrentChance(seed);
```

## API

Almost every function that is implemented in [Chance.js](http://chancejs.com).
Apart from those, you will find new additions listed below.

### Locations

```csharp
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.

```csharp
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

```csharp
CreditCardType ccType = chance.CreditCardType();

ccType.Type; // MasterCard
ccType.Prefix; // "51"
ccType.SpacingPattern; // "4-4-4-4"
```

```csharp
CreditCard cc = chance.CreditCard();

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

### People

```csharp
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()` method.

```csharp
public class Book
{
[Year]
public int PublishingYear;

[Person]
public Person Author;

[String(10)]
public string Title;

[Paragraph(sentences: 6)]
public string Summary;
}
```

```csharp
Book book = chance.Object();
```

## Credits

- Original idea, concept and some of the original datasets are credited to [Victor Quinn](https://www.victorquinn.com/) and the rest of the contributors of [Chance.js](https://chancejs.com/).
- Math contributions from [Markos Karameris](https://www.linkedin.com/in/markos-karameris-298029160/).

---


Buy Me a Coffee at ko-fi.com