From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Emanuel Berg Newsgroups: gmane.emacs.devel Subject: Re: Add seq-shuffle Date: Wed, 18 Sep 2024 03:27:55 +0200 Message-ID: <8734lxpx7o.fsf@dataswamp.org> References: <87msk8p4mb.fsf@dataswamp.org> <14bc4905-38b4-459d-8522-7cc33bbadd18@alphapapa.net> <87a5g62kkh.fsf@posteo.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16661"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: emacs-devel@gnu.org Cancel-Lock: sha1:uwCEEHZwao/ngRDCb5wzopdKLZc= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Sep 18 04:21:44 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sqkK8-0004CF-BB for ged-emacs-devel@m.gmane-mx.org; Wed, 18 Sep 2024 04:21:44 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sqkJs-0004Yh-11; Tue, 17 Sep 2024 22:21:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sqjUO-0004ty-KQ for emacs-devel@gnu.org; Tue, 17 Sep 2024 21:28:16 -0400 Original-Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sqjUM-0006IV-Ul for emacs-devel@gnu.org; Tue, 17 Sep 2024 21:28:16 -0400 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1sqjUK-0008RV-9q for emacs-devel@gnu.org; Wed, 18 Sep 2024 03:28:12 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: emacs-devel@gnu.org Mail-Copies-To: never Received-SPF: pass client-ip=116.202.254.214; envelope-from=ged-emacs-devel@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 17 Sep 2024 22:21:26 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:323711 Archived-At: Philip Kaludercic wrote: >> That's a neat and concise solution. It seems to produce >> a decently random sorting, but I'd guess that it may not >> perform well for large sequences. For example: >> >> (let (comparisons) >> (list :result (seq-sort (lambda (a b) >> (push (cons a b) comparisons) >> (zerop (random 2))) >> (number-sequence 0 10)) >> :num-comparisons (length comparisons))) >> ;; (:result (6 0 9 1 5 10 8 2 7 3 4) :num-comparisons 26) >> >> In testing that expression repeatedly, I see that the >> number of comparisons varies between about 23 and 26. >> >> Thanks for pointing it out, though. It's good to know that >> it exists and in what circumstances it could be useful. > > IIRC the approach is related to the "Naive method" mentioned > on Wikipedia [0]. I think that this variation of your code > demonstrates that not all elements are considered equally > often: > > (let ((comparisons '())) > (list :result > (seq-sort (lambda (a b) > (cl-incf (alist-get a comparisons 0)) > (cl-incf (alist-get b comparisons 0)) > (zerop (random 2))) > (number-sequence 0 10)) > :num-comparisons > (seq-sort (lambda (x y) > (< (cdr x) (cdr y))) > comparisons))) > ;; (:result > ;; (9 0 1 7 4 10 5 6 8 3 2) > ;; :num-comparisons > ;; ((0 . 2) (10 . 3) (8 . 3) (9 . 4) (7 . 4) (6 . 4) (4 . 5) (2 . 5) (3 . 7) (1 . 7) (5 . 8))) I think it is as random as `random', as for the exact execution of the algorithm one should examine 'seq-sort'. Natural thing would just be to go from one end to the other, but maybe they do some optimizations. > While acceptable as a personal hack Very generous of you, noble sir! -- underground experts united https://dataswamp.org/~incal