0

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.

*Edited
by mukti itb*: more specific

This Question has been **Answered**

-1

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.
```

0

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.

This question has already been answered. Start a new discussion instead.

Recommended Topics

I need your expertise on vb.net.

I have two data grid with database on their own. I want to copy/update the data from one database to ...

I want to write out a DXF file with line arc and maybe spline.

My issue is documentation; While I have looked at AutoCAD's site I feel that there isnt ...