python绘制如你的学号是:20214075109,则生成的随机数个数为20+21+40+75+109=265,即对1000以内的265个随机数进行排序,同时使用柱状图完成排序的动画演示。
时间: 2023-12-31 19:06:29 浏览: 51
好的,我可以为您提供相关的代码实现。首先,我们需要引入一些必要的库,包括`random`和`matplotlib`。代码如下:
```python
import random
import matplotlib.pyplot as plt
```
然后,我们可以根据您提供的学号计算出需要生成的随机数个数,并使用`random.sample()`函数生成这些随机数。代码如下:
```python
student_id = "20214075109"
num_list = [int(i) for i in student_id if i.isdigit()]
num_count = sum(num_list)
random_list = random.sample(range(1000), num_count)
```
接下来,我们可以使用`matplotlib`的柱状图功能来展示排序的动画效果。代码如下:
```python
fig, ax = plt.subplots()
ax.set_xlim(0, 250)
ax.set_ylim(0, 1000)
rects = ax.bar(range(num_count), random_list)
for i in range(num_count - 1):
for j in range(i + 1, num_count):
if random_list[i] > random_list[j]:
random_list[i], random_list[j] = random_list[j], random_list[i]
for rect, h in zip(rects, random_list):
rect.set_height(h)
plt.pause(0.01)
plt.show()
```
以上代码将生成一个柱状图窗口,并对生成的随机数进行冒泡排序的动画演示。您可以通过运行代码来查看效果。