数学类
随机
import random
random.randint(start, stop) # 都是闭区间
# random.choice 在list, tuple, string时间复杂度是O(1);在set, dictionary时间复杂度是O(n), 集合和字典是无序、没有索引, 需要遍历来随机选择
random.choice(mylist)
# 打乱顺序
cards = list(range(53))
random.shuffle(cards)-- To shuffle an array a of n elements (indices 0..n-1):
for i from n−1 down to 1 do
j ← random integer such that 0 ≤ j ≤ i
exchange a[j] and a[i]from random import randint
def randomize (arr, n):
# Start from the last element and swap one by one. We don't need to run for the first element that's why i > 0
for i in range(n - 1, 0, -1):
j = randint(0, i + 1)
arr[i], arr[j] = arr[j], arr[i]
return arr位运算
Operation
Code
Example
Note
Last updated