Author Topic: Get random number between 2 numbers  (Read 3574 times)

0 Members and 1 Guest are viewing this topic.

BazzaCAD

  • Guest
Get random number between 2 numbers
« on: October 02, 2008, 02:07:53 PM »
Is there a better why of doing this?

Code: [Select]
;;;Random number generation function - based on the linear
;;; congruential method as presented in Doug Cooper's book
;;; Condensed Pascal, pp. 116-117.
;;; Returns a random number between 0 and 1.
(defun randomize (/ modulus multiplier increment random)
  (if (not daed_seed)
    (setq daed_seed (getvar "DATE"))
  )
  (setq modulus    65536
        multiplier 25173
        increment  13849
        daed_seed  (rem (+ (* multiplier daed_seed) increment) modulus)
        random     (/ daed_seed modulus)
  )
)

(defun RandNumBtwn2Nums (in out / num)
  (while (not (and (setq num (fix (* 100 (randomize))))
              (>= num in)
              (<= num out)
         ))
  )
  num
)
« Last Edit: October 02, 2008, 02:19:26 PM by BazzaCAD »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get random number between 2 numbers
« Reply #1 on: October 02, 2008, 02:15:39 PM »
Wasn't that one penned by Kenny Ramage?

Here's one I have --

Code: [Select]
(defun _Random ( )

    ;;  Original lisp version attributable to Paul Kahut,
    ;;  who translated an algorythm from a Visual C 4.1
    ;;  library. Abused by MP so it's self initializing
    ;;  and returns a float between 0 and 1 rather than
    ;;  an int.

    (/
        (+ 2147483648
            (setq *randomseed*
                (+
                    (*
                        (if (numberp *randomseed*)
                            (fix *randomseed*)
                            (getvar "millisecs")
                        )
                        214013
                    )
                    2531011
                )
            )
        )
        4294967295.0
    )   
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

BazzaCAD

  • Guest
Re: Get random number between 2 numbers
« Reply #2 on: October 02, 2008, 02:19:02 PM »
thx but that only returns a number between 0 & 1 not between two given numbers...

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Get random number between 2 numbers
« Reply #3 on: October 02, 2008, 02:40:54 PM »
Code: [Select]
(+ in (* (- out in) (randomize)))

JohnK

  • Administrator
  • Seagull
  • Posts: 10656
Re: Get random number between 2 numbers
« Reply #4 on: October 02, 2008, 02:41:24 PM »
Here are the only one(s) i got (looks much the same as yers).

Code: [Select]
(defun getrandnum ( minNum maxNum / randnum )
    ;; Getrandnum returns a real number between minNum and maxNum.
    ;;
    ;; By: Stig Madsen
    (defun randnum ( / modulus multiplier increment random)
      ;; Randnum.lsp
      ;; Returns a random number.
      ;; Written by Paul Furman, 1996.
      ;; Based on algorithm by Doug Cooper, 1982.
      (if (not seed)
        (setq seed (getvar "DATE")))
      (setq modulus    65536
                multiplier 25173
                increment  13849
                seed  (rem (+ (* multiplier seed) increment) modulus)
                random (/ seed modulus)) )
    (if (not (< minNum maxNum))
      (progn
        (setq tmp minNum
              minNum maxNum
              maxNum tmp
              )
        )
      )
    (setq random (+ (* (randnum) (- maxNum minNum)) minNum))
    )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8779
  • AKA Daniel

BazzaCAD

  • Guest
Re: Get random number between 2 numbers
« Reply #6 on: October 02, 2008, 02:45:05 PM »
cool thx guys.

taner

  • Guest
Re: Get random number between 2 numbers
« Reply #7 on: October 02, 2008, 07:51:43 PM »
Code: [Select]
;;; ==================================================================
;;; 求随机数(0~1)--by 狂刀
(defun txt-rnd ()        ; 随机数种子
  (* (rem (getvar "cputicks") 1e3) 1e-3)
)
;;; ==================================================================
;;; 求n个a~b随机数列表----by 狂刀
(defun txt-rnd-rlst (a b n / c lst)    ; 随机数表
  (setq c (- b a))
  (repeat n
    (setq lst (cons (+ a (* (txt-rnd) c)) lst))
  )
)
;;; ==================================================================
;;; 返回在a~b內的n個随机整数的列表
(defun txt-rnd-ilst (a b n / lst c)    ; 随机整数表
  (setq c (- b a))
  (repeat n
    (setq lst (cons (fix (+ a (* (txt-rnd) c))) lst))
  )
)
;;; ==================================================================
(defun txt-rnd-i (a b)        ; 单个随机整数
  (car (txt-rnd-ilst a b 1))
)
(defun txt-rnd-r (a b)        ; 单个随机数
  (car (txt-rnd-rlst a b 1))
)
;;; ==================================================================
;;; 随机取出表中元素
(defun txt-rnd-lst (lst / len n rndoflst)
  (if (> (setq len (length lst))
0
      )
    (setq n (txt-rnd-i 0 len)
  rndoflst (nth n lst)
    )
  )
  rndoflst
)
;;; ==================================================================

Cathy

  • Guest
Re: Get random number between 2 numbers
« Reply #8 on: October 03, 2008, 03:05:44 PM »
xkcd:

Cathy

  • Guest
Re: Get random number between 2 numbers
« Reply #9 on: October 03, 2008, 04:10:38 PM »