Delphi – A little help with a delphi loop

delphi

 for m := 0 to 300 do
       if Pos(certain_String, string(arrayitem(m)) <> 0 then
        begin
          randomize;
          x := random(100);
        begin
            case x of
             0..69  : function(m); //  70 percent
            70..79 : function(m+1); // 10 percent
            80..84 : function(m+2); // 5 percent
            85..88 : function(m+3); //  5 percent
            89..91 : function(m+4); //  2 percent
            92..94 : function(m+5); //  3 percent
            95..97 : function(m+6); //  3 percent
            98..98 : function(m+7); //  1 percent
            99..99 : function(m+8); //  1 percent


           end;
           m := 300;

    end;

Trying to make this thing loop through a rather large array, but if it finds a certain string in it stop entirely and go to the next thing, had to take out some proprietary function names but m := 300; seems to be the problem, doesn't like me assigning m a value in the middle of a for loop. Sorry for the lack of knowledge here, got a halfdone pascal project dropped on my lap and have never even seen the stuff before.

Best Solution

Changed the code a bit:

randomize;
for m := 0 to 300 do begin
  if Pos(certain_String, string(arrayitem(m)) <> 0 then begin
    x := random(100);
    case x of
       0..69 : function(m); //  70 percent
      70..79 : function(m+1); // 10 percent
      80..84 : function(m+2); // 5 percent
      85..88 : function(m+3); //  5 percent
      89..91 : function(m+4); //  2 percent
      92..94 : function(m+5); //  3 percent
      95..97 : function(m+6); //  3 percent
          98 : function(m+7); //  1 percent
          99 : function(m+8); //  1 percent
    end;
    break;
  end;
end;

Will do the trick.

Randomize should not be called each loop.

And why the string cast?