Recent

Author Topic: [SOLVED] Randomizing numbers problem  (Read 1508 times)

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: Randomizing numbers problem
« Reply #15 on: November 21, 2020, 10:46:48 pm »
Perfect, jamie.  Code to study and learn from.  Have it in my app now.  I'll look at yours too, Roland57.  I can't hardly wait to see what winni does with Sets.  You guys/gals have a nice Forum here.  Very, very nice place.

Thanks for all your help.

**UPDATE - @jamie - the first button click produces same results each time.  It's not really random.  But thank you for your effort.  I have a lot to build on, thanks to all of you.
« Last Edit: November 21, 2020, 10:55:02 pm by Slyde »
Linux Mint 21.3
Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Randomizing numbers problem
« Reply #16 on: November 21, 2020, 10:58:21 pm »
yes, you may need to use the "RadomMize" at first to change the seed value
The only true wisdom is knowing you know nothing

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: Randomizing numbers problem
« Reply #17 on: November 21, 2020, 11:10:46 pm »
@jamie - Gotcha.  It now randomizes each time.  Many thanks.
Linux Mint 21.3
Lazarus 3.0

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Randomizing numbers problem
« Reply #18 on: November 21, 2020, 11:32:44 pm »
I don't know what Jamie had in mind with sets, but it may have been something like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   setb: set of Byte = [];
  4.   questionList: array[1..100] of string[12];
  5.   i: Integer;
  6.   idx: Byte;
  7.   questionSelection: array[1..20] of string[12];
  8.   s: String[12];
  9.  
  10. begin
  11.   for i := Low(questionList) to High(questionList) do
  12.     questionList[i] := 'Question ' + i.ToString;  // questionList simulates your database of questions
  13.  
  14.   Randomize;
  15.   i := 0;
  16.   repeat
  17.     idx := Succ(Random(100));
  18.     case (idx in setb) of
  19.       True: ;
  20.       False: begin
  21.                Include(setb, idx);
  22.                Inc(i);
  23.                questionSelection[i] := questionList[idx];
  24.              end;
  25.     end;
  26.   until i = 20;
  27.  
  28.   Memo1.Clear;
  29.   for s in questionSelection do
  30.     Memo1.Lines.Add(s);
  31. end;

Slyde

  • Full Member
  • ***
  • Posts: 152
Re: [SOLVED] Randomizing numbers problem
« Reply #19 on: November 22, 2020, 12:10:54 am »
@howardpc - Nice code.  I plugged that in and it worked without a glitch.  It's pretty advanced for me, though, so I'll definitely need to study it.  Thanks for the offering.  Much appreciated.
Linux Mint 21.3
Lazarus 3.0

 

TinyPortal © 2005-2018