Generowanie losowych liczb całkowitych od 0 do 9
Jak mogę wygenerować losowe liczby całkowite od 0 do 9 (włącznie) w Pythonie?
I. e. 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
16 answers
Try:
from random import randint
print(randint(0, 9))
Więcej informacji: https://docs.python.org/3/library/random.html#random.randint
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-11 21:38:58
import random
print(random.randint(0,9))
random.randint(a, b)
Zwraca losową liczbę całkowitą N taką, że a
Docs: https://docs.python.org/3.1/library/random.html#random.randint
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-30 14:51:01
Spróbuj tego:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-22 12:55:18
from random import randint
x = [randint(0, 9) for p in range(0, 10)]
To generuje 10 pseudorandomowych liczb całkowitych w zakresie od 0 do 9 włącznie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-17 08:05:17
The secrets
Moduł jest nowy w Pythonie 3.6. To jest lepsze niż random
Moduł do zastosowań kryptograficznych lub zabezpieczających.
Aby losowo wydrukować liczbę całkowitą z zakresu 0-9:
from secrets import randbelow
print(randbelow(10))
Po szczegóły patrz PEP 506 .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-06 10:40:07
Try this through random.shuffle
>>> import random
>>> nums = [x for x in range(10)]
>>> random.shuffle(nums)
>>> nums
[6, 3, 5, 4, 0, 1, 2, 9, 8, 7]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-10 17:57:41
Wybierz rozmiar tablicy (w tym przykładzie wybrałem Rozmiar 20). A następnie użyj następującego:
import numpy as np
np.random.randint(10, size=(1, 20))
Możesz spodziewać się wyświetlenia wyniku w następującej postaci ( różne losowe liczby całkowite będą zwracane za każdym razem, gdy je uruchomisz; dlatego możesz oczekiwać, że liczby całkowite w tablicy wyjściowej będą się różnić od przykładu podanego poniżej ).
array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-02 06:35:10
W przypadku liczb ciągłych randint
lub randrange
są prawdopodobnie najlepszym wyborem, ale jeśli masz kilka różnych wartości w sekwencji (np.choice
:
>>> import random
>>> values = list(range(10))
>>> random.choice(values)
5
choice
działa również dla jednego elementu z nieciągłej próbki:
>>> values = [1, 2, 3, 5, 7, 10]
>>> random.choice(values)
7
Jeśli potrzebujesz go "kryptograficznie mocnego", jest też secrets.choice
w Pythonie 3.6 i nowszych:
>>> import secrets
>>> values = list(range(10))
>>> secrets.choice(values)
2
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-24 10:33:54
Jeśli chcesz użyć numpy, użyj następującego:
import numpy as np
print(np.random.randint(0,10))
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-03 01:39:14
Pierwotne pytanie zakłada generowanie wielu losowych liczb całkowitych.
Jak mogę wygenerować liczby całkowite od 0 do 9 (włącznie) w Pythonie?
Wiele odpowiedzi pokazuje jednak tylko, jak uzyskać jedną liczbę losową, np. random.randint
i random.choice
.
Wiele Losowych Liczb Całkowitych
Dla jasności, nadal można generować wiele liczb losowych za pomocą tych technik, po prostu iterację N
CZAS:
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]
Próbka losowych liczb całkowitych
Niektóre posty pokazują, jak natywnie generować Wiele losowych liczb całkowitych.1 Oto kilka opcji, które odnoszą się do domniemanego pytania:
random.sample
zwraca k
unikalne selekcje z populacji (bez zastępowania):2
random.sample(range(10), k=N)
# [4, 5, 1, 2, 3]
W Pythonie 3.6, random.choices
zwraca k
selekcje z populacji (z zamianą):
random.choices(range(10), k=N)
# [3, 2, 0, 8, 2]
Zobacz this related post using numpy.random.choice
.
1[2010-03-14 14: 37]
2@prashanth wspomina o tym module pokazując jedną liczbę całkowitą.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-16 04:37:21
>>> import random
>>> random.randrange(10)
3
>>> random.randrange(10)
1
Aby uzyskać listę dziesięciu próbek:
>>> [random.randrange(10) for x in range(10)]
[9, 0, 4, 0, 5, 7, 4, 3, 6, 8]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-10 15:48:24
random.sample
jest inny, który może być użyty
import random
n = 1 # specify the no. of numbers
num = random.sample(range(10), n)
num[0] # is the required number
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-14 16:46:54
Najlepszym sposobem jest użycie funkcji import Random
import random
print(random.sample(range(10), 10))
Lub bez importu biblioteki:
n={}
for i in range(10):
n[i]=i
for p in range(10):
print(n.popitem()[1])
Tutaj popitems usuwa i zwraca dowolną wartość ze słownika n
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-03 01:41:12
Generowanie losowych liczb całkowitych z zakresu od 0 do 9.
import numpy
X = numpy.random.randint(0, 10, size=10)
print(X)
Wyjście:
[4 8 0 4 9 6 9 9 0 7]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-06 14:11:44
Użyłem zmiennej do kontrolowania zakresu
from random import randint
numberStartRange = 1
numberEndRange = 9
randomNumber = randint(numberStartRange, numberEndRange)
print(randomNumber)
Użyłem funkcji print, aby zobaczyć wyniki. Możesz komentować, jeśli tego nie potrzebujesz.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-24 21:12:33
Miałem więcej szczęścia z tym dla Pythona 3.6
str_Key = ""
str_RandomKey = ""
for int_I in range(128):
str_Key = random.choice('0123456789')
str_RandomKey = str_RandomKey + str_Key
Wystarczy dodać znaki takie jak 'ABCD' i 'abcd ' lub'^!~ = - >
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-20 23:43:43