PDA

View Full Version : 10% change to get Dagger of the Mighty Traveler

Traveler
13-03-2003, 08:28 AM
If you've ever played a RPG, this might sound familiar.
But my the question is: How is this 10% calculated?

At first it was my guess to use a random number between 1 and 100.
If the result is between 1 and 10 then I get the sword.

The following snippet however, seems to proof that this methode is not correct

procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
var x,number &#58; integer;
test &#58; array&#91;1..10&#93; of integer;
begin
randomize;
for x&#58;= 1 to 10 do
test&#91;x&#93;&#58;=0;

memo1.lines.clear;
for x&#58;= 1 to 100 do
begin
number &#58;= random&#40;100&#41;;
case number of
1..10&#58; test&#91;1&#93;&#58;=test&#91;1&#93;+1;
11..20&#58; test&#91;2&#93;&#58;=test&#91;2&#93;+1;
21..30&#58; test&#91;3&#93;&#58;=test&#91;3&#93;+1;
31..40&#58; test&#91;4&#93;&#58;=test&#91;4&#93;+1;
41..50&#58; test&#91;5&#93;&#58;=test&#91;5&#93;+1;
51..60&#58; test&#91;6&#93;&#58;=test&#91;6&#93;+1;
61..70&#58; test&#91;7&#93;&#58;=test&#91;7&#93;+1;
71..80&#58; test&#91;8&#93;&#58;=test&#91;8&#93;+1;
81..90&#58; test&#91;9&#93;&#58;=test&#91;9&#93;+1;
91..100&#58; test&#91;10&#93;&#58;=test&#91;10&#93;+1;
end;
end;
for x &#58;= 1 to 10 do
begin
memo1.lines.add&#40;'numbers between '+inttostr&#40;1+&#40;10*&#40;x-1&#41;&#41;&#41;+' and '+inttostr&#40;x*10&#41;+' ='+inttostr&#40;test&#91;x&#93;&#41;&#41;;
end;
end;

In some cases only 5 numbers out of 100 are < 10, in others, as much as 17 numbers were below 10.

So this is clearly not the way to do this....
But it's the best I can think of at the moment.

Any other ideas to get the dagger within the correct percentage? :roll:

Eriken
13-03-2003, 10:28 AM
Percentage isn't exactly an exact science so your results are correct. In theory 100 of 100 could still get the dagger, even though the percentage of that happening would be extremely low.

The question is: what do you need?

If you want 10% chance of getting the dagger the calculations you've made will do. If you want a more exact 1 out of 10 people will get the dagger, you should maybe make a random number between 1 and 10 and if it's number 6, the 6th person around will get it. And then do it again when the 10 first have passed.

Many solutions, which depends on what you really want :-)
A 10% chance will create variable results like you've got.
_____
Eriken

cairnswm
13-03-2003, 11:55 AM
A simple bit of statistics is needed here. If you have a statistic sample of 100 your chance of error is relativly high. If you run the same code for 1000000 your deviation from the expected 10% will be proportinatly less (probablt between 9.9 and 10.1%)

PS. I think your calculation is wrong as Random * 100 will give a number between 0 and 99 not 1 and 100.

Alimonster
13-03-2003, 12:15 PM
Yep, as cairnswm said, you need more samples to have a more accurate set of results.

To slightly hijack this thread: has anyone tried out the Mersenne Twister random number generator? If not, it might be interesting to see if/how the deviation and speed compares to Delphi's standard one.

cairnswm
13-03-2003, 12:18 PM
procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
Var
NumArr &#58; Array&#91;1..10&#93; of Integer;
I &#58; Integer;
begin
For I &#58;= 1 to 10 do
NumArr&#91;I&#93; &#58;= 0;
For I &#58;= 1 to 1000000 do
InC&#40;NumArr&#91;Trunc&#40;Random*10&#41;+1&#93;&#41;;
For I &#58;= 1 to 10 do
' = '+Format&#40;'%2.2f',&#91;&#40;NumArr&#91;I&#93;/1000000&#41;*100&#93;&#41;+'%'&#41;;
end;

Xorcist
14-03-2003, 02:06 AM
Um... what exactly are you guys trying to do? I don't see why you'd need so much code to generate a dagger given a probability of 10%. Seeing as Random returns a floating point number between zero and one (0 <= Random < 1) wouldn't it be as easy as:

[background=#FFFFFF][normal=#000080][number=#C00000][string=#00C000][comment=#8080FF][reserved=#000000]
if (Random < 0.10) then begin
//give player dagger;
end;

P.S. Traveler, you shouldn't call Randomize every time you call Random. It should be called only once upon the start of your program. Otherwise you're basically reseting the seed value, which will lead to uncertain calculations.

Traveler
14-03-2003, 08:39 AM
Thanks for all the replies...

If you replace the dagger for the gifts found in my game Pop the balloon you know why I asked this question...

The game still uses my old formula, but I'm surely going to change this with the solutions given in here.... thanx again :D

Nebula
15-03-2003, 03:05 AM
Why not decrease the percentage each time you make the check?

first try 1 out of 10
second 1 out of 9
third 1 out of 8
last = give dagger

This way you can be sure the dagger will be given in 10 checks and still be random. After the tenth check reset the value to its original