Jump to content

Recommended Posts

Posted

Dear Lee,

Your code is a great.

But for intervals of negative shifts period by one.

(LM:randrange 1 3)
1
1
1
2
1
3
3
1
2
3

 

(LM:randrange -3 -1)
-1
-2
-1
-2
0
0
0
-2
0
-1

 

Of course, I can use a range of positive and negative transform him. I can also change the signs on the negative.

Please explain.

Posted (edited)

Please try the following [untested] modification:

;; 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))))))
)

 

EDIT: Updated.

Edited by Lee Mac
Posted

Thanks for the update.

Sorry for my poor English language.

  • 7 years later...
Posted

Hey Lee,

 

I ran into a problem with your random number generator as I attempted to use it in my acaddoc.lsp file. Because it seeds off the Date variable, the value is not random between opening files as they all begin at the same seed. each day. I was able to solve this by replacing your use of "getvar Date" with the MILLISECS variable and parsing it effectively to pull out the constantly changing base numbers. I now am able to successfully generate random numbers effectively in all use cases. I'm curious if you have encountered this problem before or may have a more elegant solution.

 

Thanks!

 

(setq old (rtos (getvar "MILLISECS") 2 0))

(setq new "")
(repeat (setq i (strlen old))
(setq new (strcat new (substr old i 1))
i (1- i)
)
)
(setq new (substr new 1 4))
(setq seed (atof new))

 

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...