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))
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
(take 4 (nshuffle (vec-seq 1..65)))#(32 31 23 39)
Pascal J. Bourguignon wrote:
most Usenet forums frown on proselytizing
most Usenet forums frown on proselytizing
really???
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 475 |
Nodes: | 16 (1 / 15) |
Uptime: | 66:50:46 |
Calls: | 9,497 |
Calls today: | 1 |
Files: | 13,623 |
Messages: | 6,125,031 |