Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.