help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Incredible shrinking list....


From: Kevin Rodgers
Subject: Re: Incredible shrinking list....
Date: Fri, 17 Oct 2008 22:15:56 -0600
User-agent: Thunderbird 2.0.0.17 (Macintosh/20080914)

Scott wrote:
Hi,

I am very much a newbie, so forgive if this is a stupid question.

I am trying to write a function which returns a randomized permutation
of a certain number of items. Here is my stab at it, and the problem
which baffles me:


(defun pick-ten ()
  "Pick a series of ten unique random numbers from 0-9.
You should ensure that random is properly 'seeded', as eg:
  (random t)
prior to using this function."
(let ((all-ten '(0 1 2 3 4 5 6 7 8 9))  ;initial list
      (rnd-list '())                    ;return value
      (nxt 0))                          ;placeholder for current number
  (while all-ten                        ;step through the list
    (setq all-ten                       ;trim by
     (delq                              ;deleting selected number
      (setq nxt                         ;but remember number for later
(car (nthcdr (mod (random)(length all-ten)) all-ten)
        )
     ) all-ten))
   (setq rnd-list (cons nxt rnd-list))  ;and put into the return value
) rnd-list ;now return it.
))

;; works fine on the first evaluation,
;; but the series somehow shrinks with subsequent calls!
;; what am I doing wrong???!
;;
(pick-ten)(3 2 9 7 1 6 0 5 8 4)
(pick-ten)(1 0 2 3)
(pick-ten)(0 1)
(pick-ten)(0)

Probably destructively modifying a constant -- try this:

(let ((all-ten (list 0 1 2 3 4 5 6 7 8 9))      ;initial list

or this:

(setq all-ten (remq ... all-ten))

--
Kevin Rodgers
Denver, Colorado, USA





reply via email to

[Prev in Thread] Current Thread [Next in Thread]