如何选择随机的36个数?我的代码错在哪里?(50分)

  • 主题发起人 主题发起人 荒草地
  • 开始时间 开始时间

荒草地

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
k,i,x,y,tmp:integer;
m : array[1..7] of integer;
label label1;
begin
label1:
for x := 1 to 7 do
begin
m[x] := 0;
end;
tmp := 0;
Randomize;
for x := 1 to 7 do
begin
sleep(x);
m[x] := Random(36);
if x > 1 then
begin
for i := 1 to (x-1) do
begin
if (m[x] = m) then
begin
goto label1;
end;
end;
end;
end;

for x := 1 to 7 do
begin
for y := 1 to (7-x) do
begin
if m[y] > m[y+1] then
tmp := m[y];
m[y] := m[y+1];
m[y+1] := tmp;
end;
end;
Label2.Caption := IntToStr(m[1]);
Label3.Caption := IntToStr(m[2]);
Label4.Caption := IntToStr(m[3]);
Label5.Caption := IntToStr(m[4]);
Label6.Caption := IntToStr(m[5]);
Label7.Caption := IntToStr(m[6]);
Label8.Caption := IntToStr(m[7]);


end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Label2.Caption := IntToStr(0);
Label3.Caption := IntToStr(0);
Label4.Caption := IntToStr(0);
Label5.Caption := IntToStr(0);
Label6.Caption := IntToStr(0);
Label7.Caption := IntToStr(0);
Label8.Caption := IntToStr(0);

end;

end.

得到的数字为何一直存在重复的?费解
 
procedure TForm1.Button1Click(Sender: TObject);
var
Count, i, n: Integer;
m: array[1..7] of integer;
begin
Count := 0;
Randomize;
for i := 1 to 7 do m := -1;
while Count < 7 do begin
n := Random(36);
for i := 1 to Count do if m = n then Break;
if i <= Count then Continue;
Count := Count + 1;
m[Count] := n;
end;
Edit1.Text := '';
for i := 1 to 7 do Edit1.Text := Edit1.Text + IntToStr(m) + ',';
end;
 
你的意思是把1-36之间的数字顺序打乱吧。 你这个作法不高明啊。 你可以从1循环到36,
每次随机取一个数, 然后mod 35 , 然后把1放到随即取到的位置, 如果该位置存在,则1不动。 循环。。。
 
procedure TForm1.Button2Click(Sender: TObject);
var
Count, i, n: Integer;
e: array[0..35] of integer;
begin
Edit1.Text := '';
Count := 0;
Randomize;
for i := 0 to 35 do e := i;
while Count < 7 do begin
n := Random(36);
if e[n] < 0 then Continue;
Count := Count + 1;
Edit1.Text := Edit1.Text + IntToStr(e[n]) + ',';
e[n] := -1;
end;
end;
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
B_Start: TButton;
Memo1: TMemo;
Timer1: TTimer;
B_Stop: TButton;
procedure B_StartClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure B_StopClick(Sender: TObject);
private
{ Private declarations }
procedure s_cal;
public
{ Public declarations }
end;

var
Form1: TForm1;
mb:array [1..36] of byte;
mt:byte;

implementation

{$R *.dfm}

procedure TForm1.B_StartClick(Sender: TObject);
var s:byte;
begin
Memo1.ScrollBars:=ssBoth;
for s:=1 to 36 do mb:=s
// 初始化
mt:=0;
s_cal;
Timer1.Interval:=50;
Timer1.Enabled := true;
end;

procedure TForm1.s_cal;
var sw:word
s1,s2,s3:byte;
begin
sw:=Random($7fff);
repeat // 打乱顺序
s1:=1+Random(36);
s2:=1+Random(36);
s3:=mb[s1];
mb[s1]:=mb[s2];
mb[s2]:=s3;
dec(sw);
until sw=0;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
s_cal;
end;

procedure TForm1.B_StopClick(Sender: TObject);
var s1:byte
ss:string;
begin
Timer1.Enabled := False;
ss:='';
for s1:=1 to 36 do ss:=ss+Format('%3d',[mb[s1]])
// 全输出
Memo1.LInes.Add(ss);
end;

end.
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Count, i, n: Integer;
m: array[1..7] of integer;
begin
Count := 0;
Randomize;
for i := 1 to 7 do m := -1;
while Count < 7 do begin
n := Random(36);
for i := 1 to Count do if m = n then Break;
if i <= Count then Continue;
Count := Count + 1;
m[Count] := n;
end;
Edit1.Text := '';
for i := 1 to 7 do Edit1.Text := Edit1.Text + IntToStr(m) + ',';
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Count, i, n: Integer;
e: array[0..35] of integer;
begin
Edit1.Text := '';
Count := 0;
Randomize;
for i := 0 to 35 do e := i;
while Count < 7 do begin
n := Random(36);
if e[n] < 0 then Continue;
Count := Count + 1;
Edit1.Text := Edit1.Text + IntToStr(e[n]) + ',';
e[n] := -1;
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
i, n1,n2,n3: Integer;
e: array[0..35] of integer;
begin
for i := 0 to 35 do e := i;
Randomize;
for i := 0 to 1000 do begin
n1 := Random(36);
n2 := Random(36);
n3 := e[n1];
e[n1] := e[n2];
e[n2] := n3;
end;
Edit1.Text := '';
for i := 0 to 6 do
Edit1.Text := Edit1.Text + IntToStr(e) + ',';
end;

end.
 

Similar threads

后退
顶部