Do you known the function that return a random number ?

Discussion in 'AutoCAD' started by a107, Dec 20, 2003.

  1. a107

    a107 Guest

    Dear you,

    I have only one question but I haven’t got the anwser So I think you can help me. The question is a function that return a random number in Autolisp. In the catalogue funtions I can’t find it. Hoping you can help me or show me the way to solve it ?.

    Thank so much.
     
    a107, Dec 20, 2003
    #1
  2. a107

    jb Guest

    To generate random number you can try to use last digits of system
    variable "cdate". These are hundredths of a second of current time.
    (of course if they are not generated several times in your procedure)
    J.B.
     
    jb, Dec 20, 2003
    #2
  3. a107

    Paul Turvill Guest

    There isn't one, per se. What many folks do is to create a little function
    that uses the always-changing CDATE variable..
    ___

    Dear you,

    I have only one question but I haven't got the anwser So I think you can
    help me. The question is a function that return a random number in Autolisp.
    In the catalogue funtions I can't find it. Hoping you can help me or show me
    the way to solve it ?.

    Thank so much.
     
    Paul Turvill, Dec 20, 2003
    #3
  4. Hi a107

    This is based on the logistic equation
    p = p + rp(1-p)
    by the Belgiamn mathematician Pierre Francois Verhulst.
    It is used to simulate population models in chaos theory.

    Normaly I use C++ to do calculation which can't easily be done in Vlisp.


    Anyway,
    11.139323343 -> this can be another number of your choice
    1.13579 -> this can be another number of your choice
    Be careful with these numbers, otherwise you'll get overflow
    The numbers returned are not "tue" random numbers.


    (setq rnumber (random))


    ;;; returns a random nunber between 0 and 1
    (defun random ( / rk a c )
    (st)
    (setq rk seed
    a (- 11.139323343 seed)
    c (+ 1.13579 seed)
    rk (/ (+ (* a rk) c) 2))

    (repeat 10 ;;; can be 20 as well
    (setq rk (/ (+ (* a rk) c) 2)
    rk (- rk (fix rk)));;; the random number between 0 and 1
    )
    rk;;; the ranom number
    );;; end defun


    ;;; need a seed
    (defun st ( / start-t)
    (setq start-t (getvar "Date");;; get timer
    seed (- start-t (fix start-t)));;; seed for random number
    )


    Regards
    Dieter
     
    Dieter Berger, Dec 20, 2003
    #4
  5. a107

    David Bethel Guest

    (defun rand () (rem (fix (* 100000000 (getvar "TDINDWG"))) 10))

    If I remember correctly, the PC clock only updates 18 times per second.
    With today's hardware, this can be an issue if you need to generate
    random numbers with any kind of frequency. -David
     
    David Bethel, Dec 20, 2003
    #5
  6. using the timer isn't random ! As you stated its repeated 18 times per
    second!
    The logistic function is pseudo eg not truly random but quite near.

    Regards
    Dieter
     
    Dieter Berger, Dec 20, 2003
    #6
  7. a107

    Paul Turvill Guest

    That's incorrect. It is *not* "repeated" 18 times per second, it is
    *updated* 18 times per second, and every value is different. There's a huge
    difference between "repeated" and "updated." For most purposes, the
    inability to control the point in the update cycle at which a value is
    extracted makes the function for practical purposes "random."
    ___
     
    Paul Turvill, Dec 20, 2003
    #7
  8. which makes it cyclic.
    The value returned is *updated* at 1/18 const intervall points
    If the update intervall would be variable we would be closer
    to random.
     
    Dieter Berger, Dec 20, 2003
    #8
  9. a107

    Tom Smith Guest

    In actual practice, if your program needs only one pseudo-random number, cdate may be good enough. But if you need to generatel a lot of numbers, the cyclical nature of the update makes cdate unusable. Whatever you're calculating, whatever the speed of your machine, there will be an obviously non-random pattern in the numbers you get. Try it and see! I use a function similar to that cited earlier, with different constants.
     
    Tom Smith, Dec 21, 2003
    #9
  10. that's what I wanted to make clear.

    A random number is an arbitrary number, but an arbitrary number
    is not a random number!


    Regards
    Dieter
     
    Dieter Berger, Dec 22, 2003
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.