You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/howto.rst
+43-1Lines changed: 43 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -419,7 +419,7 @@ However, the following is *not* OK because the ``rand_mul_by_4`` function is not
419
419
Random list variables
420
420
_____________________
421
421
422
-
Sometimes, we might want to produce a list of randomized values. ``constrainedrandom`` supports this. You can turn a random variable into a list by supplying the ``length`` argument. ``length=0`` is the default behaviour, i.e. a scalar value. ``length=1`` means a list of one randomized value, similarly ``length=N`` means a list of N randomized values.
422
+
Sometimes, we might want to produce a list of randomized values. ``constrainedrandom`` supports this. You can turn a random variable into a list by supplying the ``length`` argument. ``length=None`` is the default behaviour, i.e. a scalar value. ``length=1`` means a list of one randomized value, similarly ``length=N`` means a list of N randomized values. ``length=0`` is a list of length zero, i.e. the empty list, ``[]``. This is permitted for completeness' sake.
423
423
424
424
Here is an example of a randomized list variable.
425
425
@@ -437,6 +437,48 @@ Here is an example of a randomized list variable.
437
437
438
438
See the section below on :ref:`List Constraints` to see how adding constraints to this kind of variable works.
439
439
440
+
Random list with random length
441
+
______________________________
442
+
443
+
If the user desires the length of a random list to be randomized, this can also be achieved with ``constrainedrandom``.
444
+
445
+
Firstly, add a random variable that will be used to determine the length of the random list.
446
+
447
+
Secondly, add the random list variable, using the ``rand_length`` argument. Specify the name of the variable that determines the random length which was previously added.
448
+
449
+
E.g. this code adds a variable and then uses it as a random length:
450
+
451
+
452
+
.. code-block:: python
453
+
454
+
import random
455
+
456
+
from constrainedrandom import RandObj
457
+
458
+
random.seed(0)
459
+
r = RandObj()
460
+
461
+
# Add varaible to use as random length, between 0 and 9
462
+
r.add_rand_var('rand_length', domain=range(10))
463
+
# Use this variable as the rand_length argument to a list,
464
+
# meaning the list will have length equal to rand_length
``length`` and ``rand_length`` are mutually-exclusive arguments - the length is either fixed or random.
474
+
475
+
.. warning::
476
+
Be careful of using a variable for ``rand_length`` which might give an illegal list length. List lengths are illegal if they are not an instance of ``int``, or if they are less than zero. E.g. using a variable for ``rand_length`` whose domain includes negative numbers might result in errors.
477
+
478
+
.. note::
479
+
Using ``rand_length`` and ``order`` together may mean that ``order`` is ignored. The library will ensure that a variable used as a random length will always be randomized prior to the list that relies on it. If the user tries to specify an order that defies this principle, it will be ignored with no error or warning message.
0 commit comments