context.random_expression(specs,identities="default")

Generate a random rational expression.

Arguments:

  • specs: string list operators of the generated expression, with associated densities (1 by default). The maximum length can also be set with length (6 by default).
  • identities: the identities of the resulting expression

The list of every operators:

  • nullary: \\e, \\z
  • unary: ! (prefix), {c}, *
  • binary: &, &:, :, ., <+, %, +, {/}, {\\}, {T}

See also:

Example

In [1]:
import os
# This trick ensures that we always use the same random seed,
# hence running this documentation always gives the same result.
os.environ['VCSN_SEED'] = '1'
import vcsn
from IPython.display import display
ctx = vcsn.context('lal_char(abc), b')

Densities are expressed with Bernoulli distribution if the operator is the only one, and discrete distribution otherwise.

Here "+" is twice more likely to appear than ".", and "*" has a probability of 1/2.

In [2]:
for _ in range(3):
    display(ctx.random_expression('+=2,.=1,*=0.5'))
$\left(b \, c\right)^{*} \, a$
$b + c$
${{a}^{*}}^{*} \, c$
In [3]:
for _ in range(3):
    display(ctx.random_expression('.=2,+=2,&=1,*=0.5,length=20'))
$b \, c \, {a}^{*} \, a \, c \, \left(b + {c}^{*} + c \& {c}^{*} + \left(c \, c\right)^{*}\right)$
$\left(c + c \, a \, {a}^{*}\right) \, a \, \left({a}^{*} \& {a}^{*}\right)$
$\left({b}^{*} + {{b}^{*}}^{*} + {a}^{*} \& \left(a + {\left({b}^{*} \, b\right)^{*}}^{*}\right)\right) \& \left(a + c\right) \, a$