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;
-
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. -
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;
Charles Faiga : I like the modifications you made, the code is a lot more readable Craig Stuntz : Don't call Randomize here; it's wrong. Do it in the initialization section.
0 comments:
Post a Comment