如何从反向加权列表中随机选择?(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)[2023-03-16]
一个belongs_to关联不能有一个:through选项。 你最好在Choice上缓存question_id ,并为表添加一个唯一的索引(特别是因为validates_uniqueness_of容易出现竞争条件)。 如果您偏袒,请向Choice确认确认答案的question_id匹配的自定义验证,但是听起来应该永远不会有最终用户提交创建此类不匹配的数据的机会。 A belongs_to association cannot have a :through option. You're better off ... -
belongs_to有多个模型(belongs_to with multiple models)[2022-05-23]
尝试这样的事情: :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)[2022-04-01]
您正在使用User#requests关联,该关联对Request#user条件没有任何线索。 aUser.requests.create根据requests关联中给出的任何条件构建并保存Request对象,并将其添加到关联请求列表中。 当然, Request恰好有一个user关联,但在您的示例中从未使用过。 是的,它会导致怪异的行为和不一致: >> user = User.create(:can_make_requests => false) => #继续获取ActiveRecord :: AssociationTypeMismatch(Keep getting ActiveRecord::AssociationTypeMismatch)[2024-02-03]
不确定是否与使用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 } %> 看起来在模型的自 ...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 => "acco ...问题似乎是你已经将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 associatio ...你得到这个错误的原因是关联和具有相同名称的列之间的冲突(我从你的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 r ...我认为问题是你有category列(所以Rails为它创建category方法)和具有相同名称的category关联。 您可以为关联提供其他名称 我创建了测试应用 class CreateProducts < ActiveRecord::Migration def change create_table :products, id: false do |t| t.integer :productID t.integer :category t.string ...Rails 4 ActiveRecord :: AssociationTypeMismatch(Rails 4 ActiveRecord::AssociationTypeMismatch)[2023-11-06]
您正在尝试将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. Try ...Rails 4中的ActiveRecord :: AssociationTypeMismatch(ActiveRecord::AssociationTypeMismatch in Rails 4)[2022-02-11]
你强大的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, :for ...相关文章
更多- 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
- Lua 文件 I/O 操作
- FreeMarker集合(List、Map)
- Drupal Forums instead of phpBB or vBulletin: A casestudy
- Hadoop的I/O
最新问答
更多- CSS修复容器和溢出元素(CSS Fix container and overflow elements)
- SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
- nginx 80端口反向代理多个域名,怎样隐藏端口的
- xcode提醒样式,swift 3(xcode alert style, swift 3)
- 在Chrome控制台中调试JavaScript(debugging javascript in Chrome console)
- Javascript - 试图围绕自定义事件(Javascript - Trying to wrap my head around custom events)
- 边栏链接不可点击(Sidebar links aren't clickable)
- 使用recpatcha gem时如何显示其他表单错误?(How do I display other form errors when using the recpatcha gem?)
- boost.python避免两次注册内部类,但仍然在python中公开(boost.python Avoid registering inner class twice but still expose in python)
- Android 现在软件很少吗?以后会多起来吗
- 如何在ActiveAdmin 0.5.0中为资源全局指定预先加载?(How to specify eager loading globally for a resource in ActiveAdmin 0.5.0?)
- matlab代码为黄金比例持续分数(matlab code for golden ratio continued fraction)
- Android浏览器触摸事件位置(Android browser touch event location)
- 将cURL输出分配给Bash中的变量(Assign output to variable in Bash)
- 我如何在MVC视图上没有时间获取当前日期(how i can get current date without time on MVC view)
- sql连接函数(sql join of function)
- 为什么在Xamarin Media插件中使用ImageSource.FromStream而不是FromFile?(Why use ImageSource.FromStream instead of FromFile in Xamarin Media plugin?)
- 这段代码是否真的可以防止SQL注入?(Will this code actually work against SQL-injection? [duplicate])
- 信阳方远计算机学校大专证被国家认可么
- React / Rails AJAX POST请求返回404(React/Rails AJAX POST request returns 404)
- Android与php服务器交互(Android interact with php server)
- 自动刷新QTableWidget可能吗?(Refresh QTableWidget automatically possible?)
- JVM / Compiler优化对象的未使用属性(optimization of unused properties of object by JVM / Compiler)
- 插入表格时,乌克兰字符会更改为问号(Ukrainian character change to question mark when insert to table)
- 在头文件中包含异常类(Including an exception class in a header file)
- 完成c#中的执行后关闭sqlcmd(Close sqlcmd after finishing executing in c#)
- 使用软导航栏正确检测屏幕尺寸(Detecting screensize correctly with soft navigation bar)
- Typescript:从输入更新值(Typescript : update value from input)
- 如何在执行某些行后仅在断点处停止?(How to only stop at a breakpoint after some line was executed?)
- 以未定义的元素在JSON中循环(loop in JSON with undefined elements)