Generate random integers between 0 and 9

2019-01-01 11:57发布

How can I generate random integers between 0 and 9 (inclusive) in Python?

For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

21条回答
后来的你喜欢了谁
2楼-- · 2019-01-01 12:05

Try This,

import numpy as np

X = np.random.randint(0, 99, size=1000) # 1k random integer
查看更多
柔情千种
3楼-- · 2019-01-01 12:06

Try:

from random import randint
print(randint(0, 9))

More info: https://docs.python.org/3/library/random.html#random.randint

查看更多
倾城一夜雪
4楼-- · 2019-01-01 12:06

You can try this:

import numpy as np
print ( np.random.uniform(low=0, high=10, size=(15,)) ).astype(int)

>>> [8 3 6 9 1 0 3 6 3 3 1 2 4 0 4]

Notes:

1.> np.random.uniform generates uniformly distributed numbers over the half-open interval [low, high).

2.> astype(int) casts the numpy array to int data type.

3.> I have chosen size = (15,). This will give you a numpy array of length = 15.

More information on numpy.random.uniform

More information on numpy.ndarray.astype

查看更多
看风景的人
5楼-- · 2019-01-01 12:09

The original question implies generating multiple random integers.

How can I generate integers between 0 and 9 (inclusive) in Python?

Many responses however only show how to get one random number, e.g. random.randint and random.choice.

Multiple Random Integers

For clarity, you can still generate multiple random numbers using those techniques by simply iterating N times:

import random


N = 5

[random.randint(0, 9) for _ in range(N)]
# [9, 7, 0, 7, 3]

[random.choice(range(10)) for _ in range(N)]
# [8, 3, 6, 8, 7]

Sample of Random Integers

Some posts demonstrate how to natively generate multiple random integers.1 Here are some options that address the implied question:

random.sample returns k unique selections from a population (without replacement):2

random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]

In Python 3.6, random.choices returns k selections from a population (with replacement):

random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]

See also this related post using numpy.random.choice.

1Namely @John Lawrence Aspden, @S T Mohammed, @SiddTheKid, @user14372, @zangw, et al.

2@prashanth mentions this module showing one integer.

查看更多
零度萤火
6楼-- · 2019-01-01 12:13

The secrets module is new in Python 3.6. This is better than the random module for cryptography or security uses.

To randomly print an integer in the inclusive range 0-9:

from secrets import randbelow
print(randbelow(10))

For details, see PEP 506.

查看更多
栀子花@的思念
7楼-- · 2019-01-01 12:13

From the documentation page for the random module:

Warning: The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

random.SystemRandom, which was introduced in Python 2.4, is considered cryptographically secure. It is still available in Python 3.7.1.

>>> import string
>>> string.digits
'0123456789'
>>> import random
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'1'
>>> random.SystemRandom().choice(string.digits)
'8'
>>> random.SystemRandom().choice(string.digits)
'5'

Instead of string.digits, range could be used per some of the other answers along perhaps with a comprehension. Mix and match according to your needs.

查看更多
登录 后发表回答