herd_bone
asked on
Randomize
Hi, i need to setup an array of strings , or words , and then i need to randomly pick one of them , how do i do that? and how many strings can i put in array?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No it is absolutely not necessary to use randomize more then once in your program. The only thing it does, is seeding the random generator.
Try the following.
procedure TForm1.Button1Click(Sender : TObject)
var
I: Integer;
begin
Memo1.Clear;
for I := 1 to 25 do
Memo1.Lines.Add(IntToStr(R andom(1000 )));
end;
run this program and note the result. Close program and run it again. You will see the result is the same. Then add
procedure TForm1.Create(Sender: TObject);
begin
Randomize;
end;
When you now run the program, the result will differ each time you run it.
Try the following.
procedure TForm1.Button1Click(Sender
var
I: Integer;
begin
Memo1.Clear;
for I := 1 to 25 do
Memo1.Lines.Add(IntToStr(R
end;
run this program and note the result. Close program and run it again. You will see the result is the same. Then add
procedure TForm1.Create(Sender: TObject);
begin
Randomize;
end;
When you now run the program, the result will differ each time you run it.
ASKER
like this? MyStringList.Add('AString' ,+'another string');
or
MyStringList.Add('AString' );
MyStringList.Add('AnotherS tring');
i need to setup like 6 or 7 strings and randomly grab one of them.
or
MyStringList.Add('AString'
MyStringList.Add('AnotherS
i need to setup like 6 or 7 strings and randomly grab one of them.
You don't really how many times and how many buttons are there on the form where we are going to select randomly strings or words. In your specific example you are right.
herd_bone:
For each string you want to add, you execute the Add-method of TStringList.
MyStringList.Add('String1' );
MyStringList.Add('String2' );
etc.
For each string you want to add, you execute the Add-method of TStringList.
MyStringList.Add('String1'
MyStringList.Add('String2'
etc.
Here is one way I've used in the past...
First I have an ini file like this...
========================== ========== ========
[NumOfURLs]
Num=5 // Be careful here because 0 is a number so you really have 6 unless you are specific in your code
[URLS]
1=http://fibdev.com
2=https://www.experts-exchange.com
3=http://www.blah.com
4=http://www.planet-source-code.com
5=http://www.whateverelse.com
// You can also use a static string list but I did it this way so I could update the list via my web server through a call in my application.
Code to pick one at random
procedure MyProcedureClick(Sender:TO bject);
var Ini : TIniFile;
I, N : Integer;
URL: String;
begin
Ini := TIniFile.Create('.\MyIni.i ni'); // ".\" overload to application directory
try
N := Ini.readinteger('NumOfUrls ', 'Num', 1);
// Get URL from definition list
Randomize;
I := Random(N);
Url := Ini.ReadString('urls', IntToStr(I), 'http://fibdev.com');
// Use the url here....
end;
finally
ini.free;
end;
been a long day, pardon any syntax errors please :)
First I have an ini file like this...
==========================
[NumOfURLs]
Num=5 // Be careful here because 0 is a number so you really have 6 unless you are specific in your code
[URLS]
1=http://fibdev.com
2=https://www.experts-exchange.com
3=http://www.blah.com
4=http://www.planet-source-code.com
5=http://www.whateverelse.com
// You can also use a static string list but I did it this way so I could update the list via my web server through a call in my application.
Code to pick one at random
procedure MyProcedureClick(Sender:TO
var Ini : TIniFile;
I, N : Integer;
URL: String;
begin
Ini := TIniFile.Create('.\MyIni.i
try
N := Ini.readinteger('NumOfUrls
// Get URL from definition list
Randomize;
I := Random(N);
Url := Ini.ReadString('urls', IntToStr(I), 'http://fibdev.com');
// Use the url here....
end;
finally
ini.free;
end;
been a long day, pardon any syntax errors please :)
with an array - -
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;
procedure TForm1.Button1Click(Sender : TObject);
var
aryStr1: Array[0..16] of String;
i: Integer;
begin
for i := 0 to High(aryStr1) do
aryStr1[i] := 'String '+IntToStr(i);
// the next line will get a Random number betwee 0 and 15 and this will be the index of the string arry to go into Edit1
Edit1.Text := aryStr1[Random(High(aryStr 1))];
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;
procedure TForm1.Button1Click(Sender
var
aryStr1: Array[0..16] of String;
i: Integer;
begin
for i := 0 to High(aryStr1) do
aryStr1[i] := 'String '+IntToStr(i);
// the next line will get a Random number betwee 0 and 15 and this will be the index of the string arry to go into Edit1
Edit1.Text := aryStr1[Random(High(aryStr
end;
ASKER
Ok, maby i need something different, can someone setup an array like: 'sex',+'drugs',+'rockNroll ';
and then message box one of these words randomly?
and then message box one of these words randomly?
ASKER
10-4 DeNavigator, It works perfect, disregard the above comment.
I think you must use Randomize procedure more often than ones in FormCreate procedure.
As for words (not strings) you could use TIntegerList and the same as you do with TStringList.
Sincerely,
Nestorua.