Results 1 to 8 of 8

Thread: 10% change to get Dagger of the Mighty Traveler

  1. #1

    10% change to get Dagger of the Mighty Traveler

    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
    Code:
    procedure TForm1.Button1Click(Sender: TObject);
    var x,number : integer;
        test : array[1..10] of integer;
    begin
       randomize;
       for x:= 1 to 10 do
         test[x]:=0;
    
       memo1.lines.clear;
       for x:= 1 to 100 do
       begin
         number := random(100);
         case number of
            1..10: test[1]:=test[1]+1;
           11..20: test[2]:=test[2]+1;
           21..30: test[3]:=test[3]+1;
           31..40: test[4]:=test[4]+1;
           41..50: test[5]:=test[5]+1;
           51..60: test[6]:=test[6]+1;
           61..70: test[7]:=test[7]+1;
           71..80: test[8]:=test[8]+1;
           81..90: test[9]:=test[9]+1;
          91..100: test[10]:=test[10]+1;
        end;
       end;
       for x := 1 to 10 do
       begin
          memo1.lines.add('numbers between '+inttostr(1+(10*(x-1)))+' and '+inttostr(x*10)+' ='+inttostr(test[x]));
       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:

  2. #2

    10% change to get Dagger of the Mighty Traveler

    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

  3. #3
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    10% change to get Dagger of the Mighty Traveler

    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.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  4. #4

    10% change to get Dagger of the Mighty Traveler

    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.
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  5. #5
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    10% change to get Dagger of the Mighty Traveler

    Code:
    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
        Memo1.Lines.Add&#40;IntToStr&#40;I&#41;+' &#58; '+IntToStr&#40;NumArr&#91;I&#93;&#41;+
           ' = '+Format&#40;'%2.2f',&#91;&#40;NumArr&#91;I&#93;/1000000&#41;*100&#93;&#41;+'%'&#41;;
    end;
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  6. #6

    10% change to get Dagger of the Mighty Traveler

    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:

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

    [/pascal]

    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.
    My DGDev forum pascal syntax highlight settings:
    <br />[background=#FFFFFF][comment=#8080FF][normal=#000080]
    <br />[number=#C00000][reserved=#000000][string=#00C000]

  7. #7

    10% change to get Dagger of the Mighty Traveler

    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

  8. #8

    10% change to get Dagger of the Mighty Traveler

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •