Board index » delphi » Taking list and outputing random list

Taking list and outputing random list

i want to take a list of phone numbers in the following format

615-555-0431
615-555-0765
615-555-7513
616-554-6543
615-654-4625

and output them in a random order without duplication..

code would be greatly appreciated for this non-commercial application it
will not be sold or given away. thanks in advance

Please email and post here to make sure i get it....

 

Re:Taking list and outputing random list


In article <34556ECF.5...@compu.net> of Mon, 27 Oct 1997 22:49:19 in
comp.lang.pascal.misc, Bill Larson <blar...@compu.net> wrote:

Quote
>i want to take a list of phone numbers in the following format

>615-555-0431
>615-555-0765
>615-555-7513
>616-554-6543
>615-654-4625

>and output them in a random order without duplication..

>code would be greatly appreciated for this non-commercial application it
>will not be sold or given away. thanks in advance

>Please email and post here to make sure i get it....

QHAH.   See  http://www.merlyn.demon.co.uk/pascal.htm#Rand

for J := Max downto 1 do Swap(A[J], A[Succ(Random(J))]) ;

or

  for J := Max downto 2 do begin K := Succ(Random(J)) ;
    if J<>K then begin p := @A[J] ; q := @A[K] ;
      t := p^ ; p^ := q^ ; q^ := t end end ;

Of course, for efficiency, you don't work with the numbers, but with
pointers to them - OTOH with only 12-char numbers the added overhead may
exceed the gain.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ - FAQqish topics, acronyms and links.

Re:Taking list and outputing random list


Quote
Bill Larson <blar...@compu.net> wrote:
>i want to take a list of phone numbers in the following format
>615-555-0431
>615-555-0765
>615-555-7513
>616-554-6543
>615-654-4625
>and output them in a random order without duplication..
>code would be greatly appreciated for this non-commercial application it
>will not be sold or given away. thanks in advance
>Please email and post here to make sure i get it....

******** LIST EXPANDED AND PLACED IN A TEXTFILE TELNUM.TXT. *******

615-555-0431
615-555-0765
615-555-7513
616-554-6543
615-654-4625
615-555-0123
615-555-1234
615-555-2345
616-554-3456
615-654-4567

================== Program begins ================  

Program RandList;
{Turbo v6.0  <clifp...@airmail.net>  Oct 28, 1997 }

CONST  fsource = 'TELNUM.TXT';  {change both constants to suit}
       maxnum = 100;

TYPE
{declaration of user types allows use of them as procedure parameters}

s12 = String[12];    {telephone number string length}
NumList = Array[1..maxnum] of s12;  {array of telephone numbers}
TelNumIndex = Array[1..maxnum] of Word;   {index is always positive}

VAR
f:TEXT;        {telephone number strings stored in textfile}
TNum:NumList;
indx:TelNumIndex;
NumIn:s12;            {single number str from textfile}
m, tally:Word;
response:String[1];

Procedure RandomizeList(VAR i:TelNumIndex; ct:Word);
{scrambles indexes to NumList array}
VAR j, k, n, tmp:Word;
Begin
     For n := 1 to 2 * ct + 3 Do    {arbitrary repeats}
     Begin
         j := Random(ct) + 1;  {range = 1 to number of telephone nums}
          Repeat
                k := Random(ct) + 1;
          Until j <> k;

          tmp := i[j];    {swap array indexes, TNum array unchanged}
          i[j] := i[k];
          i[k] := tmp;
     End; {indx array now stores random indexes to the TNum array}
End;

Procedure ShowList(TelNum:NumList; i:TelNumIndex; ct:Word);
VAR  n:Word;
Begin
     Writeln;
     For n := 1 to ct Do
     Begin
          Writeln(TelNum[ i[n] ]);
          If n mod 23 = 0 then  {for long lists}
          Begin
               Write('   <<< Press Enter >>>');
               Readln;   Writeln;
          End;
     End;
End;

Begin  {main program}
     response[1] := ' ';  {to allow unabiguous <Enter> response}
     RANDOMIZE;
     Assign(f, fsource);
     Reset(f);

     tally := 0;
     While not eof(f) Do
     Begin
          Inc(tally);
          Readln(f, Tnum[tally]); {readin array of tel num strings}
     End;
     Close(f);

     For m := 1 to tally Do indx[m] := m;  {initialize indx array}

     REPEAT
          RandomizeList(indx, tally);
          ShowList(Tnum, indx, tally);
          Write('<Enter> to repeat, Q to quit: ');
          ReadLn(response);
     UNTIL UpCase(response[1]) = 'Q';

     Writeln('ORIGINAL ORDER WAS:');
     For m := 1 to tally Do indx[m] := m;   {restore indx order}
     ShowList(Tnum, indx, tally);

     Writeln;   Write('   <<< Press Enter >>>');  Readln;
End.

Re:Taking list and outputing random list


In article <1146699EAD63F4DA.B4185E366848F0A8.4BC5DC4ED08DD2FA@library-
proxy.airnews.net> of Tue, 28 Oct 1997 22:37:34 in
comp.lang.pascal.misc, CLIF PENN <clifp...@airmail.net> wrote:

Quote
>Procedure RandomizeList(VAR i:TelNumIndex; ct:Word);
>{scrambles indexes to NumList array}
>VAR j, k, n, tmp:Word;
>Begin
>     For n := 1 to 2 * ct + 3 Do    {arbitrary repeats}
>     Begin
>         j := Random(ct) + 1;  {range = 1 to number of telephone nums}
>          Repeat
>                k := Random(ct) + 1;
>          Until j <> k;

>          tmp := i[j];    {swap array indexes, TNum array unchanged}
>          i[j] := i[k];
>          i[k] := tmp;
>     End; {indx array now stores random indexes to the TNum array}
>End;

The aim is to generate a re-ordered list, with all possible outcomes
being of equal probability (which the method I earlier posted to this
thread will do).

There are ct-factorial possible orderings (presuming no duplicate
numbers).

You suggest calling Random(ct), which has ct possible outcomes, a
probable total of (1+(ct/(ct-1)))*(2*ct+3) times [ the j<>k causes the
probably (ct/(ct-1)) ], so your code (considering Random to be ideal)
has a probable total of ct**((1+(ct/(ct-1)))*(2*ct+3)) possible and
equally probable paths.  But ct-factorial is probably not a factor of
this number; therefore your method _cannot_ generate all possible
orderings with equal probability.  I think - the j<>k is a complication.

See  http://www.merlyn.demon.co.uk/pascal.htm#Rand  for where I got this
idea from (Brandt).

The method I posted uses calls Random(ct), Random(ct-1), ..., Random(2);
and therefore has only the minimum necessary number of paths,
ct-factorial.

Now ct**((1+(ct/(ct-1)))*(2*ct+3)) >> ct-factorial; so it is certainly
most probable that your code can generate all orders, and I expect with
roughly equal probability; but your code is SLOW in comparison with the
simple optimum.  Also, it seems prejudiced against NOT swapping a pair,
so its chances of retaining part or all of the original order are even
less than they ought to be.

This problem in essence was all discussed more than once not long ago in
c.l.p.* - that's where I got the proper way from, AFAIR !!  PKUATB.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is a line comprising "-- ".   Year 2000 is leap.
  My list of Critical Dates is : http://www.merlyn.demon.co.uk/miscinfo.htm#CDs

Re:Taking list and outputing random list


Dr John Stockton <j...@merlyn.demon.co.uk> wrote:

Quote
>The aim is to generate a re-ordered list, with all possible outcomes
>being of equal probability (which the method I earlier posted to this
>thread will do).

The posting said that he wished to output the list, non-duplicated in
random order, which is exactly what the program I posted did also.
Each number does occur in a random order (as random as the random
number generator allows). There is more than one way to skin a cat.

Re:Taking list and outputing random list


Bill Larson <blar...@compu.net> wrote in article
<34556ECF.5...@compu.net>...

Quote
> i want to take a list of phone numbers in the following format

> 615-555-0431
> 615-555-0765
> 615-555-7513
> 616-554-6543
> 615-654-4625

> and output them in a random order without duplication..

> code would be greatly appreciated for this non-commercial application it
> will not be sold or given away. thanks in advance

Hmm.. Looks like you want to randomize a list of numbers for a war-dialler
to call , the point of randomizing being that the phone companies generally
pick up on sequential dialling. Am i right? ;-)

--
-NK
www.digitalin.com

Other Threads