游戏中的随机抽样算法

编程入门 行业动态 更新时间:2024-10-26 18:26:33

游戏中的随机抽样<a href=https://www.elefans.com/category/jswz/34/1770096.html style=算法"/>

游戏中的随机抽样算法

相关题目:
382. 链表随机节点
384. 打乱数组
398. 随机数索引

文章详解:
游戏中的随机抽样算法

class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextclass RandListNode:"""382. 链表随机节点/"""def __init__(self, head: ListNode):self.head = headself.r = random.Random()def getRandom(self) -> int:i, res = 0, 0p = self.head# while 循环遍历链表while p:i += 1# 生成一个 [0, i) 之间的整数# 这个整数等于 0 的概率就是 1/iif 0 == self.r.randint(0, i-1):res = p.valp = p.nextreturn resclass ShuffleArray:"""384. 打乱数组/"""def __init__(self, nums: List[int]):self.nums = numsdef reset(self) -> List[int]:return self.numsdef shuffle(self) -> List[int]:copy = self.nums.copy()n = len(self.nums)for i in range(n):# 生成一个 [i, n-1] 区间内的随机数r = i + random.randint(0, n-i-1)# 交换 nums[i] 和 nums[r]copy[i], copy[r] = copy[r], copy[i]return copyclass RandomIndex:"""398. 随机数索引/"""def __init__(self, nums: List[int]):self.nums = nums# self.rand = random.Random()def pick(self, target: int) -> int:count, res = 0, -1for i in range(len(self.nums)):if self.nums[i] != target:continuecount += 1if random.randint(1, count) == 1:res = ireturn resfrom collections import defaultdict
from random import choice
class RandomIndex2:"""398. 随机数索引"""def __init__(self, nums: List[int]):self.pos = defaultdict(list)for i, num in enumerate(nums):self.pos[num].append(i)def pick(self, target: int) -> int:return choice(self.pos[target])

更多推荐

游戏中的随机抽样算法

本文发布于:2023-11-16 14:49:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   游戏中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!