```
procedure TForm1.Button1Click(Sender: TObject);
var
A:array[1..30] of integer;
i:integer;
begin
Randomize;
for i:= 1 to 30 do
A[i] := Random(100);
for i:= 1 to 30 do
Showmessage('Element '+IntToStr(i)+ ' = '+IntToStr(A[i]));
end;
```

Solved

Posted on 2011-05-01

I want to create an array with 30 random numbers between 0 and 100 and print to screen. How do I go about it? (new to programming)

16 Comments

```
procedure TForm1.Button1Click(Sender: TObject);
var
A:array[1..30] of integer;
i:integer;
begin
Randomize;
for i:= 1 to 30 do
A[i] := Random(100);
for i:= 1 to 30 do
Showmessage('Element '+IntToStr(i)+ ' = '+IntToStr(A[i]));
end;
```

```
procedure TForm1.Button1Click(Sender: TObject);
var
A:array[1..30] of integer;
i:integer;
begin
Randomize;
for i:= 1 to 30 do
A[i] := Random(100);
for i:= 1 to 30 do
ListBox1.Items.Add('Element '+IntToStr(i)+ '= '+IntToStr(A[i]));
end;
```

Article 1.

Article 2.

For the random numbers we use the Random function. Randomize is called first to initialize the random number generator.

Also as per Delphi help Random(x) will return any number in the range [0 and x-1] so in your case since you need a random number in the range 0 and 100, you can use Random(101):

So update line 8 to be:

```
A[i] := Random(101); // or 1 + Random(100)
```

Here is a simple example:

Listbox---Array.zip

Listbox---Array.zip

```
unit fMinMax;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfrmMinMax = class(TForm)
edArrayfill: TEdit;
btnFill: TButton;
lblMin: TLabel;
lblMax: TLabel;
lsbResult: TListBox;
private
{ Private declarations }
arDigits: array[1..30] of integer;
procedure FillArray;
//function GetMin: integer;
//function GetMax: integer;
public
{ Public declarations }
end;
var
frmMinMax: TfrmMinMax;
implementation
{$R *.dfm}
procedure TfrmMinMax.FillArray;
var i:integer;
begin
randomize;
for i:=1 to 30 do
arDigits[i] := Random (101);
for i:= 1 to 30 do
lsbResult.Items.Add('Element '+IntToStr(i)+ '= '+IntToStr(arDigits[i]));
end;
end.
```

```
unit fMinMax;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfrmMinMax = class(TForm)
edArrayfill: TEdit;
btnFill: TButton;
lblMin: TLabel;
lblMax: TLabel;
lsbResult: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
arDigits: array[1..30] of integer;
procedure FillArray;
//function GetMin: integer;
//function GetMax: integer;
public
{ Public declarations }
end;
var
frmMinMax: TfrmMinMax;
implementation
{$R *.dfm}
procedure TfrmMinMax.FillArray;
var i:integer;
begin
randomize;
for i:=1 to 30 do
arDigits[i] := Random (101);
for i:= 1 to 30 do
lsbResult.Items.Add('Element '+IntToStr(i)+ '= '+IntToStr(arDigits[i]));
end;
procedure TfrmMinMax.FormCreate(Sender: TObject);
begin
FillArray;
end;
end.
```

```
procedure TfrmMinMax.Button1Click(Sender: TObject);
begin
FillArray;
end;
```

Element 1= 61, Element 2= 53, Element 3= 52

Can you explain what you want to do exactly (I mean what is your idea or your requirement)?

Edit1.Text := IntToStr(A[x]);

The above line will load only one element (element number x), but if you want to show all the array's elements in that Editbox then:

```
for i:= 1 to 30 do
Edit1.Text := Edit1.Text + IntToStr(A[i]);
```

Also you can add separators:

```
for i:= 1 to 30 do
if i < 30 then
Edit1.Text := Edit1.Text + IntToStr(A[i]) + ' '
else
Edit1.Text := Edit1.Text + IntToStr(A[i]);
```

```
function AMax(Arr:array of integer):integer;
var
i:integer;
begin
result := Arr[low(Arr)];
for i := low(Arr) to high(Arr) do
begin
if Arr[i] > result then result := Arr[i];
end;
end;
function AMin(Arr:array of integer):integer;
var
i:integer;
begin
result := Arr[low(Arr)];
for i := low(Arr) to high(Arr) do
begin
if Arr[i] < result then result := Arr[i];
end;
end;
```

Then you can use them as follows:

Edit1.Text := IntToStr(AMax(arDigits)); //as per your last code snippet "arDigits" is your Array

Edit2.Text := IntToStr(AMin(arDigits));

Title | # Comments | Views | Activity |
---|---|---|---|

Firewall issues | 6 | 82 | |

Virtuailstring tree compare node issue | 14 | 78 | |

Delphi: how to send PJL commands to printer | 3 | 73 | |

Delphi selector screen | 2 | 45 |

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

Connect with top rated Experts

**14** Experts available now in Live!