context
.random_expression(params, length=6, identities="default")
¶Generate a random rational expression.
Arguments:
params
: string list operators of the generated expression, with associated densities (1 by default).length
: the maximum length (defaults to 6).identities
: the identities of the resulting expression.Supported operators:
\e
, \z
!
(prefix), {c}
, *
, w.
, .w
&
, &:
, :
, .
, <+
, %
, +
, {/}
, {\}
, {T}
See also:
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.
The default coefficient is 1, therefore in the following example "+
" is twice more likely to appear than ".
", and "*
" is twice less.
for _ in range(3):
display(ctx.random_expression('+=2, ., *=0.5'))
for _ in range(3):
display(ctx.random_expression('.=2, +=2, &=1, *=0.5', length=20, identities='none'))
Weighted expressions can be generated. Use the keys w.
and .w
to control the probability of left and right product by a weight. Use the key w
to pass parameters to the random weight generator.
qrand = vcsn.context('lal(xyz), z').random_expression
for _ in range(3):
display(qrand('+, w., w="min=-5, max=5"', length=10, identities='none'))
Note that because of the identities, some weights might escape the specified range.
for _ in range(5):
display(qrand('+, w., w="min=0, max=5"', length=20))