DEV Community

Ozodbek Tohirov
Ozodbek Tohirov

Posted on

PYTHON'da random module'ining method'lari

1. choice

Ta'rif: list'dan tasodifiy element qaytaradi

Sintaksis

random.choice(sequence)
Enter fullscreen mode Exit fullscreen mode

sequence parametri majburiy. sequence - list, tuple va numberbo'lishi mumkin.

Misol:

import random

mylist = ["olma", "banan", "olcha"]

print(random.choice(mylist))
Enter fullscreen mode Exit fullscreen mode

Natija:

# ["banan", "olma", "olcha"]
Enter fullscreen mode Exit fullscreen mode

_________________________________________________

2. Randint

Ta'rif: ma'lum bir tanlangan butun sondan tanlangan ikkinchi songacha bo'lgan biror bir sonni tasodifiy (randomly) ravishda qaytaradi. ya'ni a dan b gacha bo'lgan oraliqdagi sonlarni tasodifiy (randomly) qaytaradi

Sintaksis

random.randint(start, stop)
Enter fullscreen mode Exit fullscreen mode

Misol

3 va 9 raqamlari oralig'idagi tasodifiy (randomly) raqam qaytarish:

import random

print(random.randint(3, 9))
Enter fullscreen mode Exit fullscreen mode

Natija:

# 5
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ozodbektohirov profile image
Ozodbek Tohirov

You can disscuss