Delphi(2006) 循环帮助

发布于 2024-07-10 13:05:11 字数 750 浏览 11 评论 0原文

所以我本质上想做的是让某些事情发生 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2024-07-17 13:05:11

您没有任何循环语法,因此这肯定是您感到困惑的可能根源。

不要多次调用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 to Randomize will actually reset the random seed to the same value it had before, resulting in repeated Random 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 call Randomize 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 separate if statement for each one.

挖个坑埋了你 2024-07-17 13:05:11

我刚刚修改了 CharlesF 代码来完成您需要的操作。
希望CharlesF不会介意。

begin
  randomize;
  for i := 0 to NumberOfTimesNeed do
  begin
    R :=  Random(100);
    case R of 
       0..69  : Functiontest(1); // this will fire 70% of NumberofTimes
       70..79 : Funciotntest(2); // 10 percent 
       80..89 : Funciotntest(3); // 10 percent 
       90..94 : Funciotntest(4); //  5 percent  
       // and so on ...
    end; 
end;

I just modified CharlesF code to do what you need.
Hope CharlesF won't mind.

begin
  randomize;
  for i := 0 to NumberOfTimesNeed do
  begin
    R :=  Random(100);
    case R of 
       0..69  : Functiontest(1); // this will fire 70% of NumberofTimes
       70..79 : Funciotntest(2); // 10 percent 
       80..89 : Funciotntest(3); // 10 percent 
       90..94 : Funciotntest(4); //  5 percent  
       // and so on ...
    end; 
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文