uses Math; {RandomRange}var
i, previous, current: Integer;
begin
previous := Ord('A');
edt1.Text := '';
Randomize;
for i := 0 to 99 do
begin
// 随机字母
current := RandomRange(ord('A'), ord('Z') + 1);
// 不允许出现连续的 H
if (current = previous) and (current = ord('H')) then
Continue;
previous := current;
edit1.Text := edit1.Text + chr(current);
end;
end;
i, previous, current: Integer;
begin
previous := Ord('A');
edt1.Text := '';
Randomize;
for i := 0 to 99 do
begin
// 随机字母
current := RandomRange(ord('A'), ord('Z') + 1);
// 不允许出现连续的 H
if (current = previous) and (current = ord('H')) then
Continue;
previous := current;
edit1.Text := edit1.Text + chr(current);
end;
end;
当发现出现这个特定字符时,就去掉这个字符后面的字一次.
var
i, iCounter: Integer;
ch, preCh : char;
begin
memo1.Clear; Randomize;
iCounter := 0;
preCh := ' ';
for i := 0 to 100 do
begin
ch := Chr(Random(26) + 65); if preCh = 'H' then
begin
memo1.Lines.Add(ch + '------------------------');
inc(iCounter);
end
else
memo1.Lines.Add(ch + ',' + ch);
preCh := ch;
end;
memo1.Lines.Add(format('H出现%d次', [iCounter]));
end;