def rand_ktensor(shape, rank, norm=None, random_state=None):
    """
    Generates a random N-way tensor with rank R, where the entries are
    drawn from the standard uniform distribution in the interval [0.0,1].

    Parameters
    ----------
    shape : tuple
        shape of the tensor

    rank : integer
        rank of the tensor

    norm : float or None, optional (defaults: None)
        If not None, the factor matrices are rescaled so that the Frobenius
        norm of the returned tensor is equal to ``norm``.

    ktensor : bool
        If true, a KTensor object is returned, i.e., the components are in factored
        form ``[U_1, U_2, ... U_N]``; Otherwise an N-way array is returned.

    random_state : integer, RandomState instance or None, optional (default ``None``)
        If integer, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used by np.random.


    Returns
    -------
    X : (I_1, ..., I_N) array_like
        N-way tensor with rank R.

    Example
    -------
    >>> # Create a rank-2 tensor of dimension 5x5x5:
    >>> import tensortools as tt
    >>> X = tt.randn_tensor((5,5,5), rank=2)

    """

    # Check input.
    rns = _check_random_state(random_state)

    # Randomize low-rank factor matrices i.i.d. uniform random elements.
    factors = KTensor([rns.uniform(0.0, 1.0, size=(i, rank)) for i in shape])
    return _rescale_tensor(factors, norm)
Generates a random N-way tensor with rank R, where the entries are
    drawn from the standard uniform distribution in the interval [0.0,1].

    Parameters
    ----------
    shape : tuple
        shape of the tensor

    rank : integer
        rank of the tensor

    norm : float or None, optional (defaults: None)
        If not None, the factor matrices are rescaled so that the Frobenius
        norm of the returned tensor is equal to ``norm``.

    ktensor : bool
        If true, a KTensor object is returned, i.e., the components are in factored
        form ``[U_1, U_2, ... U_N]``; Otherwise an N-way array is returned.

    random_state : integer, RandomState instance or None, optional (default ``None``)
        If integer, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used by np.random.


    Returns
    -------
    X : (I_1, ..., I_N) array_like
        N-way tensor with rank R.

    Example
    -------
    >>> # Create a rank-2 tensor of dimension 5x5x5:
    >>> import tensortools as tt
    >>> X = tt.randn_tensor((5,5,5), rank=2)