首页 \ 问答 \ 随机概率选择(Random Probability Selection)

随机概率选择(Random Probability Selection)

假设我有10个奖品给100个人。 每个人一次射击一次。 因此,如果第一个人未能赢得奖品,则概率会上升,99分中有10分,所以一个......所有10个奖项必须去。

写这篇文章的最佳方式是,如果还剩下奖品,那么这个人将有一个机会获得奖金......

我这样想的是:

int playersLeft = 100
int winners = 0

while (winners < 10)
    winners += (random.Next(playersLeft--)<(10-winners)) ? 1 : 0;

我想知道是否有更好或更直接的方法来做到这一点。 我知道它看似简单,但这个简单的任务是应用程序非常重要的一部分,它必须是正确的。

澄清:为什么我想做这样的事情:

实际上有无限数量的玩家,每个玩家有一个X在Y的概率下赢,比如说10/100 = 10%。 但是如果我把它留给随机数发生器,那么在100个玩家中,只有9个会赢,或者最差11个。在我的应用程序中,我必须保证每100个玩家不会再少于10个玩家赢得。


Say I have 10 prizes to give to 100 people. Each person gets a shot, one at a time. So if the first person fails to win a prize, the probability goes up, 10 in 99, and so one... Also all 10 prizes MUST go.

What would be the best way to write this in such a way that by the end if there is still a prize left, that person would have a 1 in 1 chance to get a prize...

What I was thinking like this:

int playersLeft = 100
int winners = 0

while (winners < 10)
    winners += (random.Next(playersLeft--)<(10-winners)) ? 1 : 0;

I wanted to know if there was a better or more straight forward way to do it. I know it seems simple but this simple task is part of a very important aspect of the app and it must be right.

TO CLARIFY: Why I want to do something like this:

In reality there is an unlimited number of players, each with an X in Y probability to win, say 10/100 = 10%. However if I leave it to the random number generator, there is a chance that in 100 players, only 9 would win, or worst, 11. In my app, I must assure that no more and no less than 10 players for every 100 will win.


原文:https://stackoverflow.com/questions/6949221
更新时间:2023-01-21 18:01

最满意答案

你没有遗漏任何东西,你的评估是正确的。 ASP.NET表不会将其内容保存为查看状态。

我假设至少部分原因是一个表可以包含任何数量的任何类型的数据,并且在某些情况下可能真的开始添加到视图状态的大小。

无论哪种方式,这就是他们的工作方式。 如果页面上有回发,则需要在每个加载事件上重新填充表,或者将表数据存储为自己查看状态,然后从数据中重新填充表。


You're not missing anything, your assessment is correct. ASP.NET tables do not save their contents to view state.

I assumed at least part of the reason is that a table can contain any amount of any type of data and could really start to add to the size of the view state in some cases.

Either way, that's the way they work. If postbacks are possible on your page, you'll need to either repopulate the table on each load event, or store the table data to view state yourself and then repopulate the table from the data.

相关问答

更多
  • 像TextBoxes和DropDownLists这样的大多数控件都可以在没有视图状态的情况下正常运行。 我不知道任何开发过程问题,除了通过代码创建或修改的任何控件或属性以外,不会在没有视图状态的情况下持续存在,因此您必须在回发中重新创建/修改它们。 我有一些非常大的视图大页面。 我做了一个实验来禁用整个项目的视图状态,并发现(起初)没有明显的功能损失。 然后在测试中出现了一些小问题,所以我们恢复了它。 但是我们的300页的网页应用程序大概99%没有视图状态的功能。 我们遇到的问题主要集中在数据网格上 - 主 ...
  • 我看到你的代码没有回发方法。 因此,一旦您的页面回发,viewstate值将丢失。 请将您的代码更改为 ViewState("ItemID") = 0 If Not Request.QueryString("id") Is Nothing Then ViewState("ItemID") = Request.QueryString("id") ItemID = Integer.Parse(ViewState("ItemID")) End If If Not Page.IsPostBack T ...
  • ViewState是允许在页面回传中保留状态值的机制。 网络是无状态的。 但在ASP.NET中,页面状态会自动保留在页面中。这是通过使用ViewState.In ViewState将值加密并保存在隐藏控件中。 当您查看使用ViewState的页面源代码(在浏览器中)时,您可能会看到隐藏的视图状态 输入将看起来像这样: ...
  • 设置客户端值不会更新ViewState。 您必须为ViewState设置样式服务器端以存储它。 或者,您可以: 回发后重新运行JavaScript。 将样式存储在cookie中并使用JavaScript恢复样式。 查找JavaScript库以在客户端修改ViewState (不推荐)。 Setting a value client-side does not update ViewState. You have to set the style server-side for ViewState to st ...
  • 你没有遗漏任何东西,你的评估是正确的。 ASP.NET表不会将其内容保存为查看状态。 我假设至少部分原因是一个表可以包含任何数量的任何类型的数据,并且在某些情况下可能真的开始添加到视图状态的大小。 无论哪种方式,这就是他们的工作方式。 如果页面上有回发,则需要在每个加载事件上重新填充表,或者将表数据存储为自己查看状态,然后从数据中重新填充表。 You're not missing anything, your assessment is correct. ASP.NET tables do not save ...
  • 假设视图状态的大小是“缓慢”的主要原因,我想建议你从更全面的方法来处理它。 你有n个服务器控件,你是否需要所有'n'号码作为服务器控件,而不仅仅是简单的HTML? 假设你真的需要所有'n',他们都需要启用viewstate吗? 这里有一篇很好的文章(如果你还没有阅读过),它提供了更多的见解: VIEWSTATE size minimization Assuming the size of viewstate is the main cause of the 'slowness', I would like ...
  • 你不需要。 检查MVC框架 。 它消除了ViewState并作为旧的ASP(至少从这个角度来看)。 You don't have to. Check out MVC framework. It eliminates ViewState and works as old ASP (at least from this point of view).
  • 对于ViewState是什么和做什么的所有描述,这一切都归结为这是一件事...... ViewState是一堆数据,序列化,base-64编码,并填充到input type="hidden"页面上的元素。 每次将页面呈现给客户端时,ViewState都会发送到客户端。 每次客户端将表单发布到服务器时,ViewState都会发送到服务器。 因此,如果ViewState包含例如200KB的数据,则每次请求都会回送200KB。 200KB的隐藏信息,在页面渲染中看不见 。 可能不重要的数据。 (基本上,残酷。) ...
  • 我能想到的唯一原因是你在页面加载事件中设置了一些默认值..比如... protected void Page_Load(object sender, EventArgs e) { SitePrefDropDownList.SelectedValue = "Proffesional"; } 在页面生命周期中触发SitePrefDropDownList_SelectedIndexChanged事件之前, SitePrefDropDownList_SelectedIndexChanged调用Page_l ...
  • 这里的问题是jquery对话框正在从表单中删除你的DIV。 这是一个可以帮助您的链接。 jQuery模态窗口从我的表单中删除元素 干杯 The problem here is that jquery dialog is removing your DIV from inside your form. Here is a link that should help you. jQuery modal window removes elements from my form Cheers

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)