• Re: make a list of different random numbers

    From B. Pym@21:1/5 to Pascal J. Bourguignon on Tue Jul 16 18:55:30 2024
    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)
  • From Kaz Kylheku@21:1/5 to B. Pym on Tue Jul 16 19:24:15 2024
    On 2024-07-16, B. Pym <Nobody447095@here-nor-there.org> wrote:
    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

    Since the range of the numbers is so tiny, it seems nicer
    to implement it as a lottery draw:

    (take 4 (nshuffle (vec-seq 1..65)))
    #(32 31 23 39)

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Neuner@21:1/5 to Nobody447095@here-nor-there.org on Tue Jul 16 15:31:49 2024
    On Tue, 16 Jul 2024 18:55:30 -0000 (UTC), "B. Pym" <Nobody447095@here-nor-there.org> wrote:

    Pascal J. Bourguignon wrote:

    First, Pascal B. hasn't posted anything here for almost 5 years. Why
    are you dredging up ancient discussions?

    Second, most Usenet forums frown on proselytizing - it is considered
    rude. This is a Lisp forum - Scheme code belongs in comp.lang.scheme.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From HenHanna@21:1/5 to All on Tue Jul 16 15:47:09 2024
    most Usenet forums frown on proselytizing


    really???

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to HenHanna on Wed Jul 17 00:29:10 2024
    On 2024-07-16, HenHanna <HenHanna@devnull.tb> wrote:


    most Usenet forums frown on proselytizing


    really???

    Text-to-speach typo; George was surely saying "are founded upon".

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)