Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Randomize

Posted on 2003-11-01
10
294 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

860 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