Solved

Randomize

Posted on 2003-11-01
10
291 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 33

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now