From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ivan Kanis Newsgroups: gmane.emacs.devel Subject: Re: random doesn't feel very random, random doesn't feel very random, Re: random doesn't feel very random Date: Sat, 25 Aug 2012 09:46:12 +0200 Message-ID: <877gsnifcr.fsf@googlemail.com> References: <876288yh72.fsf@kanis.fr> <87ipc8vkj6.fsf@uwakimon.sk.tsukuba.ac.jp> <87mx1kdd2i.fsf@thinkpad.tsdh.de> <87d32g9pmq.fsf@googlemail.com> <87ehmwuntx.fsf@uwakimon.sk.tsukuba.ac.jp> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1345880791 29894 80.91.229.3 (25 Aug 2012 07:46:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 25 Aug 2012 07:46:31 +0000 (UTC) Cc: emacs devel To: "Stephen J. Turnbull" Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Aug 25 09:46:32 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1T5B4c-0006Pw-Sl for ged-emacs-devel@m.gmane.org; Sat, 25 Aug 2012 09:46:27 +0200 Original-Received: from localhost ([::1]:37419 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T5B4b-0003zB-50 for ged-emacs-devel@m.gmane.org; Sat, 25 Aug 2012 03:46:25 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:57082) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T5B4X-0003r0-NE for emacs-devel@gnu.org; Sat, 25 Aug 2012 03:46:22 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T5B4W-00065K-2S for emacs-devel@gnu.org; Sat, 25 Aug 2012 03:46:21 -0400 Original-Received: from mail-we0-f169.google.com ([74.125.82.169]:49961) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T5B4V-00065G-OY for emacs-devel@gnu.org; Sat, 25 Aug 2012 03:46:19 -0400 Original-Received: by weys10 with SMTP id s10so1574556wey.0 for ; Sat, 25 Aug 2012 00:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:subject:references:face:cc:x-hashcash:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=IILiayHvXlxQ9SzOSSs3mviC7eeTr/JQUMtoLbJhaHM=; b=PxTbdSw9RnGr9g0ydWruibOeZnpwigwqFZXTcUk17Mnnt9djYwniCmBAq+7n+SCwnZ xSvkMesDVq5kHFHV0Nuz7+dvhMDXqZq1IKCeEwaYfElFWUI+jXqElKchDDhhqrYNTBnx f4B+BSjTK849SUw4S8HAyJd54ruesPsaoeq9BbCyGQWJHdHCzX2CTTtnabiMUptTSFGb JfxhhJKL4lXVdfV1ZTIzyZ8bBmnuSfIDZGEi4jxVZiamiecumkhr1aK04wqVBQZICigo nKUoqKEcc0Z8/ykdOejcBLTgdvi7bi+r08qgBMsoUXEz6h24yPCs+3k1ut7cDcQZepB9 7EyQ== Original-Received: by 10.217.6.14 with SMTP id x14mr3703526wes.174.1345880778357; Sat, 25 Aug 2012 00:46:18 -0700 (PDT) Original-Received: from tao ([89.83.137.164]) by mx.google.com with ESMTPS id fu8sm1512718wib.5.2012.08.25.00.46.14 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 25 Aug 2012 00:46:16 -0700 (PDT) Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAAXNSR0IArs4c6QAAAB5QTFRF IBkXUxMTOCwoTC4qcUY8iFxQmmper31txpaJ/v/8aKZ1oAAAAcVJREFUOMt100tu2zAQAFDCXnUZ oAcIqG68LZULVLQBbwuSiffm6AIRRyeotAySJoVu2+FPpJWWC3/4NKOZocSW/yxWfnJ2+Bdwzhj7 8gleWVy7DXC2rkMNr2V/zRbghXIwthf3VbIA9Ffc71vZCSFyCEsBggtNS8ludwvfmhYA0Vn9o4DP zMWxR7+cPWzAYFzwM0ModtdmcDbDS6i/hT7L+RZof5yCXGrYe5jn2YO6BYMjgY+51tCIAqHgBLwR pwLnGuRjAKyBJkuN4yd4U92uCY1vUr2D/c5b8DuxyQwfOHUeaLqDJhnkkuGXbB56h2C1IVBdgncc bBi6feroa9B6jUDojnQPQKupbyXyeeCE1oT7Oqrt+SnfY3mkiyGA/3AmD3H5g32CcBx6hY8pRkwJ 9PpcjRGobUfprFnhAa1vepwcgMOhwG+pdSgKHFU9HAvoAH6XUl7lDUCCq5Qb6GMbVm3Aj++qDYCt wdBc/YHgOFCmS3mjDMRcSE2qY4E3Q3PVIQRQmeodNH4QEbRUFZzW+VotzwX4yTcRTySOML1qjcE5 hTirVqDHkMAP0PjAywp3d18JZtqzvr9zDYD+GaSKtE6Zlr/DLPNFmOcvBAAAAABJRU5ErkJggg== X-Hashcash: 1:20:120825:stephen@xemacs.org::o1ptYnkkd7aXaZ/n:00000000000000000000000000000000000000000001H3l In-Reply-To: <87ehmwuntx.fsf@uwakimon.sk.tsukuba.ac.jp> (Stephen J. Turnbull's message of "Sat, 25 Aug 2012 03:50:02 +0900") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 74.125.82.169 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:152828 Archived-At: --=-=-= Content-Type: text/plain "Stephen J. Turnbull" wrote: > Ivan Kanis writes: > > "Stephen J. Turnbull" wrote: > > > > To avoid this behavior, use `(random t)' > > > > I do. > > Where? I don't see it in your `ivan-emms-pick-album' function I call it once in the callee function ivan-emms-random-album. I attached the source again. > this will only help across Emacs sessions; within one session the > random function should maintain state across calls to your function, > so it should give different sequences every time as long as you never > exist Emacs. I exit emacs everyday, would that have an effect on the randomness? Should I save the seed somewhere to have better result? > Unfortunately the attachment seems to have been stripped so I don't > know what might be going wrong in `ivan-emms-list-subdir' or the code > that calls `ivan-emms-pick-album'. (Probably nothing, but I can't > review it.) I have put the code source on my web site in case the attachment gets stripped again: http://ivan.kanis.fr/ivan-emms.el > If you do `M-x (ivan-emms-list-subdir (concat ivan-emms-dir "FOO")) RET' > (with some value for FOO that actually exists and contains albums), > does the answer look right? It looks good: (length (ivan-emms-list-subdir (concat ivan-emms-dir "music"))) => 769 -- Ivan Kanis http://ivan.kanis.fr An open foe may prove a curse, But a pretended friend is worse. -- John Gay --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=ivan-emms.el Content-Transfer-Encoding: quoted-printable ;;; ivan-emms.el --- emacs multimedia system ;;; Commentary: ;;; THANKS: ;;; BUGS: ;;; INSTALLATION: ;;; Code: (require 'ivan-face) (eval-when-compile (setq load-path (cons "../emms" load-path))) (defvar ivan-emms-shuffle nil "t when playlist is shuffled.") (defvar ivan-emms-dir "~/music" "Directory where music is stored.") (defvar ivan-emms-last (concat ivan-var-tmp-dir "emms-last.el") "Cache of last played albums") (defun ivan-emms-add-dired () (interactive) (ivan-emms-init) (emms-add-dired)) (defun ivan-emms-init () (if (not (fboundp 'emms-playlist-mode-go)) (progn (require 'emms) (require 'emms-playlist-mode) (require 'emms-browser) (require 'emms-setup) (require 'emms-tag-editor) (require 'url-util) (require 'emms-playlist-mode) (emms-minimalistic) (setq emms-playlist-default-major-mode 'emms-playlist-mode)))) (defun ivan-emms-message-info () (interactive) (let ((track (emms-playlist-current-selected-track))) (emms-info-really-initialize-track track) (message (emms-info-track-description track)))) (add-hook 'emms-playlist-selection-changed-hook 'ivan-emms-message-info) (defun ivan-emms-next-album () "Play next album in track list." (interactive) (let ((dir (ivan-emms-track-dir))) (while (string=3D dir (ivan-emms-track-dir)) (emms-next)))) (defun ivan-emms-track-info () "Return string containing artist and track name" (when (and (fboundp 'emms-playlist-current-selected-track) (fboundp 'emms-info-track-description)) (let ((selected-track (emms-playlist-current-selected-track))) (when selected-track (let ((track (emms-info-track-description selected-track))) (concat (nth 2 (nreverse (split-string track "/"))) " - " (replace-regexp-in-string ".*[0-9][0-9] - " "" (file-name-sans-extension (file-name-nondirectory track))))))))) (defun ivan-emms-track-dir () "Get directory of current playing track." (file-name-directory (emms-info-track-description (emms-playlist-current-selected-track)))) (defun ivan-emms-play-dired () (interactive) (ivan-emms-init) (emms-play-dired)) (defun ivan-emms-playlist-mode-go () (require 'emms-playlist-mode) (setq emms-playlist-default-major-mode 'emms-playlist-mode) (emms-playlist-mode-go)) ;;; random album picker (defun ivan-emms-get-dir (path) (let ((dir (cddr (directory-files path))) (ret nil) file) (while dir (setq file (concat path "/" (car dir))) (if (file-directory-p file) (setq ret (cons file ret))) (setq dir (cdr dir))) ret)) (defun ivan-emms-list-subdir (path) (let ((dir (ivan-emms-get-dir path)) (ret nil)) (while dir (if (string-match "music" path) (setq ret (append ret (ivan-emms-get-dir (car dir)))) (setq ret (append ret dir))) (setq dir (cdr dir))) ret)) (defun ivan-emms-pick-album (dir length with-cache) "Return a LENGTH albums in a directory DIR. If WITH-CACHE is non-nil exclude albums that are in cache." (let* ((index 0) (ret (make-vector length nil)) (album (ivan-emms-list-subdir (concat ivan-emms-dir dir))) (album-length (length album)) ring) (when with-cache (setq ring (ivan-emms-last-load))) (while (< index length) (aset ret index (nth (if with-cache (let* ((r (random album-length)) (album (nth r album))) (while (ring-member ring r) (setq r (random album-length) album (nth r album))) (ring-insert ring album) r) (random album-length)) album)) (setq index (1+ index))) (when with-cache (ivan-emms-last-save ring)) ret)) (defun ivan-emms-play-list (playlist) (let ((length (length playlist)) (index 0)) (emms-playlist-current-clear) ;; add one various song to top of playlist (emms-add-file (ivan-emms-random-file (ivan-emms-just-file (directory-files (concat ivan-emms-dir "music/Various") t)))) (while (< index length) (emms-add-directory (aref playlist index)) (setq index (1+ index))))) (defun ivan-emms-random-album () "Play albums from my library at random" (interactive) (ivan-emms-init) (random t) (let ((playlist (vconcat (ivan-emms-pick-album "music" 15 t) (ivan-emms-pick-album "fresh" 15 nil)))) (emms-shuffle-vector playlist) (ivan-emms-play-list playlist) (emms-start))) (defun ivan-emms-just-file (list) "Remove directories from LIST." (when list (if (file-regular-p (car list)) (cons (car list) (ivan-emms-just-file (cdr list))) (ivan-emms-just-file (cdr list))))) (defun ivan-emms-random-file (file) "Given a list of FILE, pick one at random" (nth (random (length file)) file)) (defun ivan-emms-last-save (ring) "Save last played directories." (with-temp-file ivan-emms-last (insert "(setq emms-last '") (prin1 (ring-elements ring) (current-buffer)) (insert ")"))) (defun ivan-emms-last-load () "Return ring of last played directories." (if (load ivan-emms-last t) (ring-convert-sequence-to-ring emms-last) (let* ((length 50) (r (make-ring length)) (i 0)) (while (< i length) (ring-insert r "") (setq i (1+ i))) r))) (setq emms-info-asynchronously nil emms-player-list (list 'emms-player-mplayer-playlist 'emms-player-mplayer) emms-playing-time-display-format "") (ivan-face emms-browser-year/genre-face yellow emms-browser-artist-face yellow emms-browser-album-face yellow emms-browser-track-face normal emms-playlist-track-face normal emms-playlist-selected-face yellow) (provide 'ivan-emms) ;; Local Variables: ;; compile-command: "make" ;; End: ;; Copyright (C) 2009 Ivan Kanis ;; Author: Ivan Kanis ;; ;; This program is free software ; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation ; either version 2 of the License, or ;; (at your option) any later version. ;; ;; This program is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY ; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with this program ; if not, write to the Free Software ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --=-=-=--