Recent

Author Topic: Random ?  (Read 3813 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1900
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Random ?
« Reply #15 on: January 20, 2025, 12:12:41 pm »
Quote
or rather if it is possible to achieve this easily without having to recurse and check that the number hasn't already been selected.
Implies Sattolo, because otherwise you would need a flag of some sort for every member. Did you miss that?

Possible. I am sorry if I misunderstood, I still think that what is needed is any permutation. I understand that he is talking about how to avoid to have same element appear twice in the result, not that the element should not keep the position where it had been.
The original poster can answer that.

Anyway, for the clarification, with Thaddys example:
If you try the example with [0, 1] it will always return [1, 0].

If you try the code with [0, 1, 2] it will return either [1, 2, 0] or [2, 0, 1]. Out of six possible permutations it will exclude four -- [0, 1, 2], [0, 2, 1], [1, 0, 2], [2, 1, 0].

With the modification I suggested it will return any of the n! possible permutations. Now you can use what you actually need.
« Last Edit: January 20, 2025, 12:15:14 pm by Zoran »
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Random ?
« Reply #16 on: January 20, 2025, 12:24:13 pm »
I have added an example in my previous post that allows for all permutations, because it uses Knuth's version.
It is also very simple.
But I am sure they don't want the Trumps back...

Zoran

  • Hero Member
  • *****
  • Posts: 1900
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Random ?
« Reply #17 on: January 20, 2025, 12:27:49 pm »
I have added an example in my previous post that allows for all permutations, because it uses Knuth's version.
It is also very simple.

Including TStringList and using its CustomSort with a function which has random output is too heavy in my opinion. I think that the code you gave in the first place (with the small modification I gave, if random permutation is what is actually needed) is a lot simpler and cleaner solution.

But okay, you gave one more possible solution.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Random ?
« Reply #18 on: January 20, 2025, 12:32:59 pm »
And one more for trunk/main
Code: Pascal  [Select][+][-]
  1. program randomsort;
  2. {$mode objfpc}{$H+}
  3. {$modeswitch anonymousfunctions}
  4. uses fgl;
  5.  
  6. type
  7.   TStrList = specialize TFPGList<string>;
  8.  
  9. var
  10.   l:TStrList;
  11.   i:integer;
  12. begin
  13.   l:= TStrList.create;
  14.   l.add('test1');
  15.   l.add('test2');
  16.   l.add('test3');
  17.   l.add('test4');
  18.   l.add('test19');
  19.   randomize;
  20.   l.Sort(function(const a,b:string):integer
  21.                   begin
  22.                     { becomes -1, 0 or 1 }
  23.                     result := Random(3)-1;
  24.                   end);
  25.   for i := 0  to l.count-1 do writeln(l[i]);
  26.   l.free;
  27. end.
I consider fgl light weight ;)
« Last Edit: January 20, 2025, 01:30:29 pm by Thaddy »
But I am sure they don't want the Trumps back...

Zoran

  • Hero Member
  • *****
  • Posts: 1900
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Random ?
« Reply #19 on: January 20, 2025, 04:07:37 pm »
Were you the vandal that tried to mutilate my first version?  >:(

No. I might be a vandal, but not that one.

My implementation is absolutely correct.

It's not the point whether your implementation of some algorithm is correct, but whether this algorithm solves what is needed here or not. Now, as I've read the topic again, I strongly believe that it does not.

It's just that you misinterpreted the words "check that the number hasn't already been selected" to "no number should keep its original position".
So you provided the code for which you thought that it serves for what is asked for, but it was actually an incorrect and misleading answer.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Thaddy

  • Hero Member
  • *****
  • Posts: 16523
  • Kallstadt seems a good place to evict Trump to.
Re: Random ?
« Reply #20 on: January 20, 2025, 04:43:11 pm »
I beg to differ, but just in case I also gave an interpretation that is in line with how you understood it.
Only OP can answer who's right, but I gave two solutions and both with easy implementations and conforming to theory.
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018