DEV Community

Ozodbek Tohirov
Ozodbek Tohirov

Posted on

1

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
ozodbektohirov profile image
Ozodbek Tohirov

You can disscuss

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay