Jump to content

Recommended Posts

Posted

I need to generate a 6 digit arbitrary number with lisp. Can you help me.

Posted (edited)

(atoi (strcat (itoa (LM:randrange 0 9)) (itoa (LM:randrange 0 9)) (itoa (LM:randrange 0 9)) (itoa (LM:randrange 0 9)) (itoa (LM:randrange 0 9)) (itoa (LM:randrange 0 9))))

or

(* (LM:rand) 1000000)

 

https://www.lee-mac.com/random.html

 

HTH. M.R.

Edited by marko_ribar
  • Thanks 1
Posted (edited)

There is also this *.lsp...

 

(defun c:6_digit_rnd-1 ( / rnd *a* 1_digit 2_digit 3_digit 4_digit 5_digit 6_digit done )

  (defun rnd ( / ti tis ns n ) ; *a* - lexical global number variable ; return - n random number from 0 to 9
    (setq ti (car (_vl-times)))
    (setq tis (itoa ti))
    (setq ns (substr tis (strlen tis)))
    (setq n (atoi ns))
    (if (null *a*)
      (setq *a* n)
      (progn
        (while (= *a* n)
          (setq ti (car (_vl-times)))
          (setq tis (itoa ti))
          (setq ns (substr tis (strlen tis)))
          (setq n (atoi ns))
        )
        (setq *a* n)
      )
    )
    n
  )

  (while (not done)
    (while (= (setq 1_digit (rnd)) 0))
    (setq 2_digit (rnd))
    (setq 3_digit (rnd))
    (setq 4_digit (rnd))
    (setq 5_digit (rnd))
    (setq 6_digit (rnd))
    (if (not (vl-position (setq *rtn* (atoi (strcat (itoa 1_digit) (itoa 2_digit) (itoa 3_digit) (itoa 4_digit) (itoa 5_digit) (itoa 6_digit)))) *lst*))
      (setq done t)
      (setq done nil)
    )
  )
  (prompt (strcat "\n" (itoa *rtn*))) ; *rtn* - global variable - 6 digits number
  (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digits numbers
  (prompt "\n6_digit_rnd is stored in global variable *rtn* - you can call it with !*rtn*...")
  (princ)
)

 

HTH.

M.R.

Edited by marko_ribar
  • Thanks 1
Posted (edited)

Another one...

 

(defun c:6_digit_rnd-2 ( / vldcl-RandomByGUID n ns lst done )

  (defun vldcl-RandomByGUID ()
    (distof
      (strcat
        "0."
        (vl-list->string
          (vl-remove-if-not
            (function (lambda ( x ) (< 47 x 58)))
            (vl-string->list
              (vlax-get (vlax-create-object "Scriptlet.TypeLib") "GUID")
            )
          )
        )
      )
    )
  )

  (while (not done)
    (setq n (* (vldcl-RandomByGUID) 1e+6))
    (setq ns (rtos n 2))
    (setq *rtn* (substr ns 1 6))
    (setq lst (list (substr *rtn* 1 1) (substr *rtn* 2 1) (substr *rtn* 3 1) (substr *rtn* 4 1) (substr *rtn* 5 1) (substr *rtn* 6 1)))
    (if (and (not (vl-position "." lst)) (not (vl-position (atoi *rtn*) *lst*)))
      (setq done t)
      (setq done nil)
    )
  )
  (prompt (strcat "\n" *rtn*))
  (setq *rtn* (atoi *rtn*)) ; *rtn* - global variable - 6 digit number
  (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digit numbers
  (prompt "\n6 digit rnd number is stored in *rtn* global variable... You can call it with : !*rtn*")
  (princ)
)

 

HTH.

M.R.

Edited by marko_ribar
Posted (edited)

To see millions of numbers, start this *.lsp and hold SPACE key, then after while click left mouse button and look in global *lst* with : !*lst*

This second version is much faster than my first one, so I used (while) looping for this second example...

I hope this is what you were seeking, otherwise I don't know what would you need RND 6 digits calculations...

 

(defun c:6_digit_rnd-1-while ( / rnd *a* 1_digit 2_digit 3_digit 4_digit 5_digit 6_digit done )

  (defun rnd ( / ti tis ns n ) ; *a* - lexical global number variable ; return - n random number from 0 to 9
    (setq ti (car (_vl-times)))
    (setq tis (itoa ti))
    (setq ns (substr tis (strlen tis)))
    (setq n (atoi ns))
    (if (null *a*)
      (setq *a* n)
      (progn
        (while (= *a* n)
          (setq ti (car (_vl-times)))
          (setq tis (itoa ti))
          (setq ns (substr tis (strlen tis)))
          (setq n (atoi ns))
        )
        (setq *a* n)
      )
    )
    n
  )

  (while (/= (car (grread)) 3)
    (setq done nil)
    (while (not done)
      (while (= (setq 1_digit (rnd)) 0))
      (setq 2_digit (rnd))
      (setq 3_digit (rnd))
      (setq 4_digit (rnd))
      (setq 5_digit (rnd))
      (setq 6_digit (rnd))
      (if (not (vl-position (setq *rtn* (atoi (strcat (itoa 1_digit) (itoa 2_digit) (itoa 3_digit) (itoa 4_digit) (itoa 5_digit) (itoa 6_digit)))) *lst*))
        (setq done t)
      )
    )
    (prompt (strcat "\n" (itoa *rtn*))) ; *rtn* - global variable - 6 digits number
    (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digits numbers
  )
  (princ)
)

;;; When routine start hold SPACE key to populate global variable *lst* ;;;
;;; To see which 6 digits number passed, when some time passed, click left mouse button and then type : !*lst* ;;;

 

(defun c:6_digit_rnd-2-while ( / vldcl-RandomByGUID n ns lst done )

  (defun vldcl-RandomByGUID ()
    (distof
      (strcat
        "0."
        (vl-list->string
          (vl-remove-if-not
            (function (lambda ( x ) (< 47 x 58)))
            (vl-string->list
              (vlax-get (vlax-create-object "Scriptlet.TypeLib") "GUID")
            )
          )
        )
      )
    )
  )

  (while (/= (car (grread)) 3)
    (setq done nil)
    (while (not done)
      (setq n (* (vldcl-RandomByGUID) 1e+6))
      (setq ns (rtos n 2))
      (setq *rtn* (substr ns 1 6))
      (setq lst (list (substr *rtn* 1 1) (substr *rtn* 2 1) (substr *rtn* 3 1) (substr *rtn* 4 1) (substr *rtn* 5 1) (substr *rtn* 6 1)))
      (if (and (not (vl-position "." lst)) (not (vl-position (atoi *rtn*) *lst*)))
        (setq done t)
      )
    )
    (prompt (strcat "\n" *rtn*))
    (setq *rtn* (atoi *rtn*)) ; *rtn* - global variable - 6 digit number
    (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digit numbers
  )
  (princ)
)

;;; When routine start hold SPACE key to populate global variable *lst* ;;;
;;; To see which 6 digits number passed, when some time passed, click left mouse button and then type : !*lst* ;;;

 

HTH.

M.R.

Edited by marko_ribar
Posted (edited)

To see concatenation of 6 digit number chunks, use this syntax :

 

(prompt (strcat "\n" (apply (function strcat) (mapcar (function itoa) *lst*))))(princ)

 

Regards, M.R.

Edited by marko_ribar
Posted (edited)

You know what...

It's better to observe 6 digit numbers as strings - this will allow that chunks sometimes have leading 0, which is totally valid representation...

So here are my codes with this intervention...

 

(defun c:6_digit_rnd-1-str ( / rnd *a* 1_digit 2_digit 3_digit 4_digit 5_digit 6_digit done )

  (defun rnd ( / ti tis ns n ) ; *a* - lexical global number variable ; return - n random number from 0 to 9
    (setq ti (car (_vl-times)))
    (setq tis (itoa ti))
    (setq ns (substr tis (strlen tis)))
    (setq n (atoi ns))
    (if (null *a*)
      (setq *a* n)
      (progn
        (while (= *a* n)
          (setq ti (car (_vl-times)))
          (setq tis (itoa ti))
          (setq ns (substr tis (strlen tis)))
          (setq n (atoi ns))
        )
        (setq *a* n)
      )
    )
    n
  )

  (while (not done)
    (setq 1_digit (itoa (rnd)))
    (setq 2_digit (itoa (rnd)))
    (setq 3_digit (itoa (rnd)))
    (setq 4_digit (itoa (rnd)))
    (setq 5_digit (itoa (rnd)))
    (setq 6_digit (itoa (rnd)))
    (if (not (vl-position (setq *rtn* (strcat 1_digit 2_digit 3_digit 4_digit 5_digit 6_digit)) *lst*))
      (setq done t)
      (setq done nil)
    )
  )
  (prompt (strcat "\n" *rtn*)) ; *rtn* - global variable - 6 digits number as string
  (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digits numbers as strings
  (prompt "\n6_digit_rnd is stored in global variable *rtn* - you can call it with !*rtn*...")
  (princ)
)

 

(defun c:6_digit_rnd-1-while-str ( / rnd *a* 1_digit 2_digit 3_digit 4_digit 5_digit 6_digit done )

  (defun rnd ( / ti tis ns n ) ; *a* - lexical global number variable ; return - n random number from 0 to 9
    (setq ti (car (_vl-times)))
    (setq tis (itoa ti))
    (setq ns (substr tis (strlen tis)))
    (setq n (atoi ns))
    (if (null *a*)
      (setq *a* n)
      (progn
        (while (= *a* n)
          (setq ti (car (_vl-times)))
          (setq tis (itoa ti))
          (setq ns (substr tis (strlen tis)))
          (setq n (atoi ns))
        )
        (setq *a* n)
      )
    )
    n
  )

  (while (/= (car (grread)) 3)
    (setq done nil)
    (while (not done)
      (setq 1_digit (itoa (rnd)))
      (setq 2_digit (itoa (rnd)))
      (setq 3_digit (itoa (rnd)))
      (setq 4_digit (itoa (rnd)))
      (setq 5_digit (itoa (rnd)))
      (setq 6_digit (itoa (rnd)))
      (if (not (vl-position (setq *rtn* (strcat 1_digit 2_digit 3_digit 4_digit 5_digit 6_digit)) *lst*))
        (setq done t)
      )
    )
    (prompt (strcat "\n" *rtn*)) ; *rtn* - global variable - 6 digits number as string
    (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digits numbers as strings
  )
  (princ)
)

;;; When routine start hold SPACE key to populate global variable *lst* ;;;
;;; To see which 6 digits number passed, when some time passed, click left mouse button and then type : !*lst* ;;;

 

(defun c:6_digit_rnd-2-str ( / vldcl-RandomByGUID n ns lst done )

  (defun vldcl-RandomByGUID ()
    (distof
      (strcat
        "0."
        (vl-list->string
          (vl-remove-if-not
            (function (lambda ( x ) (< 47 x 58)))
            (vl-string->list
              (vlax-get (vlax-create-object "Scriptlet.TypeLib") "GUID")
            )
          )
        )
      )
    )
  )

  (while (not done)
    (setq n (* (vldcl-RandomByGUID) 1e+6))
    (setq ns (rtos n 2))
    (setq *rtn* (substr ns 1 6))
    (if (vl-string-search "." *rtn*)
      (setq *rtn* (vl-string-trim (strcat "0" *rtn*) "."))
    )
    (if (not (vl-position *rtn* *lst*))
      (setq done t)
      (setq done nil)
    )
  )
  (prompt (strcat "\n" *rtn*)) ; *rtn* - global variable - 6 digit number as string
  (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digit numbers as strings
  (setq *lst* (vl-remove "" *lst*))
  (prompt "\n6 digit rnd number is stored in *rtn* global variable... You can call it with : !*rtn*")
  (princ)
)

 

(defun c:6_digit_rnd-2-while-str ( / vldcl-RandomByGUID n ns lst done )

  (defun vldcl-RandomByGUID ()
    (distof
      (strcat
        "0."
        (vl-list->string
          (vl-remove-if-not
            (function (lambda ( x ) (< 47 x 58)))
            (vl-string->list
              (vlax-get (vlax-create-object "Scriptlet.TypeLib") "GUID")
            )
          )
        )
      )
    )
  )

  (while (/= (car (grread)) 3)
    (setq done nil)
    (while (not done)
      (setq n (* (vldcl-RandomByGUID) 1e+6))
      (setq ns (rtos n 2))
      (setq *rtn* (substr ns 1 6))
      (if (vl-string-search "." *rtn*)
        (setq *rtn* (vl-string-trim (strcat "0" *rtn*) "."))
      )
      (if (not (vl-position *rtn* *lst*))
        (setq done t)
      )
    )
    (prompt (strcat "\n" *rtn*)) ; *rtn* - global variable - 6 digit number as string
    (setq *lst* (cons *rtn* *lst*)) ; *lst* - global variable - list of passed 6 digit numbers as strings
  )
  (setq *lst* (vl-remove "" *lst*))
  (princ)
)

;;; When routine start hold SPACE key to populate global variable *lst* ;;;
;;; To see which 6 digits number passed, when some time passed, click left mouse button and then type : !*lst* ;;;

 

And finally for concatenation of chunk numbered strings, just use :

 

(prompt (strcat "\n" (apply (function strcat) *lst*)))(princ)

 

HTH.

Regards, M.R.

Edited by marko_ribar
Posted (edited)

Here is Lee's sub in my interpretation...

 

(defun c:6_digit_rnd-LM-while-str ( / LM:rand done )

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

  (while (/= 3 (car (grread)))
    (setq done nil)
    (while (not done)
      (setq *rtn* (rtos (* (LM:rand) 1e+6) 2 0))
      (cond
        ( (= (strlen *rtn*) 5)
          (setq *rtn* (strcat "0" *rtn*))
        )
        ( (= (strlen *rtn*) 4)
          (setq *rtn* (strcat "00" *rtn*))
        )
        ( (= (strlen *rtn*) 3)
          (setq *rtn* (strcat "000" *rtn*))
        )
        ( (= (strlen *rtn*) 2)
          (setq *rtn* (strcat "0000" *rtn*))
        )
        ( (= (strlen *rtn*) 1)
          (setq *rtn* (strcat "00000" *rtn*))
        )
      )
      (if (not (vl-position *rtn* *lst*))
        (setq done t)
      )
    )
    (prompt (strcat "\n" *rtn*)) ;;; *rtn* - global variable - 6 digit number as string ;;;
    (setq *lst* (cons *rtn* *lst*)) ;;; *lst* - global variable - list of 6 digit numbers as strings ;;;
  )
  (setq *lst* (vl-remove "" *lst*))
  (princ)
)

;;; When routine start hold SPACE key to populate global variable *lst* ;;;
;;; To see which 6 digits number passed, when some time passed, click left mouse button and then type : !*lst* ;;;

 

HTH.

M.R.

Edited by marko_ribar

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