选择重量随机的物品

编程入门 行业动态 更新时间:2024-10-25 10:28:12
本文介绍了选择重量随机的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个大概的清单. 10000个项目.当前的情况是,每个项目都有相关的权重(优先级或重要性).现在最小的权重是-100(可以删除负值和零值),而最大的权重是1500.体重是由人们的直觉决定的(有人认为该物品对社区很重要).由于确定最重要的商品并不容易,我想使用一些随机因素,以使重量较低的商品选择的机会更少,并且将来会调整其重量(常识和随机性.)

I have a list of approx. 10000 items. The current situation is that every item has an associated weight (priority or importance). Now the smallest weight is -100 (negative and zero values can be removed) and the highest weight is 1500. Weight is determined by intuition by people (how somebody thinks the item is important to community). Because it's not easy to determine the most important item, I'd like to use some random factor, so that items with lower weight will have less chance to be chosen and their weight will be adjusted in the future (some mix of common sense and randomness).

您知道如何对函数getItem进行编码吗?

Do you know how to code a function getItem?

def getItem(dict): # this function should return random item from # the dictionary of item-weight pairs (or list of tuples) # Normally I would return only random item from the dictionary, # but now I'd like to have this: The item with weight 1500 should # have much more chance to be returned than the item with weight 10. # What's my idea is to sum up the weights of all items and then compute # some ratios. But maybe you have better idea. return randomItem

谢谢

推荐答案

看看这个,我认为您需要在不同方法之间进行一些很好的比较在Python中加权随机生成

Have a look at this, i think it's what you need with some nice comparision between different methods Weighted random generation in Python

建议的最简单方法是:

import random def weighted_choice(weights): totals = [] running_total = 0 for w in weights: running_total += w totals.append(running_total) rnd = random.random() * running_total for i, total in enumerate(totals): if rnd < total: return i

您可以在上面的链接中找到更多详细信息和可能的改进以及一些不同的方法.

You can find more details and possible improvements as well as some different approaches in the link above.

更多推荐

选择重量随机的物品

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

发布评论

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

>www.elefans.com

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