Jump to content

Randomness


paulmcz

Recommended Posts

I have a problem I'd like to solve with lisp. I have 151 balls that I need to distribute somewhat randomly inside a cylinder Ø120 x 180"H by moving each in "Z" direction to its own elevation. The balls were polar arrayed in elevation 0.

 

(defun c:rand (/ n a b el nm sl ss osn)
  (command "_.undo" "begin")
  (setq	osn (getvar "osmode")
	h   (getdist "\n Space to fill height?: ")
	ss  (ssget)
	sl  (sslength ss)
	n   (/ (float h) (- sl 1))
	a   0
  )
  (setvar "osmode" 0)
  (repeat sl
    (setq b (* n a)
	  el (list 0 0 b)
	  a (1+ a)
	  sl (1- sl)
	  nm (ssname ss sl)
    )
    (command "move" nm "" (list 0 0 0) el)
  )
  (setvar "osmode" osn)
  (command "_.undo" "end")
  (princ)
)

 

If I select each one by one in random order to create selection set in the lisp here, the ball are processed in order I picked them. But I'd like to select them in one shot with crossing window, because I will have hundreds of them in the next project.

Is there a way to force 'ssget' to create selection set where the objects are numbered in random order?
Or a way to step through the selection set in random order?

 

Thanks

Random.dwg

Edited by paulmcz
Link to comment
Share on other sites

Give this a try:

(defun c:foo (/ el n p s)
  ;; http://www.lee-mac.com/random.html
  ;; Rand  -  Lee Mac
  ;; PRNG implementing a linear congruential generator with
  ;; parameters derived from the book 'Numerical Recipes'
  (defun lm:rand (/ a c m)
    (setq m   4294967296.0
	  a   1664525.0
	  c   1013904223.0
	  $xn (rem (+ c
		      (* a
			 (cond ($xn)
			       ((getvar 'date))
			 )
		      )
		   )
		   m
	      )
    )
    (/ $xn m)
  )
  ;; Random in Range  -  Lee Mac
  ;; Returns a pseudo-random integral number in a given range (inclusive)
  (defun lm:randrange (a b) (+ (min a b) (fix (* (lm:rand) (1+ (abs (- a b)))))))
  (if (and (setq n (getdist "\nEnter height: ")) (setq s (ssget ":L" '((0 . "INSERT")))))
    (foreach b (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
      (setq p (cdr (assoc 10 (setq el (entget b)))))
      (entmod (append el (list (list 10 (car p) (cadr p) (lm:randrange 0 n)))))
    )
  )
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

Wow, Ron, this is gold.

I have to study it to know what is going on there. I have seen the lisp on Lemac site but I had no idea of how to use it for my purpose.

 

Thanks very much. It is a great help.

Link to comment
Share on other sites

28 minutes ago, paulmcz said:

Wow, Ron, this is gold.

I have to study it to know what is going on there. I have seen the lisp on Lemac site but I had no idea of how to use it for my purpose.

 

Thanks very much. It is a great help.

You're welcome but most of the credit should go to Lee. 🍻

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...