Recent

Author Topic: [SOLVED] Vector, Array, Randomize and the missing 1  (Read 1109 times)

Slyde

  • Full Member
  • ***
  • Posts: 152
[SOLVED] Vector, Array, Randomize and the missing 1
« on: December 01, 2020, 08:20:04 pm »
I got some help here the other day, randomizing an array.  It worked great.  But after re-thinking the situation, I saw it wouldn't do exactly what I needed it to do.  So I augmented the code by adding a vector and changing some of the other code.  It works...but it doesn't.  When it randomizes now, I can't ever get 1.  It's always a 0.  Everything else is right.  And no matter what I do, the number 1 always shows up as a 0. 

Code: Pascal  [Select][+][-]
  1. var
  2.   setb: set of Byte = [];
  3.   idx: Byte;
  4.   a, i, j : Integer;
  5.   v : iVector;
  6.   arr : array[1..20] of Integer;
  7. begin
  8. {------------------------------------------------------------------------------
  9.         LET'S STORE THE IDs IN A VECTOR AND GET A RECORD COUNT
  10. -------------------------------------------------------------------------------}
  11.   v := iVector.Create;
  12.   a := 0;
  13.   Memo1.Clear;
  14.   Memo2.Clear;
  15.   ZQ1.Close;
  16.   ZQ1.SQL.Text := 'SELECT amc_id FROM adultMultipleChoice';
  17.   ZQ1.Open;
  18.   ZQ1.First;
  19.   while not ZQ1.EOF do
  20.   begin
  21.     i := ZQ1.Fields[0].AsInteger;
  22.     v.PushBack(i);
  23.     Inc(a);
  24.     Memo1.Lines.Add(IntToStr(i));
  25.     ZQ1.Next;
  26.   end;
  27.  
  28. {-------------------------------------------------------------------------------
  29.         NOW WE'LL RANDOMIZE THE NUMBERS AND STORE THEM IN AN ARRAY
  30. -------------------------------------------------------------------------------}
  31.   Randomize;
  32.   j := 0;
  33.   repeat
  34.     idx := Succ(Random(a));
  35.     case (idx in setb) of
  36.       True: ;
  37.       False: begin
  38.               Include(setb, idx);
  39.               Inc(j);
  40.               arr[j] := v[idx];
  41.               Memo2.Lines.Add(IntToStr(arr[j]));
  42.             end;
  43.     end;
  44.   until j = 20;

Like I said, most of this was a freebie from a Forum regular.  And I don't understand all of it.  I'll get there.  But can someone tell me why I get a 0 in place of the 1?  Thank you.

I attached the database.  To do so I had to change the extension from .db to .txt
« Last Edit: December 01, 2020, 10:49:38 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: Vector, Array, Randomize and the missing 1
« Reply #1 on: December 01, 2020, 10:11:18 pm »
Change line 34 to:
Code: Pascal  [Select][+][-]
  1. idx := Random(a);

That "iVector" is zero based, so you need values from 0 to 19, and not from 1 to 20.

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: Vector, Array, Randomize and the missing 1
« Reply #2 on: December 01, 2020, 10:35:23 pm »
@dsellgo - I realize vectors are zero-based and pascal arrays begin at 1, so all I wanted to do is fill the vector with as many record IDs as the table holds and then randomize them back into the array until I got 20 entries, then break out of the repeat loop.

I tried your suggestion and it fixed it.  I don't have a clue what Succ means.  But I thank you for the help.  Much appreciated, dsellgo.
« Last Edit: December 02, 2020, 05:23:23 am by Slyde »
Linux Mint 21.3
Lazarus 3.0

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: [SOLVED] Vector, Array, Randomize and the missing 1
« Reply #3 on: December 01, 2020, 10:57:33 pm »
Quote
Succ returns the element that succeeds the element that was passed to it. If it is applied to the last value of the ordinal type, and the program was compiled with range checking on ( {$R+} ), then a run-time error will be generated.
--FreePascal.org
Linux Mint 21.3
Lazarus 3.0

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: Vector, Array, Randomize and the missing 1
« Reply #4 on: December 01, 2020, 11:46:16 pm »
I don't have a clue what Succ means.
Random function returns random values from 0 to a-1 (from 0 to 19). Succ increments it by 1 (and you don't want that here).

 

TinyPortal © 2005-2018