[Notes2-team] =?iso-8859-1?b?UmU6W05vdGVzMi10ZWFtXSBQcukgLSBBTFBIQQ==?=
Brought to you by:
andersonrb
|
From: titanius <tit...@te...> - 2004-04-27 19:13:11
|
Pessoal, me desculpe usar a lista do Notes para isto, mas eh de extrema i=
mportancia.. como faco pra gerar uma matriz com conteudos randomicos de 0=
e 1.. ou seja...
A[1,0,0,1,1,0,1,0,1]...
tentei:
Random(2)
tentei a seguinte funcao:
Randomize;
if System.Random(9999) mod 2 =3D 0 then
Result :=3D 1
else
Result :=3D 0;
e nada me retorna a variavel toda, mas eh igual... o vetor tem sempre os =
mesmos valores.. estranho...
O problema e que precisaria disso hoje pra faculdade..
Mais uma vez pe=E7o desculpas por estar usando a lista do Notes, porem em=
nenhuma lista de discussao q participo,inclusive foruns.. soube me respo=
nder...
segue o codigo...
procedure TForm1.Button1Click(Sender: TObject);
// Funcao randomica de 0 e 1
function Randomiza: Byte;
var
x, Seed: integer;
begin
Randomize;
if Random(9999) mod 2 =3D 0 then
Result :=3D 1
else
Result :=3D 0;
end;
var
Base: array[0..19] of Real;
Populacao: array[0..199, 0..19] of ShortInt;
SomaPop: array[0..199] of Real;
cBase, bBase, i, j, LastMin, LastMax: integer;
TempSoma, ValLastMin, ValLastMax: Real;
s: string;
begin
// Limpa parte Visual
Memo1.Lines.Clear;
Memo2.Lines.Clear;
Memo3.Lines.Clear;
Randomize;
// Preenche a matriz Base
for i :=3D 0 to 19 do
begin
cBase :=3D Randomiza;
bBase :=3D RandomRange(20, 50);
if cBase <> 1 then
bBase :=3D bBase * -1;
Base[i] :=3D bBase;
end;
// Exibe a Matriz Base
for i :=3D 0 to 19 do
begin
s :=3D s + FloatToStr(Base[i]) + ' ';
end;
Memo1.Lines.Add(s);
//-------------------------------------------
TempSoma :=3D 0;
for i :=3D 0 to 199 do
begin
Randomize;
s :=3D '';
for j :=3D 0 to 19 do
begin
// Preenche a Populacao
Populacao[i, j] :=3D Randomiza;
s :=3D s + IntToStr(Populacao[i, j]) + ' ';
end;
end;
// Exibe na tela a matriz Populacao
ValLastMin :=3D 0;
ValLastMax :=3D 0;
for i :=3D 0 to 199 do
begin
TempSoma :=3D 0;
for j :=3D 0 to 19 do
begin
TempSoma :=3D TempSoma + (Base[j] * Populacao[i, j]);
end;
SomaPop[i] :=3D TempSoma;
if SomaPop[i] < LastMin then
begin
ValLastMin :=3D SomaPop[i];
LastMin :=3D i;
end;
if SomaPop[i] > LastMax then
begin
ValLastMax :=3D SomaPop[i];
LastMax :=3D i;
end;
if i < 10 then
Memo2.Lines.Add('0' + IntToStr(i) + '-> ' + s + ' SOMA -> ' + Flo=
atToStr(SomaPop[i]))
else
Memo2.Lines.Add(IntToStr(i) + '-> ' + s + ' SOMA -> ' + FloatToSt=
r(SomaPop[i]));
end;
Memo3.Lines.Add('Individuo Menos Adaptado:');
Memo3.Lines.Add(Memo2.Lines.Strings[LastMin]);
Memo3.Lines.Add('---------------------------');
Memo3.Lines.Add('Individuo Mais Adaptado:');
Memo3.Lines.Add(Memo2.Lines.Strings[LastMax]);
end; |