Delphi(2006) 循环帮助
所以我本质上想做的是让某些事情发生 70% 的时间,另外一些事情发生 10% 的时间,如果这有意义的话,但我的应用程序似乎没有执行我猜测的任何操作我误解了循环语法或其他东西,无论如何,如果有人可以看一下,也许可以给我一些建议
per1 := 70;
per2 := 77;
per3 := 84;
per4 := 91;
per5 := 100;
per6 := Random(2) + 1;
randomize;
RandPer:= Random(100);
randomize;
RandPer2 := Random(100);
if RandPer2 <= 70 then begin
If RandPer <= per1 then begin
Functiontest(1);
end Else If RandPer <= per2 then begin
Functiontest(3);
end Else begin If RandPer <= per3 then begin
Functiontest(5);
end Else begin If RandPer <= per4 then begin
Functiontest(6);
end Else begin If RandPer <= per5 then begin
Functiontest(9);
end;
end;
end;
end;
So What I'm essentially trying to do is have something happen 70% of the time, another few things happen 10% of the time each if that makes sense but my app doesn't seem to do any of the actions I'm guessing I'm misunderstanding the loop syntax or something, anyway if anyone could take a look and maybe give me some advice
per1 := 70;
per2 := 77;
per3 := 84;
per4 := 91;
per5 := 100;
per6 := Random(2) + 1;
randomize;
RandPer:= Random(100);
randomize;
RandPer2 := Random(100);
if RandPer2 <= 70 then begin
If RandPer <= per1 then begin
Functiontest(1);
end Else If RandPer <= per2 then begin
Functiontest(3);
end Else begin If RandPer <= per3 then begin
Functiontest(5);
end Else begin If RandPer <= per4 then begin
Functiontest(6);
end Else begin If RandPer <= per5 then begin
Functiontest(9);
end;
end;
end;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有任何循环语法,因此这肯定是您感到困惑的可能根源。
不要多次调用
Randomize
。 每次您执行此操作时,它都会根据系统时钟重新初始化随机种子。 如果您的代码运行速度快于时钟前进的速度,那么您对 Randomize
的多次调用实际上会将随机种子重置为之前的相同值,从而导致重复随机调用返回相同的值。
帮助建议您在程序开始时仅调用一次
Randomize
。 如果您正在编写一个单元或组件并且您不负责整个程序,那么根本不要调用Randomize
。 相反,记录代码的使用者应该自己调用它。如果您正在编写 DLL 并且不使用运行时包,请在 DLL 导出的初始化函数中调用Randomize; 您的 DLL 的使用者将无法访问您的 DLL 的 Delphi 运行时库副本。
另外,如果您希望某些事情在 70% 的情况下发生,那么您应该检查您的值是否严格小于 70。
Random
的可能返回值包括零; 70% 的结果将在 0 到 69 之间(含 0 和 69)。 允许 70 实际上会使事件发生的概率为 71%。最后,你的 10% 时间的计算对我来说没有意义。 您有 3 个事件发生的概率为 7%,另外一个事件发生的概率为 9%。 当你只剩下 30% 的时间时,你不可能让四个事件发生的概率分别为 10%。 您的意思是每个事件的频率都独立于其他事件进行测量吗? 如果是这样,那么不要将所有条件测试与
else
链接在一起; 为每一个完全使用单独的if
语句。You don't have any loop syntax, so that's certainly a possible source of your confusion.
Do not call
Randomize
multiple times. It reinitializes the random seed each time you do, and that's based on the system clock. If your code runs faster than the clock advances, then your several calls toRandomize
will actually reset the random seed to the same value it had before, resulting in repeatedRandom
calls returning the same value.The help advises you to call
Randomize
just once at the start of your program. If you are writing a unit or component and you are not in charge of the whole program, then do not callRandomize
at all. Instead, document that consumers of your code should call it themselves.If you are writing a DLL and not using run-time packages, then call
Randomize
in an initialization function that your DLL exports; consumers of your DLL won't have access to your DLL's copy of the Delphi run-time library.Also, if you want something to happen 70 percent of the time, then you should check whether your value is strictly less than 70. The possible return values of
Random
include zero; 70 percent of the results will be between 0 and 69 inclusive. Allowing 70 will actually make the event happen 71 percent of the time.Finally, your calculations of 10 percent of the time don't make sense to me. You have three events that will happen 7 percent of the time, and one that will happen 9 percent of the time. You can't have four events that each happen 10 percent of the time when you only have 30 percent remaining. Do you mean for each event's frequency to be measured independently of the others? If so, then do not link all your conditional tests together with
else
; Use completely a separateif
statement for each one.我刚刚修改了 CharlesF 代码来完成您需要的操作。
希望CharlesF不会介意。
I just modified CharlesF code to do what you need.
Hope CharlesF won't mind.