如何从反向加权列表中随机选择?(How can I make a random selection from an inversely-weighted list?)
给定一个整数列表,例如
1, 2, 3, 4
,我知道如何根据它们的重量选择项目。 示例项目的概率分别为10%,20%,30%和40%。是否有一种同样简单的方法来选择基于其重量的倒数的项目? 使用此方法,示例列表将等于
1, 1/2, 1/3, 1/4
(48%,24%,16%,12%)的加权列表,但我想避免转换和使用浮点运算。 (假设所有整数都是正数且非零。)Given a list of integers, e.g.
1, 2, 3, 4
, I know how to select items based on their weight. The example items would have probabilities of 10%, 20%, 30%, and 40%, respectively.Is there an equally simple method of selecting items based on the inverse of their weight? With this method, the example list would be equal to a weighted list of
1, 1/2, 1/3, 1/4
(48%, 24%, 16%, 12%), but I want to avoid the conversion and use of floating-point arithmetic. (Assume all of the integers are positive and non-zero.)
原文:https://stackoverflow.com/questions/7762569
满意答案
你得到这个错误的原因是关联和具有相同名称的列之间的冲突(我从你的
deal_params
方法知道它)。 您应该从deals
表中删除client
列,重命名此列或重命名belongs_to :client
关联。The reason you get this error is conflict between association and column with the same name (I know it from your
deal_params
method). You should either remove yourclient
column fromdeals
table, rename this column or rename thebelongs_to :client
association.
相关问答
更多属于_通过协会(belongs_to through associations)
belongs_to有多个模型(belongs_to with multiple models)
有关belongs_to的条件(Conditions on belongs_to)
继续获取ActiveRecord :: AssociationTypeMismatch(Keep getting ActiveRecord::AssociationTypeMismatch)
Rails - 将模型与另一个模型的多个实例相关联时出现AssociationTypeMismatch错误(Rails - AssociationTypeMismatch error with associating a model with multiple instances of another model)
ActiveRecord :: AssociationTypeMismatch:for has_many:through relationship(ActiveRecord::AssociationTypeMismatch: for has_many :through relationship)
多个belongs_to和AssociationTypeMismatch(Multiple belongs_to and AssociationTypeMismatch)
自定义数据库上的Rails has_many / belongs_to ActiveRecord :: AssociationTypeMismatch得到了Fixnum(Rails has_many/belongs_to on custom database ActiveRecord::AssociationTypeMismatch got Fixnum)
Rails 4 ActiveRecord :: AssociationTypeMismatch(Rails 4 ActiveRecord::AssociationTypeMismatch)
Rails 4中的ActiveRecord :: AssociationTypeMismatch(ActiveRecord::AssociationTypeMismatch in Rails 4)
相关文章
更多Python 列表(list)操作
A Great List of Windows Tools
Becoming a data scientist
[How to] Make custom search with Nutch(v 1.0)?(转)
hibernate 对list修改
Become a Master Designer: Rule Three: Contrast, Contrast, Contrast
FreeMarker集合(List、Map)
Drupal Forums instead of phpBB or vBulletin: A casestudy
Hadoop的I/O
最新问答
更多sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
湖北京山哪里有修平板计算机的
SimplePie问题(SimplePie Problem)
在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
为什么我会收到链接错误?(Why do I get a linker error?)
如何正确定义析构函数(How to properly define destructor)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!