- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有4个号码
a,b,c,d : integers
我需要为每个数字分配一个 2-7 之间的随机数,但所有四个数字的总和必须为 22
我该怎么做?
最佳答案
首先,我要澄清的是,如上所述,这个问题并没有唯一地定义问题。您要求随机抽样,但没有指定所需的样本分布。
当您实际上表示均匀分布时,却说随机,这是对数学术语的常见滥用。所以我假设这就是你的意思。具体来说,您希望所有可能的不同 4 个数字组具有相同的选择概率。实现这一目标的最简单、最有效的方法如下:
可能的不同集合的列表很小。我凭直觉猜想大概有 50 名候选人。
生成候选人列表非常简单。只需运行从 2 到 7 的三个嵌套 for 循环即可。这将为您提供前三个数字的组合。将它们相加,然后从 22 中减去,然后检查最终数字是否在范围内。
<小时/>既然你似乎喜欢看代码,这里有一个简单的演示:
{$APPTYPE CONSOLE}
uses
System.Math,
Generics.Collections;
type
TValue = record
a, b, c, d: Integer;
procedure Write;
end;
procedure TValue.Write;
begin
Writeln(a, ' ', b, ' ', c, ' ', d);
end;
var
Combinations: TArray<TValue>;
procedure InitialiseCombinations;
var
a, b, c, d: Integer;
Value: TValue;
List: TList<TValue>;
begin
List := TList<TValue>.Create;
try
for a := 2 to 7 do
for b := 2 to 7 do
for c := 2 to 7 do
begin
d := 22 - a - b - c;
if InRange(d, 2, 7) then
begin
Value.a := a;
Value.b := b;
Value.c := c;
Value.d := d;
List.Add(Value);
end;
end;
Combinations := List.ToArray;
finally
List.Free;
end;
end;
function GetSample: TValue;
begin
Result := Combinations[Random(Length(Combinations))];
end;
var
i: Integer;
begin
Randomize;
InitialiseCombinations;
for i := 1 to 25 do
GetSample.Write;
Readln;
end.
<小时/>
从检查中可以清楚地看出,该算法从可用值中均匀采样。
但是其他提出的算法呢?我们可以通过重复采样并计算每个可能样本产生的次数来执行粗略的启发式测试。这是:
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Math,
Generics.Collections;
type
TValue = record
a, b, c, d: Integer;
procedure Write;
class operator Equal(const lhs, rhs: TValue): Boolean;
end;
procedure TValue.Write;
begin
Writeln(a, ' ', b, ' ', c, ' ', d);
end;
class operator TValue.Equal(const lhs, rhs: TValue): Boolean;
begin
Result := (lhs.a=rhs.a) and (lhs.b=rhs.b) and (lhs.c=rhs.c) and (lhs.d=rhs.d);
end;
var
Combinations: TArray<TValue>;
procedure InitialiseCombinations;
var
a, b, c, d: Integer;
Value: TValue;
List: TList<TValue>;
begin
List := TList<TValue>.Create;
try
for a := 2 to 7 do
for b := 2 to 7 do
for c := 2 to 7 do
begin
d := 22 - a - b - c;
if InRange(d, 2, 7) then
begin
Value.a := a;
Value.b := b;
Value.c := c;
Value.d := d;
List.Add(Value);
end;
end;
Combinations := List.ToArray;
finally
List.Free;
end;
end;
function GetSampleHeffernan: TValue;
begin
Result := Combinations[Random(Length(Combinations))];
end;
function GetSampleVanDien: TValue;
const
TOTAL = 22;
VALUE_COUNT = 4;
MIN_VALUE = 2;
MAX_VALUE = 7;
var
Values: array[0..VALUE_COUNT-1] of Integer;
Shortage: Integer;
Candidates: TList<Integer>;
ValueIndex: Integer;
CandidateIndex: Integer;
begin
Assert(VALUE_COUNT * MAX_VALUE >= TOTAL, 'Total can never be reached!');
Assert(VALUE_COUNT * MIN_VALUE <= TOTAL, 'Total is always exceeded!');
Randomize;
Candidates := TList<Integer>.Create;
try
for ValueIndex := 0 to VALUE_COUNT-1 do
begin
Values[ValueIndex] := MIN_VALUE;
Candidates.Add(ValueIndex);
end;
Shortage := TOTAL - VALUE_COUNT * MIN_VALUE;
while Shortage > 0 do
begin
CandidateIndex := Random(Candidates.Count);
ValueIndex := Candidates[CandidateIndex];
Values[ValueIndex] := Values[ValueIndex] + 1;
if Values[ValueIndex] = MAX_VALUE then
Candidates.Remove(CandidateIndex);
Shortage := Shortage - 1;
end;
finally
Candidates.Free;
end;
Result.a := Values[0];
Result.b := Values[1];
Result.c := Values[2];
Result.d := Values[3];
end;
function GetSampleLama: TValue;
type
TRandomValues = array[1..4] of Integer;
var
IntSum: Integer;
Values: TRandomValues;
begin
// initialize a helper variable for calculating sum of the generated numbers
IntSum := 0;
// in the first step just generate a number in the range of 2 to 7 and store
// it to the first integer element
Values[1] := RandomRange(2, 7);
// and increment the sum value
IntSum := IntSum + Values[1];
// as the next step we need to generate number, but here we need also say in
// which range by the following rules to ensure we ever reach 22 (consider, if
// the 1st number was e.g. 3, then you can't generate the second number smaller
// than 5 because then even if the next two numbers would be max, you would get
// e.g. only 3 + 4 + 7 + 7 = 21, so just use this rule:
// Values[1] Values[2]
// 2 6..7
// 3 5..7
// 4 4..7
// 5 3..7
// 6..7 2..7
Values[2] := RandomRange(Max(2, 8 - Values[1]), 7);
// and increment the sum value
IntSum := IntSum + Values[2];
// if the third step we need to generate a value in the range of 15 to 20 since
// the fourth number can be still in the range of 2 to 7 which means that the sum
// after this step must be from 22-7 to 22-2 which is 15 to 20, so let's generate
// a number which will fit into this sum
Values[3] := RandomRange(Max(2, Min(7, 15 - IntSum)), Max(2, Min(7, 20 - IntSum)));
// and for the last number let's just take 22 and subtract the sum of all previous
// numbers
Values[4] := 22 - (IntSum + Values[3]);
Result.a := Values[1];
Result.b := Values[2];
Result.c := Values[3];
Result.d := Values[4];
end;
function IndexOf(const Value: TValue): Integer;
begin
for Result := 0 to high(Combinations) do
if Combinations[Result] = Value then
exit;
raise EAssertionFailed.Create('Invalid value');
end;
procedure CheckCounts(const Name: string; const GetSample: TFunc<TValue>);
const
N = 1000000;
var
i: Integer;
Counts: TArray<Integer>;
Range: Integer;
begin
SetLength(Counts, Length(Combinations));
for i := 1 to N do
inc(Counts[IndexOf(GetSample)]);
Range := MaxIntValue(Counts) - MinIntValue(Counts);
Writeln(Name);
Writeln(StringOfChar('-', Length(Name)));
Writeln(Format('Range = %d, N = %d', [Range, N]));
Writeln;
end;
begin
Randomize;
InitialiseCombinations;
CheckCounts('Heffernan', GetSampleHeffernan);
//CheckCounts('Van Dien', GetSampleVanDien);
CheckCounts('Lama', GetSampleLama);
Readln;
end.
一次特定运行的输出是:
Heffernan---------Range = 620, N = 1000000Lama----Range = 200192, N = 1000000
The Van Dien variant is commented out at the moment since it produces invalid values.
OK, I debugged and fixed the Van Dien variant. The test and results now look like this:
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Math,
Generics.Collections;
type
TValue = record
a, b, c, d: Integer;
procedure Write;
class operator Equal(const lhs, rhs: TValue): Boolean;
end;
procedure TValue.Write;
begin
Writeln(a, ' ', b, ' ', c, ' ', d);
end;
class operator TValue.Equal(const lhs, rhs: TValue): Boolean;
begin
Result := (lhs.a=rhs.a) and (lhs.b=rhs.b) and (lhs.c=rhs.c) and (lhs.d=rhs.d);
end;
var
Combinations: TArray<TValue>;
procedure InitialiseCombinations;
var
a, b, c, d: Integer;
Value: TValue;
List: TList<TValue>;
begin
List := TList<TValue>.Create;
try
for a := 2 to 7 do
for b := 2 to 7 do
for c := 2 to 7 do
begin
d := 22 - a - b - c;
if InRange(d, 2, 7) then
begin
Value.a := a;
Value.b := b;
Value.c := c;
Value.d := d;
List.Add(Value);
end;
end;
Combinations := List.ToArray;
finally
List.Free;
end;
end;
function GetSampleHeffernan: TValue;
begin
Result := Combinations[Random(Length(Combinations))];
end;
function GetSampleVanDien: TValue;
const
TOTAL = 22;
VALUE_COUNT = 4;
MIN_VALUE = 2;
MAX_VALUE = 7;
var
Values: array[0..VALUE_COUNT-1] of Integer;
Shortage: Integer;
Candidates: TList<Integer>;
ValueIndex: Integer;
CandidateIndex: Integer;
begin
Assert(VALUE_COUNT * MAX_VALUE >= TOTAL, 'Total can never be reached!');
Assert(VALUE_COUNT * MIN_VALUE <= TOTAL, 'Total is always exceeded!');
Candidates := TList<Integer>.Create;
try
for ValueIndex := 0 to VALUE_COUNT-1 do
begin
Values[ValueIndex] := MIN_VALUE;
Candidates.Add(ValueIndex);
end;
Shortage := TOTAL - VALUE_COUNT * MIN_VALUE;
while Shortage > 0 do
begin
CandidateIndex := Random(Candidates.Count);
ValueIndex := Candidates[CandidateIndex];
inc(Values[ValueIndex]);
if Values[ValueIndex] = MAX_VALUE then
Candidates.Delete(CandidateIndex);
dec(Shortage);
end;
finally
Candidates.Free;
end;
Result.a := Values[0];
Result.b := Values[1];
Result.c := Values[2];
Result.d := Values[3];
end;
function GetSampleLama: TValue;
type
TRandomValues = array[1..4] of Integer;
var
IntSum: Integer;
Values: TRandomValues;
begin
// initialize a helper variable for calculating sum of the generated numbers
IntSum := 0;
// in the first step just generate a number in the range of 2 to 7 and store
// it to the first integer element
Values[1] := RandomRange(2, 7);
// and increment the sum value
IntSum := IntSum + Values[1];
// as the next step we need to generate number, but here we need also say in
// which range by the following rules to ensure we ever reach 22 (consider, if
// the 1st number was e.g. 3, then you can't generate the second number smaller
// than 5 because then even if the next two numbers would be max, you would get
// e.g. only 3 + 4 + 7 + 7 = 21, so just use this rule:
// Values[1] Values[2]
// 2 6..7
// 3 5..7
// 4 4..7
// 5 3..7
// 6..7 2..7
Values[2] := RandomRange(Max(2, 8 - Values[1]), 7);
// and increment the sum value
IntSum := IntSum + Values[2];
// if the third step we need to generate a value in the range of 15 to 20 since
// the fourth number can be still in the range of 2 to 7 which means that the sum
// after this step must be from 22-7 to 22-2 which is 15 to 20, so let's generate
// a number which will fit into this sum
Values[3] := RandomRange(Max(2, Min(7, 15 - IntSum)), Max(2, Min(7, 20 - IntSum)));
// and for the last number let's just take 22 and subtract the sum of all previous
// numbers
Values[4] := 22 - (IntSum + Values[3]);
Result.a := Values[1];
Result.b := Values[2];
Result.c := Values[3];
Result.d := Values[4];
end;
function IndexOf(const Value: TValue): Integer;
begin
for Result := 0 to high(Combinations) do
if Combinations[Result] = Value then
exit;
raise EAssertionFailed.Create('Invalid value');
end;
procedure CheckCounts(const Name: string; const GetSample: TFunc<TValue>);
const
N = 1000000;
var
i: Integer;
Counts: TArray<Integer>;
Range: Integer;
begin
SetLength(Counts, Length(Combinations));
for i := 1 to N do
inc(Counts[IndexOf(GetSample)]);
Range := MaxIntValue(Counts) - MinIntValue(Counts);
Writeln(Name);
Writeln(StringOfChar('-', Length(Name)));
Writeln(Format('Range = %d, N = %d', [Range, N]));
Writeln;
end;
begin
Randomize;
InitialiseCombinations;
CheckCounts('Heffernan', GetSampleHeffernan);
CheckCounts('Van Dien', GetSampleVanDien);
CheckCounts('Lama', GetSampleLama);
Readln;
end.
Heffernan---------Range = 599, N = 1000000Van Dien--------Range = 19443, N = 1000000Lama----Range = 199739, N = 1000000<小时/>
为了让大家明白,这里有一些不同分布的经验概率质量函数图:
<小时/>
好的,现在我修复了@TLama 的代码。它正在使用 RandomRange
错误地。 documentation状态:
RandomRange returns a random integer from the range that extends between AFrom and ATo (non-inclusive).
关键是范围被定义为闭开区间。返回值的范围是[AFrom..ATo),或者用不等号表示,AFrom <= Value < ATo。
但是@TLama的代码是在区间两端闭合的假设下编写的。因此,可以通过在每次调用 RandomRange
的第二个参数中添加 1 来轻松修复代码。当我们这样做时,输出如下所示:
Heffernan---------Range = 587, N = 1000000Van Dien--------Range = 19425, N = 1000000Lama----Range = 79320, N = 1000000
经验 PMF 图变为:
<小时/>
所有这一切的底线是,如果您关心分布,则很难获得正确的采样。
关于delphi - 总数相等的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19465347/
+--------+-------+----------+-----------+ | Maker | Model | SeatType | NoOfSeats | +--------+------
如何使用 jQuery 计算 p 标签之间的字符数? 我尝试: DEMO html: 1 1 1 js: var tBytes = 0, tFiles = $('b').length; fo
在 MongoDB 上运行正常的“查找”查询时,我可以通过在返回的游标上运行“计数”来获得总结果计数(不考虑限制)。因此,即使我将结果集限制为 10(例如),我仍然可以知道结果总数为 53(再次,例如
在 100% 堆叠条形图中,如何让数据标签同时显示值和总百分比?示例:129 (60.3%) 当您将鼠标悬停在栏上时,它会显示在工具提示中,但在栏本身上不可见。 此处示例:https://docs.g
我在Kibana中的总和有问题。 我的用例是,我的每个服务器都会定期报告打开的 session 数。在Kibana中,我想可视化所有服务器上所有 session 的总数。但是,即使只有一台服务器联机且
我正在使用 jQuery 和 ASP.NET MVC 3 以及 razor View 引擎。 我有几个可以在其中输入数值的文本框。我有一个标签控件,其中包含由 jQuery 计算的文本框总数。 我有以
像这样的结果: 75 Ansari 5 10 88 Koodoo 4 0 90 Koodoo 14 0 83 Koodoo 5 0
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我是 PHP 的初学者,我正在为我的网站编写一些代码。我想获得当时处于事件状态的 session 总数。我知道这是一项艰巨的任务,但有可能。我该怎么做? 我google了一下,有人说可以通过统计tem
1。问题陈述 我很难在正确的记录行中显示 COUNT() 的总数。 如何将 COUNT() 显示到正确的相应服务 2。背景 我想根据stage_id 和分解到project_name 显示员工负责的项
我整个下午都在尝试处理一个(或两个或三个)查询,以便获得三个表的所有子表的计数。看看我的设计: 用户表 id_user | name 1 | foo 2 | bar 获奖表 id_won | user
我有以下脚本。想要文件夹、子文件夹和文件的数量: Sub CountFiles(ByVal path1 As String) Dim fso As Object Dim subfolder As Ob
我对 c3.js 中的饼图有疑问。 如何在标题中添加饼图的总数? var title = new Array('data1.sql','data2.sql') var dtitle = new Arr
我在这方面玩得很开心。我正在尝试针对具有递归关系(分层)的表编写查询(使用 Oracle),并获取存储在树中每个节点及其下方的另一个表中的记录总数。另一个表只有与叶节点相关的记录。但是,我想获得树中每
有没有办法获取模块在任何时间点使用的绑定(bind)总数(通过模板的 {{ .. }}/ng-xxx="..." 、 $scope.$watch(...) 等)? 最佳答案 使用 document.g
我有一个非常简单的表格,因为我现在真的只是在玩 RoR,只是收集一些数据并将其插入数据库,没有什么令人兴奋的只是基本的 CRUD。但是,我想在表格的页脚中放置一个总和字段,但我在网上找不到任何接近的东
这个 mysql 查询给出了我的产品的销售数量(total 和total_staff),按一天中的天数和小时数分组。我想要每个产品的 total 和 total_staff 的总和(不按任何内容分组,
我正在尝试计算 For 循环中每个 user_name 赢得的总金额,并将其显示在 Amount Won: 之后。但是,当我运行下面的代码时,赢得金额后没有任何显示: - 它完全是空白的。我什至尝试将
我有 3 个表。产品价格、开票产品和订购产品的表格。我正在尝试创建一个连接这些的 View 。我想输出产品价格以及开票产品总数和订购产品总数。 产品价格 id season_id product
例如,我在另一个查询的 while 循环内的查询中有一个 mysql_num_rows 结果为 4,8,15,16,23,42。我的问题是如何计算 while 循环中的所有结果? (共 133 个)谢
我是一名优秀的程序员,十分优秀!