随机概率选择(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
最满意答案
你没有遗漏任何东西,你的评估是正确的。 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