Jump to content

Lisp tạo một số ngẫu nhiên trong khoảng nhập vào

Với góp ý anh Tot77 em có chương trình gửi lên để ai cần mng dùng.

(defun c:SRD(/ );;;;SO RANDOM
(setq i 0)
(setvar "CMDECHO" 0)
(or *a* (setq *a* 0))
(setq a (getreal (strcat "\n \nNhap so nho:  <"
		  (rtos *a* 2 0)
		 "> :"
	  )
 )
)
(if (not a) (setq a *a*) (setq *a* a))
(or *b* (setq *b* 10.0))
(setq b (getreal (strcat "\n \nNhap so lon: <"
		  (rtos *b* 2 0)
		 "> :"
	  )
 )
)
(if (not b) (setq b *b*) (setq *b* b))
(or *h* (setq *h* 1.00))
(setq h (getreal (strcat "\n \nNhap chieu cao chu: <"
		  (rtos *h* 2 2)
		 "> :"
	  )
 )
)
(if (not h) (setq h *h*) (setq *h* h))
(if (< a b)
    (progn
	(while
		(setq Pnt (getpoint "\nChon diem Pick ghi so ngau nhien: "))
	  	(setq NumberRandom (+ (rand1 a b) (* 0.1 (rand2 0 9)) (* 0.01 (rand2 0 9))))
		(Entmake (list (cons 0 "TEXT") (cons 10 Pnt) (cons 1 (rtos NumberRandom 2 2)) (cons 40 h)))

	)
     )
     (Alert "Ban nhap so a lon hon so b roi! Nhap lai nhe ban")
)
(princ)
)


(defun rand1 (a b)
    (+ a (fix (* (- b a) (atof (strcat "0." (substr (rtos (getvar 'cdate) 2 18) 16 2))))))
)
(defun rand2 (a b)
    (+ a  (* (- b a) (atof (strcat "0." (substr (rtos (getvar 'cdate) 2 18) 16 2)))))
)

Cách dùng

  1. Tải tệp 309486_srd.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh SRD để chạy lisp.

Mã nguồn 309486_srd.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/109300-y%C3%AAu-c%E1%BA%A7u-lisp-t%E1%BA%A1o-m%E1%BB%99t-s%E1%BB%91-ng%E1%BA%ABu-nhi%C3%AAn-trong-kho%E1%BA%A3ng-nh%E1%BA%ADp-v%C3%A0o/
(defun c:SRD(/ );;;;SO RANDOM
(setq i 0)
(setvar "CMDECHO" 0)
(or *a* (setq *a* 0))
(setq a (getreal (strcat "\n \nNhap so nho:  <"
		  (rtos *a* 2 0)
		 "> :"
	  )
 )
)
(if (not a) (setq a *a*) (setq *a* a))
(or *b* (setq *b* 10.0))
(setq b (getreal (strcat "\n \nNhap so lon: <"
		  (rtos *b* 2 0)
		 "> :"
	  )
 )
)
(if (not b) (setq b *b*) (setq *b* b))
(or *h* (setq *h* 1.00))
(setq h (getreal (strcat "\n \nNhap chieu cao chu: <"
		  (rtos *h* 2 2)
		 "> :"
	  )
 )
)
(if (not h) (setq h *h*) (setq *h* h))
(if (< a b)
    (progn
	(while
		(setq Pnt (getpoint "\nChon diem Pick ghi so ngau nhien: "))
	  	(setq NumberRandom (+ (rand1 a b) (* 0.1 (rand2 0 9)) (* 0.01 (rand2 0 9))))
		(Entmake (list (cons 0 "TEXT") (cons 10 Pnt) (cons 1 (rtos NumberRandom 2 2)) (cons 40 h)))

	)
     )
     (Alert "Ban nhap so a lon hon so b roi! Nhap lai nhe ban")
)
(princ)
)


(defun rand1 (a b)
    (+ a (fix (* (- b a) (atof (strcat "0." (substr (rtos (getvar 'cdate) 2 18) 16 2))))))
)
(defun rand2 (a b)
    (+ a  (* (- b a) (atof (strcat "0." (substr (rtos (getvar 'cdate) 2 18) 16 2)))))
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Yêu cầu] Lisp tạo một số ngẫu nhiên trong khoảng nhập vào. Xem đúng bài viết #309486 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 169)