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.