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, then the number should not be choosen again for A..A.
Any one can help please!
Edited by mukti itb: more specific
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.
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; A:=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).
i've got code like this
naglibog.CommandText = "UPDATE ptransaction " & _ " SET Quantityt = " & mm & _ ", Partial_totalt='" & jj & "'" & _ " ...
Good day community,
I have completed vb.net program (a school timetabling program) and I am in the process to create a website. At the moment word -press on wamp server ...
I am looking for log archival utility or approach which should be able to help me in based on below policy.
(1) whenever log file (system.log) size >30859 or file ...