Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4098
Number of posts: 14002

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
stuck on number generation Posted by don0098 on 15 May 2011 at 2:13 PM
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:

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.           



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.