Solved

Randomize

Posted on 2003-11-01
10
297 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
[X]
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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, left to enroll

617 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