def rtensor(tensorclass, n_qnums, rqhape, rshape, rdirs, rcharge):
    """Return a function that generates a random tensor of the given
    tensorclass, using the given fixtures for generating form data.
    """

    def _rtensor(
        shape=None,
        qhape=None,
        n=None,
        chi=None,
        nlow=0,
        nhigh=5,
        chilow=0,
        chihigh=6,
        charge=None,
        dirs=None,
        cmplx=True,
        **kwargs
    ):
        """Return a random tensor of the given form data.

        Full form data (`shape`, `qhape`, `dirs`, `charge`) can be provided, in
        which case only the elements of the tensor are random. Alternatively,
        random form data can also be generated. In that case, `n` is the number
        of indices (alternatively a random number between `nlow` and `nhigh`)
        and `chi` is the bond dimension of all sectors (alternatively random
        numbers between `chilow` and `chihigh`).

        Partial form data can also be given. For instance, if one only gives
        the `shape` argument, random `qhape`, `dirs`, and `charge` are
        generated that match it.

        `cmplx` sets whether the tensor should be complex instead of
        real, and is by default True.
        """
        if shape is None:
            shape = rshape(
                n=n,
                chi=chi,
                nlow=nlow,
                nhigh=nhigh,
                chilow=chilow,
                chihigh=chihigh,
            )
        if qhape is None:
            qhape = rqhape(shape)
        if dirs is None:
            dirs = rdirs(shape=shape)
        elif dirs == 1:
            dirs = [1] * len(shape)
        if charge is None:
            charge = rcharge()

        real = tensorclass.random(
            shape, qhape=qhape, dirs=dirs, charge=charge, **kwargs
        )
        if cmplx:
            imag = tensorclass.random(
                shape, qhape=qhape, dirs=dirs, charge=charge, **kwargs
            )
            res = real + 1j * imag
        else:
            res = real
        return res

    return _rtensor