stuck on number generation - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

stuck on number generation

don0098don0098 Posts: 1Member
I'm trying to generate random sets of numbers from 1~9. I've sucessfully got 2 sets of numbers and in the format i wanted output. the output so far looks something like:

3_8____4_9____4_5
7_6____4_6____8_2
2_5____1_4____4_2
...
...

_ <= it's just spaces

What i want to do is out put to sets of 6. so something like

3_5_7_8_2_4____2_5_7_9_3_6____1_5_7_2_4_8
...
...

Can anyone help? I think I went the wrong way of solving this from the beginning.

Here's the code:

[code]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;

type
TForm1 = class(TForm)
private
{ private declarations }
public
{ public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.lfm}
const C_NUMS = 9;
C_GROUPS = 3;
C_COMBIS = (C_NUMS * (C_NUMS-1)) div 2;
var nums : array[1..C_NUMS] of integer;
combi : array[1..C_COMBIS] of TPoint;
i,j,l : integer;
s : string;
begin
// Sample user input: 1..9
for i := 1 to C_NUMS do
nums[ i ] := i;

// Set combinations
l := 1;
for i := 1 to C_NUMS-1 do
for j := i+1 to C_NUMS do
begin
combi[ l ] := Point(nums[ i ], nums[ j ]);
inc(l)
end;

// Generate output
s := '';
for i := 1 to C_COMBIS do
begin
repeat
j := random(C_COMBIS) + 1
until combi[ j ].x >= 0;
if random(2) = 1 then
s := format('%s%d %d ',[s, combi[ j ].x, combi[ j ].y])
else
s := format('%s%d %d ',[s, combi[ j ].y, combi[ j ].x]);
combi[j].x := -1;
if (i mod C_GROUPS) = 0 then
s := s + #13#10
end;

// Show result
ShowMessage(s);


end.
[/code]
Sign In or Register to comment.