Pascal J. Bourguignon wrote:
i would like to get a list of 4 different random numbers.
(wk wn wb bk)
i started this code :
(defun random-position () (1+ (random 64)))
(defparameter wk (random-position))
(excl:until (not (= wb wk)) (setf wb (random-position)))
but it is not working. i just need to ensure that none of the
positions
are the same. please to help.
thanks, david
(do ((results '() results)
(alea (random-position) (random-position)))
((<= 4 (length results))
results)
(pushnew alea results))
or:
(loop
:with results = '()
:for alea = (random-position)
:while (< (length results) 4)
:do (pushnew alea results)
:finally (return results))
Pascal hasn't yet mastered "do"; perhaps he never will.
Gauche Scheme
(use srfi-1) ;; lset-adjoin (To act as "pushnew".)
(use srfi-27) ;; random-integer
(define (random-position) (+ 1 (random-integer 64)))
(do ((results '() (lset-adjoin = results (random-position))))
((> (length results) 3) results))
===>
(26 31 48 49)
Check for duplication:
(length (delete-duplicates
(do ((results '() (lset-adjoin = results (random-position))))
((> (length results) 63) results))))
===>
64
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)