I need help in filling array in Pascal with random number.

I have 9 empty array ( A: array[1..10] of integer ), and i want to fill the array with integer for 1 to 9 randomly, but one number can only be choosen one time. So when a number (eg. 5) was choosen for A[1], then the number should not be choosen again for A[2]..A[9].

Any one can help please!
Thnk you.

Recommended Answers

All 4 Replies

Hi
This code create random numbers without repeating between 1 and 9 and insert them into an array

Program RandomArray;
Var
   A:Array[1..9] Of Integer;
   I,j,N:Integer;
   Found:Boolean;
Begin
     Randomize;
     For I:=1 To 9 Do
     Begin
          N:=Random(9)+1;
          Found:=True;
          While Found Do
          Begin
              Found:=False;
              J:=1;
              While (J<I) And (Not Found) Do
              Begin
                   If A[J]=N Then
                      Found:=True;
                   J:=J+1;
              End;
              If Found Then
                 N:=Random(9)+1;
          End;
          A[I]:=N;
     End;
     For I:=1 To 9 Do
         WriteLn(a[I]);
     ReadLn;
End.
commented: giving away free help!!!! +0

thanks, thats help me alot.

Very ineficient... there is a much more efficient way to do that:

Randomize;
For I:=1 To 9 Do 
Begin
     A[I]:=I; // First put them in order
End;
For I:=1 To NumberOfIterationsYouWantButAtLeastNine Do 
Begin // Now do permutations, so re-order in a random order
     N:=Random(9)+1;
     Auxiliar:=A[1];
     A[1]:=A[N];
     A[N]:=Auxiliar;
End;

The concept for having an array filled with random numbers but no repeating any, for optimal perfonmance, is first fill it with ordered numbers, then do random permutations with same element or random element... see the next is dual random:

Randomize;
For I:=1 To 9 Do 
Begin
     A[I]:=I; // First put them in order
End;
For I:=1 To NumberOfIterationsYouWantButAtLeastfive Do 
Begin // Now do permutations, so re-order in a random order
     N1:=Random(9)+1;
     N2:=Random(9)+1;
     Auxiliar:=A[N1];
     A[N1]:=A[N2];
     A[N2]:=Auxiliar;
End;

This is not the top most efficient, there is also a much more efficient way: Use a TList as auxiliar (Integers can be saved inside as pointers), see sample:

Var
   A: Array[1..9] Of Integer;
   Ordered:TList;
Begin
     Ordered:=TList.Create;
     For I:=1 To 9 Do // Here indexes indicates range for integer values wanted to be choosen, not for positions
     Begin
          Ordered.Add(TObject(I)); // First put them in order in one array
     End;
     For I:=1 To 9 Do // Here indexes indicates positions on the array and can be different from previous loop
     Begin
          N:=Random(Ordered.Count); // Select a random index
          A[I]:=Integer(Ordered.Items[N]); // Now take the number stored at that random index
          Ordered.Delete(N); // Now remove that element so can not be taken again, for letting elements to be repeatted just comment this line
     End;
End;

This is te top most efficent way to get an array (of any size) filled with Integers in a range, with or without acepting elements to be repeated.

Hope helps, hope the idea can be understand: first fill in order in an auxiliar one, then take randonly one after another as much as you need and if not want to be repeated, just delete them.

Ensure there will be enough if not comment the .Delete line...

This example solves also the next:
-I need an array of a million numbers from range 13 to 123, but randomly choosen
But also this other one:
-I need an array of a million numbers randomly choosen from a list the user input, they are not inside a range... for example: get a million numbres randomly from list (1,6,13,16,22,1123,123,56)... just change the first for loop to something like:

Ordered.Add(TObject(1));
Ordered.Add(TObject(6));
Ordered.Add(TObject(13));
Ordered.Add(TObject(16));
Ordered.Add(TObject(22));
Ordered.Add(TObject(1123));
Ordered.Add(TObject(123));
Ordered.Add(TObject(56));
...

And you are done it efficiently.

Hope the idea is understand: Put desired numbers in a TList, then take randomly from it (deleting them after or not depends on your wish to have duplicates or not).

Cheers.

Ok, i know i'm a little late ?? but I tried my best to write the most efficient code i could think of.
i hope someone will find it usefull.

program unu;
uses crt;
type vek=array[1..10] of integer;
var a:vek;
    n,i,m:integer;

function num(x:integer):boolean;
var j:integer;
begin
    num:=false;
    j:=1;
    while not num and (j<=n) do
    begin
            if x=a[j] then num:=true;
            j:=j+1;
    end;
end;

begin
randomize;
    write('n=');
    readln(n);
    i:=0;
    while i<=n do
    begin
            m:=random(10)+1;
            if not num(m) then
            begin
                    i:=i+1;
                    a[i]:=m;
            end;
    end;
    for i:=1 to n do write(a[i],' ');
    readln;
end.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.