Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnuniform[]” which performs this task with ease and using just the one word. This method is defined in “random” module
Đồng nhất [] là một phương thức được chỉ định trong thư viện ngẫu nhiên trong Python 3.uniform[int x, int y]
Ngày nay, nói chung, các nhiệm vụ hàng ngày, ở đó, luôn luôn cần phải tạo ra các số ngẫu nhiên trong một phạm vi. Các cấu trúc lập trình thông thường yêu cầu một phương thức không chỉ là một từ để đạt được nhiệm vụ cụ thể này. Trong Python, có một phương thức sẵn có, đồng phục [], thực hiện nhiệm vụ này một cách dễ dàng và chỉ sử dụng một từ. Phương pháp này được định nghĩa trong mô -đun ngẫu nhiên "
x Specifies the lower limit of the random number required to generate.
y Specifies the upper limit of the random number required to generate.Cú pháp: đồng nhất [int x, int y] Returns the generated floating point random number between lower limit and upper limit
Tham số: x Chỉ định giới hạn thấp hơn của số ngẫu nhiên cần thiết để tạo. Chỉ định giới hạn trên của số ngẫu nhiên cần thiết để tạo.
Code #1 : Code to generate float random number.
Trả về: Trả về số ngẫu nhiên điểm nổi được tạo giữa giới hạn dưới và giới hạn trên
& nbsp; Mã số 1: Mã để tạo số ngẫu nhiên nổi.
import
random
The winner of game is : Player23
The winner of game is : Player24
The winner of game is : Player25
The winner of game is : Player26
=
The winner of game is : Player28
The winner of game is : Player23
uniform[x, y]0
Output:
The random number generated between 4 and 9 is : 7.494931618830411
a
=
4
There are many possible applications that can be thought of this function, some of the notable being generating random numbers in casino games, for lottery or custom games.
Below is the game that decided the winner on the basis of closeness to a certain value.
The winner of game is : Player20
=
The winner of game is : Player22
Code #2 : Application of uniform[] – A Game
Ứng dụng: Có nhiều ứng dụng có thể được nghĩ đến chức năng này, một số đáng chú ý là tạo ra các số ngẫu nhiên trong các trò chơi sòng bạc, cho các trò chơi xổ số hoặc tùy chỉnh. .
& nbsp; mã số 2: Ứng dụng đồng phục [] - một trò chơi
import
uniform[x, y]2
uniform[x, y]3
=
uniform[x, y]5
uniform[x, y]6
=
uniform[x, y]8
uniform[x, y]9
=
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]1
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]2
=
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]4
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]5
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]6__12
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]9
=
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691991
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691992
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691993
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691994
=
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691991
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691992
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691998
import
8
The winner of game is : Player23
The winner of game is : Player24
random
1The winner of game is : Player26
=
The winner of game is : Player28
import
8
The winner of game is : Player23
The winner of game is : Player24
random
8#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]8
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691999
=
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691991
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.53263691992
import
3import
8
The winner of game is : Player23
The winner of game is : Player24
random
1The winner of game is : Player26
=
The winner of game is : Player28
import
4import
55____56 import
7
import
4a
1import
6 a
3
import
8
The winner of game is : Player23
The winner of game is : Player24
random
1The winner of game is : Player26
=
The winner of game is : Player28
import
8
The winner of game is : Player23
The winner of game is : Player24
The winner of game is : Player200
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]8
Output:
The winner of game is : Player2
Sự mô tả
Phương thức số python đồng nhất [] trả về một float ngẫu nhiên r, sao cho x nhỏ hơn hoặc bằng r và r nhỏ hơn y.uniform[] returns a random float r, such that x is less than or equal to r and r is less than y.
Cú pháp
Sau đây là cú pháp cho phương thức đồng nhất [] -uniform[] method −
uniform[x, y]
LƯU Ý - Hàm này không thể truy cập trực tiếp, vì vậy chúng ta cần nhập mô -đun thống nhất và sau đó chúng ta cần gọi chức năng này bằng đối tượng tĩnh ngẫu nhiên. − This function is not accessible directly, so we need to import uniform module and then we need to call this function using random static object.
Thông số
x - Đặt giới hạn dưới của phao ngẫu nhiên. − Sets the lower limit of the random float.
y - đặt giới hạn trên của phao ngẫu nhiên. − Sets the upper limit of the random float.
Giá trị trả về
Phương pháp này trả về một số điểm nổi.
Thí dụ
Ví dụ sau đây cho thấy việc sử dụng phương thức đồng nhất [].
#!/usr/bin/python import random print "Random Float uniform[5, 10] : ", random.uniform[5, 10] print "Random Float uniform[7, 14] : ", random.uniform[7, 14]
Hãy để chúng tôi chạy chương trình trên, điều này sẽ tạo ra kết quả sau -
Random Float uniform[5, 10] : 5.52615217015 Random Float uniform[7, 14] : 12.5326369199
python_numbers.htm