From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id SFsxMOE90V5SKAAA0tVLHw (envelope-from ) for ; Fri, 29 May 2020 16:52:49 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id EHMPLOE90V6PCgAA1q6Kng (envelope-from ) for ; Fri, 29 May 2020 16:52:49 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 3149C940308 for ; Fri, 29 May 2020 16:52:49 +0000 (UTC) Received: from localhost ([::1]:48562 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeiFT-0001bE-Hi for larch@yhetil.org; Fri, 29 May 2020 12:52:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36062) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeiEu-0001UN-N8 for emacs-orgmode@gnu.org; Fri, 29 May 2020 12:52:12 -0400 Received: from relay7-d.mail.gandi.net ([217.70.183.200]:47129) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeiEt-0001VV-Cx for emacs-orgmode@gnu.org; Fri, 29 May 2020 12:52:12 -0400 X-Originating-IP: 82.69.109.251 Received: from localhost (82-69-109-251.dsl.in-addr.zen.co.uk [82.69.109.251]) (Authenticated sender: stig@brautaset.org) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 01B9620007; Fri, 29 May 2020 16:52:07 +0000 (UTC) From: Stig Brautaset To: Uwe Brauer , emacs-orgmode@gnu.org Subject: Re: [random sorting] (was: org table: one column of random numbers (but natural ones)) In-Reply-To: <87eer3dm4a.fsf_-_@mat.ucm.es> References: <87mu5taz7a.fsf@mat.ucm.es> <52b74175-5b6a-a78d-7d1f-8e3da16f184a@free.fr> <87lflbhq7a.fsf@mat.ucm.es> <87eer3dm4a.fsf_-_@mat.ucm.es> Date: Fri, 29 May 2020 17:52:03 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=217.70.183.200; envelope-from=stig@brautaset.org; helo=relay7-d.mail.gandi.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/29 11:34:58 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -1.01 X-TUID: EdWnan9w57A5 Uwe Brauer writes: >>>> "SB" == Stig Brautaset writes: > > >> > In row 67 you would have a random integer in the range [0..67) > >> > f0 format removes any fractional part leaving only an integer number > >> > >> Aha thanks, a minor thing, which I thank, cannot be really done: > >> > >> Is it possible to avoid number repetition? > >> > >> So I want a random sequence of the column 67 but I don't want numbers to > >> be repeated. > > > To avoid duplicates you could generate a sequence from [0..67), shuffle > > it[1], then use the row number as an index into that list. (Or pop off the > > front.) How to do that from an org table function I have no idea, > > however. > > Thanks I tried in a row of 33 > $5=random([0..34]);f0 > $5=random([0..34));f0 > $5=random([0..33));f0 > > But random repeats, however org-table-sort-lines sorts anyway > And what I truly needed is a random sorting of sorts. Right, I think I failed to make myself understood so here's an example of what I had in mind. It's not convenient to use (need to execute a source code block) but hopefully what I mean is a bit clearer, and someone can clean it up a little :-) First we need to generate a randomised sequence of unique integer in a range, using the Knuth shuffle I pointed to earlier. Every time you run tihs you get a new sequence. I've kept the output more to verify that the results have unique output, as the table formula later will read from the lisp variable, IIUC. #+name: random-seq #+begin_src emacs-lisp :var length=10 :results list (defun nshuffle (sequence) (loop for i from (length sequence) downto 2 do (rotatef (elt sequence (random i)) (elt sequence (1- i)))) sequence) (setq random-seq (nshuffle (number-sequence 0 (1- length)))) #+end_src #+RESULTS: random-seq - 5 - 9 - 6 - 0 - 7 - 8 - 3 - 2 - 1 - 4 And now for the table that uses the variable. As we compute the randomised sequence ahead of time for each invocation of the column formula, and we can use each row number as an index into the sequence to assign a unique, randomised integer from a range to each column. | 0 | 5 | | 1 | 9 | | 2 | 6 | | 3 | 0 | | 4 | 7 | | 5 | 8 | | 6 | 3 | | 7 | 2 | | 8 | 1 | | 9 | 4 | #+TBLFM: $2='(nth (string-to-number $1) random-seq) Possible improvements (that I don't think I'm up to making): 1. Don't require the column of indices to use as index into the sequence 2. Show how to do this without having the separate pre-compute step of the index (possibly with memoizing a sequence on first use?) Hope this helps! Regards, Stig