Hướng dẫn how does python random sample work? - mẫu ngẫu nhiên python hoạt động như thế nào?

Mẫu [] 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, tức là danh sách, tuple, chuỗi hoặc bộ. Được sử dụng để lấy mẫu ngẫu nhiên mà không cần thay thế. is an inbuilt function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement.

Cú pháp: ngẫu nhiên.sample [trình tự, k] random.sample[sequence, k]

Tham số: Trình tự: Có thể là một danh sách, tuple, chuỗi hoặc set.k: Giá trị số nguyên, nó chỉ định độ dài của mẫu.
sequence: Can be a list, tuple, string, or set.
k: An Integer value, it specify the length of a sample.

Trả về: K LEGN DANH SÁCH MỚI Các yếu tố được chọn từ chuỗi. k length new list of elements chosen from the sequence.

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

from random import

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']
1
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']
5
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']
5
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']
5
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
223

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
4
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
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
8
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
7

Output:

[2, 3, 5]

& nbsp; Mã số 2: Hàm sử dụng cơ bản của mẫu [].
Code #2: Basic use of sample[] function.

import

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
9

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
1
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']
5
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']
5
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']
5
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
22215
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
3
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
3

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
4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
7
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
8
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
8
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
7

>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
1
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
2
>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
3

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
4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6
>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
6
>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]
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
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
7

>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set.  For dicts, use list[d].
0
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
2
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set.  For dicts, use list[d].
3
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
5
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set.  For dicts, use list[d].
5
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
5
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set.  For dicts, use list[d].
7
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
5
>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set.  For dicts, use list[d].
9from0

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
4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6random 0random 1
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
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
7

random 4

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
2 random 6random 7
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
5random 9
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']

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
4
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
6import9
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
00
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
8
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
7

Output:

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

Lưu ý: Đầu ra sẽ khác nhau mọi lúc vì nó trả về một mục ngẫu nhiên. & NBSP; Mã số 3: Tăng ngoại lệOutput will be different everytime as it returns a random item.
 
Code #3: Raise Exception

Nếu cỡ mẫu, tức là K lớn hơn kích thước trình tự, giá trịerror sẽ được nâng lên.

import

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
9

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
1
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']
5
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']
5
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']
5
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
22215
>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]
3
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
3

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
4
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
17
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
With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
19

Output:

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

Theo tài liệu:

ngẫu nhiên.sample [dân số, k]

Trả về một danh sách chiều dài k của các yếu tố duy nhất được chọn từ chuỗi dân số. Được sử dụng để lấy mẫu ngẫu nhiên mà không cần thay thế.

Về cơ bản, nó chọn k các yếu tố ngẫu nhiên duy nhất, một mẫu, từ một chuỗi:

>>> import random
>>> c = list[range[0, 15]]
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> random.sample[c, 5]
[9, 2, 3, 14, 11]

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
20 cũng hoạt động trực tiếp từ một phạm vi:

>>> c = range[0, 15]
>>> c
range[0, 15]
>>> random.sample[c, 5]
[12, 3, 6, 14, 10]

Ngoài các chuỗi,

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
20 cũng hoạt động với các bộ:

>>> c = {1, 2, 4}
>>> random.sample[c, 2]
[4, 1]

Tuy nhiên,

With list: [3, 1, 2]
With string: ['e', 'f', 'G', 'G']
With tuple: ['ankit', 'portal', 'geeks', 'computer']
With set: ['b', 'd', 'c']
20 không hoạt động với các trình lặp tùy ý:

>>> c = [1, 3]
>>> random.sample[iter[c], 5]
TypeError: Population must be a sequence or set.  For dicts, use list[d].

Làm thế nào để Python tạo ra dữ liệu ngẫu nhiên?

Sử dụng chức năng ngẫu nhiên.rand [d0, d1, sth, dn] để tạo ra một mảng n chiều của các số float ngẫu nhiên trong phạm vi [0,0, 1.0].Sử dụng một ngẫu nhiên.đồng nhất [thấp = 0,0, cao = 1,0, kích thước = không có] hàm để tạo ra một mảng N chiều của các số nổi ngẫu nhiên trong phạm vi [thấp, cao]. rand[d0, d1, …, dn] function to generate an n-dimensional array of random float numbers in the range of [0.0, 1.0] . Use a random. uniform[low=0.0, high=1.0, size=None] function to generate an n-dimensional array of random float numbers in the range of [low, high] .

Làm thế nào để gói ngẫu nhiên hoạt động trong Python?

Mô-đun ngẫu nhiên Python là một mô-đun Python được xây dựng được sử dụng để tạo ra các số ngẫu nhiên.Đây là những số giả ngẫu nhiên có nghĩa là đây không thực sự ngẫu nhiên.Mô -đun này có thể được sử dụng để thực hiện các hành động ngẫu nhiên như tạo số ngẫu nhiên, in ngẫu nhiên một giá trị cho danh sách hoặc chuỗi, v.v.used to generate random numbers. These are pseudo-random numbers means these are not truly random. This module can be used to perform random actions such as generating random numbers, print random a value for a list or string, etc.

Là ngẫu nhiên trong Python thực sự ngẫu nhiên?

Hầu hết dữ liệu ngẫu nhiên được tạo ra với Python không hoàn toàn ngẫu nhiên theo nghĩa khoa học của từ này.Thay vào đó, nó là giả: được tạo bằng trình tạo số giả [PRNG], về cơ bản là bất kỳ thuật toán nào để tạo dữ liệu dường như ngẫu nhiên nhưng vẫn có thể tái tạo.. Rather, it is pseudorandom: generated with a pseudorandom number generator [PRNG], which is essentially any algorithm for generating seemingly random but still reproducible data.

Bài Viết Liên Quan

Chủ Đề