再加100分,请高人指点!! (150分)

不会吧,可能是你粘贴时没有弄好,下面是完整的:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function RandomSelectString(iCount: integer;
SomeString: string): string;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function TForm1.RandomSelectString(iCount: integer
SomeString : string): string;
var
sl : TStringList;
str : string;
iRand,i : integer;
begin
sl :=TStringList.create;
//根据iCount的值取出SomeString中的字符串
for i :=0 to iCount-1 do
begin
if i<iCount-1 then
str :=copy(someString,1,pos(',',SomeString)-1)
else
str :=SomeString;
sl.add(str);
if i<iCount-1 then
begin
system.delete(someString,1,pos(',',SomeString));
end;
end;
//利用随机数来求出随机字符串
iCount :=sl.count;
Randomize;
iRand :=Random(iCount);
Result :=sl.Strings[iRand];
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
showMessage(RandomSelectString(6,'1白猫,2黑猫,3野猫,4波斯猫,5黄鼠锒,6老虎'));
end;

end.

 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function RandomSelectString(iCount: integer;
SomeString: string): string;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function RandomSelectString(iCount: integer
SomeString : string): string;
var
sl : TStringList;
str : string;
iRand,i : integer;
begin
sl :=TStringList.create;
for i :=0 to iCount-1 do
begin
if i<iCount-1 then
str :=copy(someString,1,pos(',',SomeString)-1)
else
str :=SomeString;
sl.add(str);
if i<iCount-1 then
begin
system.delete(someString,1,pos(',',SomeString));
end;end;
iCount :=sl.count;
Randomize;
iRand :=Random(iCount);
Result :=sl.Strings[iRand];
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption:=RandomSelectString(5,'狮子,老虎,土狼,豺狗,猎豹');
label2.Caption:=RandomSelectString(6,'野牛,斑马,羚羊,角马,梅花鹿,野猪');
label3.Caption:=RandomSelectString(2,'晴天,雨天');end;
end.

 
to dok001:
  你的那个函数代码与我的有什么不同吗?
 
再加50分,请正确点评一下,20分钟结贴
经编译验证,dok001和小唐都能通过,但小唐的函数在前面有说明,而dok001无说明,孰优孰劣,有无大的影响,请高人指点,另送50分!
 
哇塞,好复杂。小小的事情嘛。以下是正解:
function TakeString(const Strings: array of string): string;
begin
Result := Strings[Random(1000000) mod Succ(High(Strings))];
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Caption := TakeString(['一', '二', '三', '四', '五',
'六', '七', '八', '九', '十']);
end;

initialization
Randomize;
end.
 
barton:你更厉害,不过你的最后两句什么意思,指点一下!!
initialization
Randomize;
 
初始化随机种子呀。要不然每次就不那么随机了呀。当然,只需要运行一次就够了。
 
initialization
Randomize;
这两句放在程序什么位置最好?
 
不好意思没有及时结帐,这个话题越谈越有意思,有质量的回帖可以继续加分,让大家又学知识又挣分!!
 
//initialization
//Randomize;
//这两句放在程序什么位置最好?
“end.”之前,其他之后

我前面的回答也是用的这个方法呀,你看都不看一眼:(

type
TArrStr = array of string;
var
S1: array [1..5] of string =
('狮子','老虎','土狼','豺狗','猎豹');
...
function RandomSelect(var ArrStr: TArrStr): string;
begin
Result := ArrStr[Random(Length(ArrStr)) + 1];
end;
 
是是,基本一致,我只是改改。
1.加上const会省一些;
2.用足够大的数然后取模分布更好一些;
3.取数组长度的方法,Length是函数,而High基本是只是一个宏。
 
//1.加上const会省一些;
那我加 var,一样省:)

//2.用足够大的数然后取模分布更好一些;
速度慢一点点:)

//3.取数组长度的方法,Length是函数,而High基本是只是一个宏。
比我快一点:)


我那都是说给楼主看的:)
 
呵呵, 是啊, 我的代码连提都没提, 好伤心啊....
 
哈哈哈哈!笑死我了!
对不起了tseug,没看见,没看见。惭愧惭愧!
 
只要会用
Randomize ;//重置随机数发生器序列;

Random(n); // 随机产生一个0至n-1之间的整数(包括0和n-1)
就可以了
 
tseug 大叔,我理解您的心情[:D]
 
[:(], 是不是明月那个小丫头跟你说我是他大伯的?! 没想到我已经很老了。。。。[:(]
 
beta副帖已得46分;这里分5分;
cococo副帖已得90分;这里分5分;
其他人的分叫我分的头疼.再次感谢!!
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
884
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
734
import
I
顶部