Ver Mensaje Individual
  #46  
Antiguo 09-05-2018
Avatar de danielmj
danielmj danielmj is offline
Miembro
 
Registrado: jun 2011
Posts: 383
Reputación: 13
danielmj Va por buen camino
Gracias ecfisa y bucanero, miraré vuestro codigo sin falta por que ya no sé que mas hacer (lmitación de conocimientos siendo honesto)

En este momento, tengo este codigo para generar los aleatorios no repeditos

en una unidad aparte, uso esta funcion para generar los aleatorios no repetidos:

Código Delphi [-]
unit RandomArray; //fuente: https://www.experts-exchange.com/que...f-numbers.html

{$MODE Delphi}

interface

uses LCLIntf, LCLType, LMessages;

type
  TIntegerArray = array[0..32760*2] of Integer;
  PIntegerArray = ^TIntegerArray;

function CompRandArray(var IntArrayPtr: PIntegerArray; Count, LowRange, HighRange: integer): boolean;

implementation

function CompRandArray(var IntArrayPtr: PIntegerArray; Count, LowRange, HighRange: integer): boolean;
var
  I, J: integer;
  HaveDup: boolean;
begin
  Result := True;
  if (HighRange - LowRange + 1) < (Count) then
  begin
    Result := False;
    Exit;
  end;
  Randomize;
  for I := 0 to Count - 1 do
  begin
    IntArrayPtr^[i] := LowRange + Random(HighRange - LowRange + 1);
    Repeat
    HaveDup := False;
    for J := 0 to I - 1 do
    begin
      if (IntArrayPtr^[J] = IntArrayPtr^[i]) and (I <> J) then
      begin
        IntArrayPtr^[i] := LowRange + Random(HighRange - LowRange + 1);
        HaveDup := True;
        break;
      end;
    end;
    Until HaveDup = False;
  end;
end;

end.

en la unidad principal tengo esto...

Código Delphi [-]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Grids, RandomArray;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    grid: TStringGrid;
    Label1: TLabel;
    lista: TListView;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  RandomArray: PIntegerArray; //This is a pointer type to Dynamic array
  i, Ctr, Count, Low, High: integer;

  begin
    Count := 6; //fill with 20 items
    Low := 1; //Lowest Random Number
    High := 49; //Highest Random Number
    GetMem(RandomArray, Count*SizeOf(Integer)); //Allocate memory
    try
      ListBox1.Clear;
      if CompRandArray(RandomArray, Count, Low, High) then
        For Ctr := 0 to Count - 1 do
        //if false Something went wrong
        ListBox1.Items.Add(IntToStr(RandomArray^[Ctr])); //dont forget ^
    finally
      FreeMem(RandomArray, Count*SizeOf(Integer)); //DeAllocate
    end;
  end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i,j: integer;

begin
  with Lista.Items.Add do
      begin
        for i:= 0 to listBox1.Items.Count -1 do
        SubItems.Add(listBox1.Items.Strings[i]);
      end;
  button1.Click;
end;

end.

Bien, cada vez que pulso el button1, se genera en el listbox1 una combinacion de 6 numeros aleatorios sin repeticion y si pulso el button2, mete el contenido del listbox1 en el listview y hace una llamada a button1.
Y aqui viene mi problema por mucho que coloco for y while... no consigo rellenar el listview con por ejemplo 50, 100, 140 combinaciones de 6 numeros aleatorios que me entrege la funcion, o mejor dicho, lo rellena pero salvo la primera fila, el resto siempre son la misma combinacion.

Ejemplo:
3 5 7 12 45 32
23 4 12 41 33 13
23 4 12 41 33 13
23 4 12 41 33 13
23 4 12 41 33 13
...

Saludos y muchas gracias por vuestro tiempo, ayuda y paciencia.
__________________
La juventud pasa, la inmadurez se supera, la ignorancia se cura con la educación, y la embriaguez con la sobriedad, pero la estupidez dura para siempre. Aristofanes.
Responder Con Cita