如何从反向加权列表中随机选择?(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
2024-04-24 17:04

满意答案

你得到这个错误的原因是关联和具有相同名称的列之间的冲突(我从你的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 your client column from deals table, rename this column or rename the belongs_to :client association.

相关问答

更多

属于_通过协会(belongs_to through associations)

一个belongs_to关联不能有一个:through选项。 你最好在Choice上缓存question_id ,并为表添加一个唯一的索引(特别是因为validates_uniqueness_of容易出现竞争条件)。 如果您偏袒,请向Choice确认确认答案的question_id匹配的自定义验证,但是听起来应该永远不会有最终用户提交创建此类不匹配的数据的机会。 A belongs_to association cannot have a :through option. You're bette...

belongs_to有多个模型(belongs_to with multiple models)

尝试这样的事情: :locals => {:feed_group => @books.feeds.all(:conditions => {:section_id => @section.id})} Try something like this: :locals => {:feed_group => @books.feeds.all(:conditions => {:section_id => @section.id})}

有关belongs_to的条件(Conditions on belongs_to)

您正在使用User#requests关联,该关联对Request#user条件没有任何线索。 aUser.requests.create根据requests关联中给出的任何条件构建并保存Request对象,并将其添加到关联请求列表中。 当然, Request恰好有一个user关联,但在您的示例中从未使用过。 是的,它会导致怪异的行为和不一致: >> user = User.create(:can_make_requests => false) => #<User id: 3, can_make_r...

继续获取ActiveRecord :: AssociationTypeMismatch(Keep getting ActiveRecord::AssociationTypeMismatch)

不确定是否与使用select而不是collection_select有关? <%= f.select( :parent_id, Satellite.all.collect { |s| [ s.name, s.id ] }, { :include_blank => '-select-' } ) %> 可能 <%= f.collection_select :parent_id, Satellite.all, :id, :name, { :include_blank => true } %> 看起来...

Rails - 将模型与另一个模型的多个实例相关联时出现AssociationTypeMismatch错误(Rails - AssociationTypeMismatch error with associating a model with multiple instances of another model)

original_account_id期待一个帐户对象。 你不能设置id。 credit_record.original_owner = account credit_record.account = account 要么 credit_record.account_id = account.id 请将您的关联重命名为以下内容 class CreditRecord < ActiveRecord::Base belongs_to :original_owner, :foreign_key => ...

ActiveRecord :: AssociationTypeMismatch:for has_many:through relationship(ActiveRecord::AssociationTypeMismatch: for has_many :through relationship)

问题似乎是你已经将PlayedGame上的belongs_to关联错误地定义为复数,当它们应该是单数时。 将它们更改为: class PlayedGame < ActiveRecord::Base belongs_to :user belongs_to :game end 然后你应该可以使用: User.first.games << Game.first The problem appears to be that you've defined the belongs_to assoc...

多个belongs_to和AssociationTypeMismatch(Multiple belongs_to and AssociationTypeMismatch)

你得到这个错误的原因是关联和具有相同名称的列之间的冲突(我从你的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 eit...

自定义数据库上的Rails has_many / belongs_to ActiveRecord :: AssociationTypeMismatch得到了Fixnum(Rails has_many/belongs_to on custom database ActiveRecord::AssociationTypeMismatch got Fixnum)

我认为问题是你有category列(所以Rails为它创建category方法)和具有相同名称的category关联。 您可以为关联提供其他名称 我创建了测试应用 class CreateProducts < ActiveRecord::Migration def change create_table :products, id: false do |t| t.integer :productID t.integer :category t.str...

Rails 4 ActiveRecord :: AssociationTypeMismatch(Rails 4 ActiveRecord::AssociationTypeMismatch)

您正在尝试将UserAction对象添加到UserLog集合,这肯定会引发TypeMismatch错误。 试试这个: user = User.first action = UserAction.first user.user_actions << action user.save You're trying to add UserAction object to UserLog collection which will definitely throw a TypeMismatch error...

Rails 4中的ActiveRecord :: AssociationTypeMismatch(ActiveRecord::AssociationTypeMismatch in Rails 4)

你强大的params行应该使用art_description_attributes ,而不是art_description : params.require(:artwork).permit(:name, :autor, :date, :museum, :comment, :create_place, art_description_attributes: [ :type, :base, :style, :genre, :plot, :reference, :format_weight,...

相关文章

更多

Python 列表(list)操作

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结 ...

A Great List of Windows Tools

Windowsis an extremely effective and a an efficient ...

Becoming a data scientist

Data Week: Becoming a data scientist Data Pointed, ...

[How to] Make custom search with Nutch(v 1.0)?(转)

http://puretech.paawak.com/2009/04/29/how-to-make-c ...

hibernate 对list修改

class A{ private String a; private String b; pri ...

Become a Master Designer: Rule Three: Contrast, Contrast, Contrast

Part Three of Seven Easy Principles to Becoming a M ...

FreeMarker集合(List、Map)

我们上一节认识了FreeMarker基本数据类型,接口认识FreeMarker集合(List、Map) ...

Drupal Forums instead of phpBB or vBulletin: A casestudy

5th Jan, 10 Drupal drupal advanced forum drupa ...

Hadoop的I/O

1. 数据完整性:任何语言对IO的操作都要保持其数据的完整性。Hadoop当然希望数据在存储和处理中不 ...

最新问答

更多

sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)

否(它不会使它们无法访问),是(您可以在没有停机的情况下运行它)。 sp_updatestats可以在没有停机的情况下针对实时数据库运行。 No (it doesn't make them inaccessible), and Yes (you can run it without downtime). sp_updatestats can be run against a live database without downtime.

如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)

最终,我们选择了使用Spark Framework for Java实现的后端REST API。 这可能不是最强大的,用户反馈一直是个问题。 我们将命令行界面拆分为提交REST调用,并将结果显示给用户。 Ultimately, we chose to go the route of having a backend REST API that was implemented with the Spark Framework for Java. This may not be the most r

AESGCM解密失败的MAC(AESGCM decryption failing with MAC)

您不能将Encoding.UTF8.GetString应用于任意二进制数据。 它只能解码使用UTF-8编码字符串的结果的字节。 .net实现将默默地破坏数据,而不是默认情况下抛出异常。 您应该使用Base64: Convert.FromBase64String和Convert.ToBase64String You can't apply Encoding.UTF8.GetString to arbitrary binary data. It can only decode bytes that

Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)

我希望能看到更多你的Sass代码等,但我的猜测是你需要在所有嵌套行上使用nest行为。 在我看来,基金会在Sass中的行主要是为了在一个层面上使用。 嵌套在另一行中的任何行都应使用nest行为,除非您希望在列上添加额外的填充。 在你的CodePen中,我能够通过向所有行添加一类collapse来修复列上填充的问题,我认为这与执行$behavior: nest相同$behavior: nest在Sass中$behavior: nest :

湖北京山哪里有修平板计算机的

京山有个联想的专卖店,那里卖平板电脑,地址在中百前面的十字路口右拐 ,他们应该会提供相关的维修服务。

SimplePie问题(SimplePie Problem)

我怀疑由于内容的性质(包含代码),stackoverflow提要不起作用。 我使用许多feed解析器看似“正常”的feed有类似的问题,尽管我最近运气最多的是Zend_Feed。 试试吧 I suspect the stackoverflow feed is not working due to the nature of the content (contains code). I have had similar issues with seemingly "normal" feeds us

在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)

是的,您可以通过getApplicationContext()任意数量的时间(后台任务), getApplicationContext()仅返回应用程序的context 。 Yes, you can pass getApplicationContext() any number of time (Background Tasks ) you want, getApplicationContext() simply returns context of the application.

HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)

这是我最终做的事情: 我无法以这种方式提供完全访问权限,而是在project level folder中设置了一个虚拟HTML页面,该页面单击自身以重定向到位于separate, non-project util folder的HTML文件。 这允许我保留除了那个之外的所有内容,非常小的文件分开但不存在文件访问问题。 Here is what I ended up doing: I wasn't able to provide full access exactly this way, but

为什么我会收到链接错误?(Why do I get a linker error?)

看起来您的编译器错误地将名称引入到全局名称空间中,而不是C ++ 11 3.5 / 7中指定的最内层名称空间( Bushman ): 如果没有找到具有链接的实体的块范围声明来引用某个其他声明,那么该实体是最内层封闭名称空间的成员。 代码按照预期在GCC上编译: http : //ideone.com/PR4KVC 你应该能够通过在构造函数的块作用域中声明它之前(或代替它)在正确的名称空间中声明该函数来解决该bug。 但是,我无法访问您的编译器来测试它。 It looks like your co

如何正确定义析构函数(How to properly define destructor)

在C ++中,你需要手动释放内存。 没有垃圾收集器。 您显然需要在析构函数内手动释放内存。 如果您使用new分配内存,则需要对在deconstructor中使用new分配的每个资源使用delete ,例如: class1::~class1(void) { delete resource1; delete resource2; etc... } In C++ you need to free the memory manually. There's no garbage