-6

我知道这对某人来说是一个简单的问题,但我在这里迷路了。

假设我希望我的结果是数组值中的 10 个随机数:

np.random.randint(0,2)  # returns 0,1

我想迭代 10 次:

for i=1 to 10 : 
   np.random.randint(0,2) addto myarray

0,1,1,0...我想要一个10 次的 numpy 数组

谢谢你。

4

1 回答 1

7

阅读文档总是一个好主意。如果您键入help(np.random.randint),您会看到:

randint(...)
    randint(low, high=None, size=None)

    Return random integers from `low` (inclusive) to `high` (exclusive).

    Return random integers from the "discrete uniform" distribution in the
    "half-open" interval [`low`, `high`). If `high` is None (the default),
    then results are from [0, `low`).

    Parameters
    ----------
    low : int
        Lowest (signed) integer to be drawn from the distribution (unless
        ``high=None``, in which case this parameter is the *highest* such
        integer).
    high : int, optional
        If provided, one above the largest (signed) integer to be drawn
        from the distribution (see above for behavior if ``high=None``).
    size : int or tuple of ints, optional
        Output shape. Default is None, in which case a single int is
        returned.

第一个例子是:

Examples
--------
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
于 2013-10-02T19:55:05.567 回答