Random module in Python

Random module in Python

Time and again if you are building a small game or if you are trying to pick something randomly in Python, you will have to use Random module.

This write up tries to explore the random module in python.

  • As always, to use the random module, import it.
from random import choice, choices, shuffle, sample
  • To pick one element out of a list randomly, use choice from random module
>>> what_to_do = ['study', 'play', 'work', 'hobby project', 'sleep', 'read']
>>> choice(what_to_do)
'study'
>>> choice(what_to_do)
'work'
  • To pick more than one element at the same time from a list, use choices from random module. The second parameter for choices signifies the number of times an element has to be picked from the list.

Note : In the below code section you can see that, a same element can be picked more than once from a list on using Choices.

>>> choices(what_to_do, k=4)
['work', 'work', 'read', 'study']
>>> choices(what_to_do, k=6)
['work', 'play', 'read', 'sleep', 'read', 'read']
  • Now making use of Counters from collection module, we can see how the elements are picked when we decide to choose them from a list for a large number of times. (say over a 1000 times)
>>> from collections import Counter
>>> Counter(choices(what_to_do, k=1000))
Counter({'read': 191, 'work': 182, 'study': 172, 'hobby project': 163, 'sleep': 157, 'play': 135})

Note: Details of using Counter written in a separate article here.

  • We can also control the ratios in which the elements are picked from a list. From the above example list, if you love studying and you want it to be picked more often you can set it to higher ratio like the below code.
>>> Counter(choices(what_to_do, [6,5,4,3,2,1], k=1000))
Counter({'study': 291, 'work': 222, 'play': 213, 'hobby project': 137, 'sleep': 93, 'read': 44})

In the above snippet you can note that, "study" is picked over 6 times more than "read". Note : The ratio / weight-age should be provided to each element in the list. Else, a ValueError is raised. In should length of ratio set should be equivalent to the length of list.

  • As seen in choices above, as you choose more than one element, there might be a chance of duplication of the element picked. i.e, a same element might be picked twice. To avoid this, you may use sample.
>>> sample(what_to_do, k=4)
['sleep', 'read', 'play', 'work']
  • shuffle can be used to shuffle the elements in the list and jumble them up.
>>> what_to_do
['study', 'play', 'work', 'hobby project', 'sleep', 'read']
>>> shuffle(what_to_do)
>>> what_to_do
['read', 'work', 'hobby project', 'study', 'sleep', 'play']

Note that the original list is shuffled above.

Hope this helps.

要查看或添加评论,请登录

Akshay Deshpande的更多文章

  • [Performance] : What does CPU% usage tell us ?

    [Performance] : What does CPU% usage tell us ?

    [Edit] -- All my latest articles are not published at - https://performanceengineeringin.wordpress.

    6 条评论
  • [Performance] : Understanding CPU Time

    [Performance] : Understanding CPU Time

    As a Performance Engineer, time and again you will come across a situation where you want to profile CPU of a system…

    4 条评论
  • Performance Engineering Checklists

    Performance Engineering Checklists

    Checklists; something that I truly believe in having for everything. Right from list of things to check before leaving…

    5 条评论
  • [Linux] Understanding Load Average

    [Linux] Understanding Load Average

    I am writing this quick article to help understand the importance of Load Average in Linux for a Performance Engineer…

    4 条评论
  • Understanding Physical and Logical CPUs

    Understanding Physical and Logical CPUs

    Here is a quick write up on what actually Physical and Logical CPU mean, and how are they different. To begin with, the…

    3 条评论
  • Counter in Python

    Counter in Python

    Counters is the best thing that has happened to Python when you think as a Data analyst. Counters can be used as an…

    2 条评论
  • Jprofiler - CPU profiling

    Jprofiler - CPU profiling

    Jprofiler can serve many purposes, and CPU profiling and analysis is one of them. This article is more about how to…

    1 条评论
  • Learning how to learn [Python]

    Learning how to learn [Python]

    As Brain Herbert has rightly said: It is very much important to learn new things (anything) as learning never ends…

社区洞察

其他会员也浏览了