Skip to content

Commit 32ddc6b

Browse files
will-keenimgtec-admin
authored andcommitted
Merge pull request #9 from imaginationtech/rand_list_doc
Add documentation for random length lists
2 parents 6c8f73a + 9435bef commit 32ddc6b

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

docs/howto.rst

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ However, the following is *not* OK because the ``rand_mul_by_4`` function is not
419419
Random list variables
420420
_____________________
421421

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.
423423

424424
Here is an example of a randomized list variable.
425425

@@ -437,6 +437,48 @@ Here is an example of a randomized list variable.
437437
438438
See the section below on :ref:`List Constraints` to see how adding constraints to this kind of variable works.
439439

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
465+
# upon randomization.
466+
r.add_rand_var('rand_list', bits=8, rand_length='rand_length')
467+
r.randomize()
468+
print(r.get_results())
469+
# Prints:
470+
# {'rand_length': 6, 'rand_list': [194, 227, 107, 10, 66, 247]}
471+
472+
.. note::
473+
``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.
480+
481+
440482
Initial values
441483
______________
442484

0 commit comments

Comments
 (0)