Lisp to randomly select a set number of instances

Lisp to randomly select a set number of instances

jf.bedard22
Explorer Explorer
734 Views
2 Replies
Message 1 of 3

Lisp to randomly select a set number of instances

jf.bedard22
Explorer
Explorer

Hi,

I'm having issues with a lisp.

I'm trying to get a random selection by inputting a quantity rather than a percentage. This lisp is a base that works well for percentages. I am aware this has been already asked.  

 

(defun c:randset (/ ss sl i en el ep pct qty rs)

;; SMadsen Random Number
(defun randnum (/ modulus multiplier increment random)
 (if (not seed)
     (setq seed (getvar "DATE")))
 (setq modulus    65536
    multiplier    25173
     increment    13849
       seed       (rem (+ (* multiplier seed) increment) modulus)
       random     (/ seed modulus)))

 (cond ((or (setq ss (ssget "i")) (setq ss (ssget)))
        (sssetfirst nil ss)
        (setq sl (sslength ss)
               i -1)
        (while (setq en (ssname ss (setq i (1+ i))))
               (setq el (cons en el))))
       (T (alert "\nNo Entities Found")
          (exit)))

 (initget 7)


 (setq pct (getreal "\nPercentage To Randomly Choose:   "))
 (setq qty (fix (* sl pct 0.01)))

 (setq rs (ssadd))
 (while (> qty (sslength rs))
    (setq ep (fix (* sl (randnum)))
          en (nth ep el))
    (if (not (ssmemb en rs))
        (ssadd en rs)))

 (sssetfirst nil rs)
 (princ)
)

I found another forum where one user suggests to swap a part of the script.

Instead of this:

(setq pct   (getreal  "\nPercentage To Randomly Choose:   "))
(setq qty (fix (* sl pct 0.01))) 

do this:

(setq qty (geting "\nNumber of items to be selected: ")) 

I tried the swap but I get an no function definition error. I don't truly understand what I'm doing so if any of you has a solution that would be awesome. Feel free to redirect me if there is a more appropriate place to ask such questions.

As a sidenote I posted this on reddit but I'm thinking this here may be more appropriate. 

Thanks

0 Likes
Accepted solutions (1)
735 Views
2 Replies
Replies (2)
Message 2 of 3

ВeekeeCZ
Consultant
Consultant
Accepted solution

Rewritten to be more efficient.

 

(defun c:randset ( / randnum ss sn n e)
  
  ;; SMadsen Random Number
  (defun randnum (/ modulus multiplier increment random)
    (if (not seed)
      (setq seed (getvar "DATE")))
    (setq modulus    65536
	  multiplier    25173
	  increment    13849
	  seed       (rem (+ (* multiplier seed) increment) modulus)
	  random     (/ seed modulus)))
  
  (if (and (or (and (ssget "_I")
		    (sssetfirst nil)
		    (setq ss (ssget "_P")))
	       (setq ss (ssget)))
	   (not (initget 0))
	   (setq n (getint (strcat "\nNumber of items of " (itoa (sslength ss)) ": ")))
	   (or (<= n (sslength ss))
	       (prompt "\nError: The selected number has exceeded the possible maximum."))
	   (setq sn (ssadd)))
    (repeat n
      (setq e (ssname ss (fix (* (sslength ss) (randnum)))))
      (ssadd e sn)
      (ssdel e ss)))
  (if sn (sssetfirst nil sn))
  (princ)
  )

 

0 Likes
Message 3 of 3

jf.bedard22
Explorer
Explorer
Thanks! That's a nice little touch to have added a count of the selected items!
0 Likes