Solved

Randomize

Posted on 2003-11-01
10
295 Views
Last Modified: 2010-04-05
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?
0
Comment
Question by:herd_bone
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 2

Accepted Solution

by:
DeNavigator earned 65 total points
ID: 9662392
You could use a TStringList in stead of an array. The limit of the TStringList is only the size of memory in the computer. (or 2GigaByte whichever comes first :) )
Then to choose one randomly do:

Procedure TForm1.Create(Sender: TObject);
begin
// add these lines
  Randomize;
  MyStringList := TStringList.Create;
// fill the list with your strings like:
  MyStringList.Add('AString');
end;

Procedure TForm1.Button1Click(Sender: TObejct);
var
  Number: Integer;
  MyChoice: Integer;
begin
  Number := MyStringList.Count;
  MyChoice := Random(Number);
  Label1.Caption := MyStringList.Strings[MyChoice];
end;
0
 
LVL 4

Expert Comment

by:nestorua
ID: 9662412
HI,
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.
0
 
LVL 2

Expert Comment

by:DeNavigator
ID: 9662443
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(Random(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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:herd_bone
ID: 9662505
like this?  MyStringList.Add('AString',+'anotherstring');
or
  MyStringList.Add('AString');
  MyStringList.Add('AnotherString');

i need to setup like 6 or 7 strings and randomly grab one of them.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 9662509
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.
0
 
LVL 2

Expert Comment

by:DeNavigator
ID: 9663570
herd_bone:
For each string you want to add, you execute the Add-method of TStringList.

  MyStringList.Add('String1');
  MyStringList.Add('String2');
etc.
0
 
LVL 3

Expert Comment

by:fibdev
ID: 9664187
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=http://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:TObject);
var Ini : TIniFile;
     I, N : Integer;
     URL: String;
begin
  Ini := TIniFile.Create('.\MyIni.ini');  // ".\" 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 :)
0
 
LVL 34

Expert Comment

by:Slick812
ID: 9665055
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(aryStr1))];
end;
0
 

Author Comment

by:herd_bone
ID: 9665223
Ok, maby i need something different, can someone setup an array like: 'sex',+'drugs',+'rockNroll';
and then message box one of these words randomly?
0
 

Author Comment

by:herd_bone
ID: 9665629
10-4 DeNavigator, It works perfect, disregard the above comment.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question