Math Functions#

sub - Subtraction#

Calculates the difference of two scalar arguments.

import ison

dicData = {
    "result": "$sub{3, 2}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 1
}

sum - Summation#

Calculates the sum of all scalar arguments.

import ison

dicData = {
    "result": "$sum{1, 2, 3}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 6
}

To sum all elements of a list, you need to unroll the list.

import ison

dicData = {
    "__locals__": {
        "lA": [1, 2, 3]
    },
    "result": "$sum{*$lA}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 6
}

prod - Product#

Calculates the product of all scalar arguments.

import ison

dicData = {
    "__locals__": {
        "lA": [1, 2, 3, 4, 5]
    },
    "result": "$prod{*$lA}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 120
}

mod - Modulo#

Calculates the modulo of two scalar arguments.

import ison

dicData = {
    "result": "$mod{9, 7}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 2
}

rand.choice - Select Randomly#

Randomly selects an element from a list or a dictionary.

import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",

        "lA": [1, 2, 3, 4, 5],
        "dicA": {
            "a": { "x": 1 },
            "b": { "x": 2 }
        }
    },
    
    "result 1": "$rand.choice{$rndX, $lA}",
    "result 2": "$rand.choice{$rndX, $dicA}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": 4,
    "result 2": {
        "x": 1
    }
}

rand.generator - Create a random number generator instance#

This creates a random number generator internally, base on the given seed. The result of this function is a string, which can be used by other rand.* functions to reference this random number generator. The generator instance is unique to the parser class instance. If an already generated reference string is used in a new parser instance, the generator is re-created from the same seed.

Use this function to ensure that the random values are deterministically dependent on the seed you give. This may not be the case if you are using a system wide random number generator, which may be influenced by external calls. Referencing a generator also has the advantage that the process order when parsing the ISON code does not have an effect on the outcome.

Every rand.* function accepts as first, optional argument a random generator reference. If no random number generator reference is given, a default random number generator is used. However, note that the order of processing the random number functions will have an influence on the random values.

import ison

dicData = {
    "__locals__": {
        # Create two random number generators using strings as seeds.
        # In this example we use the same seed for both generators.
        # The generators will produce pseudo random values in th same order
        # but do not influence each other.
        "rndA": "$rand.generator{gen 1}",
        "rndB": "$rand.generator{gen 1}",
    },
    
    "result 1": "$rand.uniform{$rndA, 0, 1}",
    "result 2": "$rand.uniform{$rndB, 0, 1}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": 0.9298913845803418,
    "result 2": 0.9298913845803418
}

rand.int - Get Uniform Integer Random Number#

Returns an integer random number uniformly distributed in the range given by the two arguments. The minimum and maximum values are both included in the range.

import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{1}",
    },
    
    "result": "$rand.int{$rndX, 3, 10}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 5
}

rand.sample - Sample from Set#

Randomly sample a given number of elements from a list or dictionary. Parameters:

  1. (list|dict): list or dictionary from which to sample

  2. (int): number of samples to draw

  3. (bool): Flag whether to sample uniquely from source or not. Default is true.

import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",

        "lA": [1, 2, 3, 4, 5],
        "dicA": {
            "a": { "x": 1 },
            "b": { "x": 2 }
        }
    },
    
    "result 1": "$rand.sample{$rndX, $lA, 4}",
    "result 2": "$rand.sample{$rndX, $lA, 4, false}",
    "result 3": "$rand.sample{$rndX, $lA, 10, false}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": [
        2,
        5,
        1,
        4
    ],
    "result 2": [
        4,
        5,
        1,
        5
    ],
    "result 3": [
        2,
        1,
        4,
        4,
        4,
        4,
        1,
        5,
        2,
        3
    ]
}
import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",

        "dicA": {
            "a": { "x": 1 },
            "b": { "x": 2 },
            "c": { "x": 3 },
            "d": { "x": 4 }
        }
    },
    
    "result 1": "$rand.sample{$rndX, $dicA, 3}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": {
        "b": {
            "x": 2
        },
        "a": {
            "x": 1
        },
        "c": {
            "x": 3
        }
    }
}

rand.sample_range - Sample from Integer Range#

Randomly sample a given number of elements from an integer range. Parameters:

  1. (int): minimal integer value of range

  2. (int): maximal integer value of range

  3. (int): number of samples to draw

  4. (optional, bool): unique=[true|false]. Flag whether to sample uniquely from source or not. Default is true.

  5. (optional, bool): consec-differ=[true|false]. Flag whether consecutive values should differ (true) or not (false). Default is false. If the fourth parameter is true, this value is ignored.

import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",
    },
    
    "result 1": "$rand.sample_range{$rndX, 1, 4, 6, unique=false}",
    "result 2": "$rand.sample_range{$rndX, 1, 4, 6, unique=false, consec-differ=true}",
    "result 3": "$rand.sample_range{$rndX, 3, 12, 5}",
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result 1": [
        2,
        2,
        1,
        4,
        4,
        1
    ],
    "result 2": [
        2,
        1,
        4,
        1,
        2,
        3
    ],
    "result 3": [
        5,
        7,
        9,
        10,
        12
    ]
}

rand.seed - Set Random Seed#

Sets the random seed for the following random value operations.

Note that the this function also seed the global random number generator, for backward compatibility.

import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",
        # Seeding a random generator again is not necessary but possible.
        "do_seed": "$rand.seed{$rndX, 3}",
    },
    
    "confirm_exec": "$do_seed",
    "result": "$rand.uniform{3, 6}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "confirm_exec": ":eval:rand.seed:[':eval:rand.generator(3054121613703434466):[3054121613703434466]', '3']",
    "result": 5.533265554575144
}

rand.uniform - Get Uniform Random Number#

Returns a random number uniformly distributed in the range given by the two arguments.

import ison

dicData = {
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",
    },
    
    "result": "$rand.uniform{$rndX, 3, 6}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": 5.948865008965477
}

rand.zwicky - Create Instance of Zwicky Box#

Return a random instance of a Zwicky box.

import ison

dicData = {
    "__func_locals__": {
        # Define the zwicky box in __func_locals__ as lambda function, so that the
        # element 'fValue' is only parsed, when the box is used.
        # Pass in the random number generator reference as first lambda parameter.
        "box": {
            "__lambda__": {},
            "sA": [ "a", "b", "c", "d", "e"],
            "fValue": "$rand.uniform{%0, 1, 2}",
            "dicB": {
                "iX": [1, 2, 3, 4],
                "lY": [[1, 2], [3, 4]]
            }
        }

    },
    "__locals__": {
        # Create a random generator instance
        "rndX": "$rand.generator{2}",

    },
    
    "result": "$rand.zwicky{$rndX, ${box, $rndX}}"
}

dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
    "result": {
        "sA": "c",
        "fValue": 1.7868504406687737,
        "dicB": {
            "iX": 4,
            "lY": [
                3,
                4
            ]
        }
    }
}