Casino_plus, an easy way to create uniform and non-uniform random distributions

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…


This content originally appeared on DEV Community and was authored by Amohammadi2

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 logo Amohammadi2 / Casino_plus

create uniform and non-uniform distribution

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'


This content originally appeared on DEV Community and was authored by Amohammadi2


Print Share Comment Cite Upload Translate Updates
APA

Amohammadi2 | Sciencx (2021-06-03T19:19:26+00:00) Casino_plus, an easy way to create uniform and non-uniform random distributions. Retrieved from https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/

MLA
" » Casino_plus, an easy way to create uniform and non-uniform random distributions." Amohammadi2 | Sciencx - Thursday June 3, 2021, https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/
HARVARD
Amohammadi2 | Sciencx Thursday June 3, 2021 » Casino_plus, an easy way to create uniform and non-uniform random distributions., viewed ,<https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/>
VANCOUVER
Amohammadi2 | Sciencx - » Casino_plus, an easy way to create uniform and non-uniform random distributions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/
CHICAGO
" » Casino_plus, an easy way to create uniform and non-uniform random distributions." Amohammadi2 | Sciencx - Accessed . https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/
IEEE
" » Casino_plus, an easy way to create uniform and non-uniform random distributions." Amohammadi2 | Sciencx [Online]. Available: https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/. [Accessed: ]
rf:citation
» Casino_plus, an easy way to create uniform and non-uniform random distributions | Amohammadi2 | Sciencx | https://www.scien.cx/2021/06/03/casino_plus-an-easy-way-to-create-uniform-and-non-uniform-random-distributions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.