Casino_plus, an easy way to create uniform and non-uniform random distributions
Tags: cpp, python, randomdistributions
Published: 2021-06-03 19:19:26

Casino_plus is a **C++ library** for creating both uniform and non-uniform random distributions. The good thing about this library is, it has **python language bindings** as well, so you can use it in your python projects as well.

This library has a very easy to use interface for python programmers, also it is really fast.

In this article, I want to show you how to use python bindings of this library. So let's begin

## Installation
the installation process is fairly simple. here is the requirements:
* Visual C++ build tools 2017 or higher
* CMake version 3.8 or higher

here is the github repo:
{% github Amohammadi2/Casino_plus no-readme %}

first go ahead and clone the repository:
```
$ git clone https://github.com/Amohammadi2/Casino_plus.git
```

Then, `cd` into the project root directory, there you can find a setup.py file. (installation verified on python version 3.8)
```
$ python setup.py install
```

If you have the required tools installed, the installation will finish successfully.

## How to use it
you have to import the module before you can use it, write the following code in a python file:
```
import CasinoPlus
```

the module contains a class called `CasinoRandomGenerator` but due to constraints of C++ type system, there are some prefixes that you should use based on the data type you want to pass to the generator.
* s_ : for generators containing strings
* cs_ : for generators containing chars
* i_ : for generators containing ints
* f_ : for generators containing floats
* d_ : for generators containing doubles

for example you could use: `s_CasinoRandomGenerator` to create a generator that accepts string items

at the time being, only the types mentioned above are supported. We'll try to improve it and make it more general.

### adding items to the generator
to add items to the generator, you should use one of the methods `add_item` or `add_sequence`.

* `add_item(item: Item_Type, probability: int) -> None` : adds one item to generator with specified probability. for uniform distributions `probability` could be set to 1 for all the items but if you want to create non-uniform random distributions, you can change this value. For example if you want the item to appear more frequently, you can increase the probability. **probability cannot be a negative value**. 
* `add_sequence(items: List[Tuple[Item_Type, int]]) -> None` : can add multiple items at once. it is more optimal to use this function when you want to add multiple items to the generator.

notice that `Item_Type` is actually the data type that the generator is using for example if you're using `s_CasinoRandomGenerator` then Item_Type would be `str`.

### getting a random item out of generator
in order to get a random item, you should use `get_random_item` method. It has the following signature:
```
get_random_item() -> Item_Type
```

## example
here is a example program that uses all the functionalities of  CasinoPlus.

```
>>> from CasinoPlus import s_CasinoRandomGenerator as strgen
>>>
>>> rand = strgen()
>>> rand.add_item("Ashkan Mohammadi", 1)
>>> my_brothers = [["Arshia Mohammadi", 1], ["Ilia Mohammadi", 1]]
>>>
>>> rand.add_sequence(my_brothers)
>>> rand.get_random_item()
'Ilia Mohammadi'
>>> rand.get_random_item()
'Ashkan Mohammadi'
>>> rand.get_random_item()
'Arshia Mohammadi'
```