Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The 20th official Python column, stop! Don’t miss this zero-based article!

In the first two articles we looked at some common mathematical processing functions, and then we showed some random number related functions!

The random number

Programming, sometimes we need random numbers. For example, in the case of lottery or random sampling for data analysis, the random function is very important.

Python provides the built-in Random library, which gives developers a wealth of choices

Here we can be divided into the following categories:

  • Generate random numbers randomly (e.g., function random)
  • Select a number within a given range or range as a random number (e.g. function choice, uniform)
  • Use a random algorithm to generate random numbers (such as seed and random combined random number generation)

The code shown

The committee has prepared the following four cycles to show the process of generating random numbers.

You can simply copy and save as randnumber.py to run:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/10/26 10:29 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : randnumber.py
# @Project : hello
from random import choice, randrange, seed, random, uniform

for i in range(5) :print("round [%s] get a choice : %s" % (i, choice([1.2.3.4.5)))Select a random parameter from the given argument list

print("*" * 16)
for i in range(5) :print(
        "round [%s] randrange value %s" % (i, randrange(1.10.3)))  # select number from 1 to 10, step 3, similar to choice([1,4,7])

print("*" * 16)
for i in range(2) :print("round [%s] seed value %s" % (i, seed(i)))  The seed function does not produce any results; the seed function affects the random function
    print("rand value %s " % random())
    print("rand value %s " % random())
    print("round [%s] seed value %s" % (i, seed(f"hello{i}")))  The seed function does not produce any results; the seed function affects the random function
    print("next rand value %s " % random())

print("*" * 16)
for i in range(2) :print("round [%s] uniform %s" % (i, uniform(2.10)))  # Generate random numbers between 2 and 10 (float)
Copy the code

Here’s how it works:

One by one within the specified range. Any other options?

This process is similar to shuffling, except we can use the choice function to do it. You can also use the shuffle function.

Its argument is also of type list. Very simple, let’s look at the code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2021/10/26 10:29 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : randnumber2.py
# @Project : hello
from random import shuffle

print("*" * 16)
for i in range(2) :list = [1.2.3.4.5]
    print("round [%s] shuffle value %s" % (i, shuffle(list)))  # Random shuffle function, like when we play poker, shuffle the cards after finishing a line
    print("list = %s" % list)
Copy the code

Here’s how it works. We shuffled the deck twice, and each time the result was different (because the probability of it being the same was very low).

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!