Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate change figure by using Random function in Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "Python how to use random functions to generate change graphics". In daily operation, I believe many people have doubts about how Python uses random functions to generate change graphics. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "Python how to use random functions to generate change graphics"! Next, please follow the small series to learn together!

preface

Thinking:

1) How many pixels are moved at a time is determined by a random function, and the direction of each movement is also randomly determined. Move position size and direction by random direction and random pixel together.

2) Make sure to save the position of each move in a list for later matplotlib calls to generate charts.

Therefore, it can be divided into two files, one for the rand_moving class, generating moving pixels, directions, and recording related data, stored in the series, and the other for the drawing module randdraw_visual, calling matplotlib and rand_moving classes, generating an instance, and calling the calculated series group to generate a chart.

The rand_moving.py file defines functions as follows:

1. Initialize the program, set a parameter, that is, the number of times of movement, and set all initialization positions to 0.

2. Randomly generate the directions of x and y and the moving pixels, and multiply them to obtain the relative moving distance, that is, the distance and direction of each movement, that is, four random functions are needed to determine the size and direction of the moving position in the horizontal direction and the vertical direction respectively.

3, calculate the next position, and save it to the position series, that is, after each step, the absolute position in the screen.

As follows:

from random import choice #random is a random function module that comes with the system class Rand_moving(): #Define a Rand_moving class def __init__(self,num_times=100000): #initialization, set the default parameter to 100000, you can modify this parameter to try the machine running speed self.num_times = num_times #Number of moves self.x_values=[0] #Set two series to store the position of each step. The initial position is (0, 0). The number of elements in the series will be increased to num_times to record the position information of each step. self.y_values=[0] def fill_moving(self): #Defines a function to calculate the direction and distance of movement and calculate the position information to be saved while len(self.x_values)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report