#Random #
2 messages in this thread
#Random # — declared parent_msg_num=(none), resolved parent_id points to #(none)
Hello everyone! Maybe somebody can give me some help. I'm trying to
generate a random integer between 1 and 64 for a calling program. I can get
the random number from the system clock, but I don't know how to set the
limits of 1 to 64. Here is a sample of the code:
. (defun get_random ( / ctime hour min sec) (setq ctime (getvar "cdate")
hour (* 100 (- ctime (fix ctime)))
min (* 100 (- hour (fix hour)))
sec (* 100 (- min (fix min)))
msec (* 100 (- sec (fix sec)))) (princ (strcat "\n" (rtos msec 2 2))))
.
If anyone can give me a hand I'd appreciate it…I might even be able
to return the favor someday!
==================
==>> Big D in Big D <<==
==================
.
#22266-Random # — declared parent_msg_num=22266, resolved parent_id points to #22266
You will no doubt get a thousands answers to this (cause it's fun and fairly
easy) but this is how I solved your problem.
Note: You had half the battle finished.
;random.lsp
(defun get_random ( / ctime hour min sec)
(setq ctime (getvar "cdate")
hour (* 100 (- ctime (fix ctime)))
min (* 100 (- hour (fix hour)))
sec (* 100 (- min (fix min)))
msec (* 100 (- sec (fix sec))))
)
;And now the rest of the story
(defun c:random (/ msec)
(while (or (< msec 1)(> msec 65)) ;this sets a range for the while loop
(get_random) ;get a random number
)
(prompt (strcat "\nYour answer is : "(rtos msec 2 0)))
(princ)
)
Jamie