Trăn lấy mẫu ngẫu nhiên

sample[] là một hàm sẵn có của mô-đun ngẫu nhiên trong Python trả về một danh sách độ dài cụ thể của các mục được chọn từ chuỗi i. e. danh sách, bộ, chuỗi hoặc bộ. Được sử dụng để lấy mẫu ngẫu nhiên mà không cần thay thế

cú pháp. ngẫu nhiên. mẫu [dãy, k]

Thông số
sự liên tiếp. Có thể là một danh sách, bộ dữ liệu, chuỗi hoặc bộ
k. Một giá trị Số nguyên, nó chỉ định độ dài của một mẫu

trả lại. độ dài k danh sách mới các phần tử được chọn từ chuỗi

Mã số 1. Thực hiện đơn giản hàm sample[]




# Python3 program to demonstrate

# the use of sample[] function .

 

# import random 

from randomimport

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
0

 

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
2

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
3
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
4
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
5
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
6
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
7
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
8
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
7
Traceback [most recent call last]:
  File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in 
    print[random.sample[list1, 5]]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample
    raise ValueError["Sample larger than population or is negative"]
ValueError: Sample larger than population or is negative
0
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
7
Traceback [most recent call last]:
  File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in 
    print[random.sample[list1, 5]]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample
    raise ValueError["Sample larger than population or is negative"]
ValueError: Sample larger than population or is negative
2
Traceback [most recent call last]:
  File "C:/Users/user/AppData/Local/Programs/Python/Python36/all_prgm/geeks_article/sample_method_article.py", line 8, in 
    print[random.sample[list1, 5]]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\random.py", line 317, in sample
    raise ValueError["Sample larger than population or is negative"]
ValueError: Sample larger than population or is negative
5

Một cái nhìn tổng quát về công việc với sự ngẫu nhiên trong Python, chỉ sử dụng các hàm thư viện chuẩn

Tạo số float ngẫu nhiên giữa 0. 0 và 1. 0

Hàm

>>> import random
>>> random.randint[1, 10]
10
>>> random.randint[1, 10]
3
>>> random.randint[1, 10]
7
8 return number float ngẫu nhiên trong nửa khoảng [0. 0, 1. 0]. Điều này có nghĩa là số ngẫu nhiên trả về sẽ luôn nhỏ hơn điểm đầu mút bên phải [1. 0]

>>> import random
>>> random.random[]
0.11981376476232541
>>> random.random[]
0.757859420322092
>>> random.random[]
0.7384012347073081

Tạo số nguyên ngẫu nhiên giữa x và y

Đây là cách tạo ra một số nguyên ngẫu nhiên giữa hai đầu mút trong Python với hàm

>>> import random
>>> random.randint[1, 10]
10
>>> random.randint[1, 10]
3
>>> random.randint[1, 10]
7
9. Cái này bao gồm cả khoảng [x, y]

>>> import random
>>> random.randint[1, 10]
10
>>> random.randint[1, 10]
3
>>> random.randint[1, 10]
7

Với hàm

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
0, bạn có thể loại trừ khoảng bên phải, tức là số được sinh ra nằm trong nửa khoảng [x, y] và luôn nhỏ hơn điểm đầu phải

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4

Tạo số float ngẫu nhiên giữa x và y

Nếu bạn cần tạo các số float ngẫu nhiên nằm trong khoảng [x, y] để xác định, bạn có thể sử dụng hàm

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
1

>>> import random
>>> random.uniform[1, 10]
7.850184644194309
>>> random.uniform[1, 10]
4.00388600011348
>>> random.uniform[1, 10]
6.888959882650279

Chọn một phần tử ngẫu nhiên từ danh sách

Để lấy một phần tử ngẫu nhiên từ một dãy không trống [danh sách hoặc bộ dữ liệu], bạn có thể sử dụng hàm

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
2

>>> import random
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> random.choice[items]
'five'
>>> random.choice[items]
'one'
>>> random.choice[items]
'four'

Nếu chuỗi trống,

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
3 sẽ bị ném ra

Ngẫu nhiên một danh sách các yếu tố

Bạn có thể ngẫu nhiên hóa một chuỗi sử dụng hàm

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
4. Hàm này sẽ chỉnh sửa trình tự đối tượng chỉnh sửa và ngẫu nhiên hóa thứ tự các phần tử

>>> import random
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> random.shuffle[items]
>>> items
['four', 'one', 'five', 'three', 'two']

Nếu bạn không muốn thay đổi trình tự gốc, bạn cần tạo bản sao trước, sau đó xáo trộn bản sao. Bạn có thể tạo ra các bản sao của các đối tượng Python với mô-đun

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
5

Chọn n mẫu ngẫu nhiên từ danh sách các phần tử

Để lấy một mẫu ngẫu nhiên bao gồm n phần tử từ một dãy, ta sử dụng hàm

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
6

>>> import random
>>> items = ['one', 'two', 'three', 'four', 'five']
>>> random.sample[items, 3]
['one', 'five', 'two']
>>> random.sample[items, 3]
['five', 'four', 'two']
>>> random.sample[items, 3]
['three', 'two', 'five']

Tạo số ngẫu nhiên an toàn bằng mật mã

Nếu bạn cần các số ngẫu nhiên đã được mã hóa vì mục đích bảo mật, hãy sử dụng

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
7 - lớp sử dụng bộ sinh số giả ngẫu nhiên mã hóa

Thể hiện của lớp

>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
8 cung cấp hầu hết các giao tác sinh ngẫu nhiên như trong mô-đun
>>> import random
>>> random.randrange[1, 10]
5
>>> random.randrange[1, 10]
3
>>> random.randrange[1, 10]
4
9

>>> import random
>>> rand_gen = random.SystemRandom[]

>>> rand_gen.random[]
0.6112441459034399

>>> rand_gen.randint[1, 10]
2

>>> rand_gen.randrange[1, 10]
5

>>> rand_gen.uniform[1, 10]
8.42357365980016

>>> rand_gen.choice['abcdefghijklmn']
'j'

>>> items = ['one', 'two', 'three', 'four', 'five']
>>> rand_gen.shuffle[items]
>>> items
['two', 'four', 'three', 'one', 'five']

>>> rand_gen.sample['abcdefghijklmn', 3]
['g', 'e', 'c']

Trăn 3. 6+ – Mô-đun
>>> import random
>>> random.uniform[1, 10]
7.850184644194309
>>> random.uniform[1, 10]
4.00388600011348
>>> random.uniform[1, 10]
6.888959882650279
0

Nếu bạn đang sử dụng Python 3 và mục đích của bạn là sinh các số ngẫu nhiên mã hóa, hãy thử mô-đun

>>> import random
>>> random.uniform[1, 10]
7.850184644194309
>>> random.uniform[1, 10]
4.00388600011348
>>> random.uniform[1, 10]
6.888959882650279
0. Mô-đun này có sẵn trong Python 3. 6

Chủ Đề