Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of generating changing figure by using Random function in Python

Shulou Source: shulou.com Published: 2022-05-31 19:21:07 09月24日 Update

The knowledge of this article "Python uses random functions to generate changing graphics" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "Python uses random functions to generate changing graphics" article.

Question:

1. Whether the above programs can be optimized (such as those with the same function)

2. Create three three instances and use three statements. Can you create a function and automatically create n instances by entering only one number n? At the same time, the num_times of each instance is random, (when n is relatively large, the num_times should be small)

3. When the above functions are implemented, the program runs and only one parameter (the number of instances created) is entered, the corresponding num_times will be automatically generated, and the relevant functions will be called to generate the corresponding chart.

Yes, a multiplication formula is used to calculate the direction and moving position of each step in the class Rand_moving ().

X_direction = choice, 1 upward, 0 unchanged,-1 downward x_distance = choice, x_step = x_direction*x_distance # x per moving pixel, moving direction multiplied by moving distance To determine the direction of movement along x distance y_direction = choice ([1maimei 1]) # y, 1 up, 0 unchanged,-1 downward y_distance = choice ([0 mine1 min2 pencore 4 5]) # y, y_step = y_direction*y_distance # the moving direction multiplied by the moving distance to determine the distance moving along y

Therefore, we can sort out a calculation method, which can be called directly. As for 2Jing 3, it is obvious that it can also be done.

In the first step, the multiplication formula is proposed to form a separate method that acts on itself. The code is as follows:

Def get_step (self,direction,distance): return distance*direction def fill_moving (self): while len (self.x_values)

Tags: Movement dictionary instance number generation data method input function code variable content direction number of times run graphics color change character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Microsoft NVidia MySQL