Board index » delphi » Easy *Random* Reserve word Question

Easy *Random* Reserve word Question

I'm having a problem with the random function for D1.

Here is the complete source code. The Random function does not work
and I cannot figure out why? I have (2) buttons and a group box. The objective
of the random line is to generate A-Z characters randomly and display those
characters in the group list box. The first button does this. The second box
sorts the characters A-Z in that order.

The random line does not work cause? It appears that random needs
a property, so if you add RANDOM.DATA(26) it's appears ok, but it
does not work.

I'm not sure what I am doing wrong, the code comes for a book *QUE*.
This is the main reason why I hate typing in programs. They look good
on payer and once you type them in... standby for the darn thing to fail.

unit Random;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    List: TGroupBox;
    Scramble: TButton;
    Button2: TButton;
    procedure ScrambleClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
Var

Data: Array[1..30] of char;

procedure TForm1.ScrambleClick(Sender: TObject);
Var
I: Integer;
Data: Array[1..30] of char;
test: integer;
data2: integer;
begin
Randomize;
List.Caption:= ' ';
For I:=1 to 30 do
begin

     DATA[I]:= chr(Random(30)+65);
{Here is the line above that is causing trouble}

     List.Caption:=List.Caption + Data[i]+ ' ';
     update;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
Var
I,J,LoW, Index: Integer;
Hold : Char;
Begin
     For I:=1 to 29 do
     begin
     Low:=I;
     For j:=i + 1 to 30 do
     If Data[j] < data[low] then low := j;
     If I<>low then
     begin
     hold :=data[i];
     data[i]:=data[low];
     data[low]:=hold;
     list.caption:=' ';
     for index:=1 to 30 do
     begin
     list.caption:=list.caption+data[index]+' ';
     update;
     end;
     end;

end;

end;
END.

 

Re:Easy *Random* Reserve word Question


Quote
>unit Random;

>interface

>uses
>  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
>  Forms, Dialogs, StdCtrls;

>type
>  TForm1 = class(TForm)
>    List: TGroupBox;
>    Scramble: TButton;
>    Button2: TButton;
>    procedure ScrambleClick(Sender: TObject);
>    procedure Button2Click(Sender: TObject);
>  private
>    { Private declarations }
>  public
>    { Public declarations }
>  end;

>var
>  Form1: TForm1;

>implementation

>{$R *.DFM}
>Var

>Data: Array[1..30] of char;

>procedure TForm1.ScrambleClick(Sender: TObject);
>Var
>I: Integer;
>     {Data: Array[1..30] of char;}   {don't use local var}
>test: integer;
>data2: integer;
>begin
>   {Randomize;}   {call only once for application (in OnCreate)}
>List.Caption:= ' ';
>For I:=1 to 30 do
>begin

>     DATA[I]:= chr(Random(30)+65);
>{Here is the line above that is causing trouble}

>     List.Caption:=List.Caption + Data[i]+ ' ';
>     update;
>end;
>end;

Chris

Re:Easy *Random* Reserve word Question


Quote
PBBS90210 wrote:
> I'm having a problem with the random function for D1.

[...]

Quote
> unit Random;

[...]

Quote
>      DATA[I]:= chr(Random(30)+65);
> {Here is the line above that is causing trouble}

Not astonishing. I suppose the compiler is complaining because Random is already
the name of the unit.

Re:Easy *Random* Reserve word Question


Quote
pbbs90...@aol.com (PBBS90210) wrote:
>I'm having a problem with the random function for D1.

>Here is the complete source code. The Random function does not work
>and I cannot figure out why? I have (2) buttons and a group box. The objective
>of the random line is to generate A-Z characters randomly and display those
>characters in the group list box. The first button does this. The second box
>sorts the characters A-Z in that order.

>The random line does not work cause? It appears that random needs
>a property, so if you add RANDOM.DATA(26) it's appears ok, but it
>does not work.

>I'm not sure what I am doing wrong, the code comes for a book *QUE*.
>This is the main reason why I hate typing in programs. They look good
>on payer and once you type them in... standby for the darn thing to fail.

You have two problems.  First, you should not call your unit "Random".
Choose another name to avoid a conflict.

Second, you should NOT call "Randomize" every time you click the button.
Call "Randomize" exactly once in your application, in FormCreate or some
other one-time function.  "Randomize" resets the random seed to the current
time.  If you happen to click the button twice rapidly, you'll get EXACTLY
the same sequence.

The random number generator is only "random" when it gets to generate a
sequence.  Interfering with the sequence by forcing the seed with
"Randomize" makes it considerably LESS random.
--
- Tim Roberts, t...@probo.com
  Providenza & Boekelheide, Inc.

Other Threads