Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Randomize

Posted on 2003-11-01
10
Medium Priority
?
299 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 260 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Independent Software Vendors: 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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

886 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