Author Topic: Random Number  (Read 5713 times)

0 Members and 1 Guest are viewing this topic.

David Bethel

  • Swamp Rat
  • Posts: 656
Random Number
« on: September 22, 2004, 05:20:33 PM »
I've tried numerous random number generators ( stig's included ) and all are a bit off.

Generate a 1,000 element list of numbers 0-99.  Run the stuff we did in maximum number a repeats in a list.

The law of averages says that each number should shown up 10 times ( 1 % ).  I've ran it numerous times and the max number of repeats will be anywhere from 16 to 25 times.

Q:  What would you consider to be a randon sampling.  Should each number show up 8 - 12 times? Or is that a bit too limited.  I've tried even large list and it stills run about the same percentages

-David
R12 Dos - A2K

SMadsen

  • Guest
Re: Random Number
« Reply #1 on: September 22, 2004, 05:47:18 PM »
Quote from: David D Bethel
... ( stig's included ) ...
I stole mine from people who are under suspicion of understanding the stuff (cos' I don't). Just to get the credits in order it was written by Paul Furman in '96 on the basis of an algorithm written in Pascal by Doug Cooper in '82.

When writing IFS fractals back in the 80's I relied on the Mac's own RNG. It worked great but I never made statistics on it. Can't really say anything clever about how much they are supposed to deviate.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Random Number
« Reply #2 on: September 22, 2004, 06:19:01 PM »
When I needed to generate a random number I used a quasi-rng .....
I took the system time, carried it to 14 decimal places, extracted the last 3 digits, applied an algorythm consisting of PI, multiplication and digit addition, then extracted the fraction then grabbed the digit in the original system time string fraction corresponding to (14 - digit) ... I never tested it outside of my application and it seemed to give a descent spread of randomness inthe application, so I never questioned it....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

David Bethel

  • Swamp Rat
  • Posts: 656
Random Number
« Reply #3 on: September 23, 2004, 09:31:11 AM »
Quote
it was written by Paul Furman in '96


Now there's a name I haven't heard in a while....

Yes for single random number, most of these work well.  If large lists, patterns can tend to imerge and give some funky looking tiling patterns.  -David
R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Random Number
« Reply #4 on: September 23, 2004, 09:55:33 AM »
I found this page that talks about random numbers and formulas thats kinda worth a read.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
Random Number
« Reply #5 on: September 23, 2004, 10:35:39 AM »
Yes, very interesting reading indeed.  Thanks!  -David
R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Random Number
« Reply #6 on: September 23, 2004, 10:44:40 AM »
Not a problem. I found that snip following links and searches based on this snip from S.I.C.P.

Quote
One common way to implement rand-update is to use the rule that x is updated to ax + b modulo m, where a, b, and m are appropriately chosen integers. Chapter 3 of Knuth 1981 includes an extensive discussion of techniques for generating sequences of random numbers and establishing their statistical properties. Notice that the rand-update procedure computes a mathematical function: Given the same input twice, it produces the same output. Therefore, the number sequence produced by rand-update certainly is not ``random,'' if by ``random'' we insist that each number in the sequence is unrelated to the preceding number. The relation between ``real randomness'' and so-called pseudo-random sequences, which are produced by well-determined computations and yet have suitable statistical properties, is a complex question involving difficult issues in mathematics and philosophy. Kolmogorov, Solomonoff, and Chaitin have made great progress in clarifying these issues; a discussion can be found in Chaitin 1975
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
Random Number
« Reply #7 on: September 23, 2004, 11:44:45 AM »
Quote
Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.


I liked that 1.  That makes a lot of us 'Sinners'  -David
R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Random Number
« Reply #8 on: September 23, 2004, 11:49:22 AM »
heh! yeah that comment kinda got a laugh out of me too.

The other one was Bill Gates said.  ...``If you think you're a really good programmer,...read [Knuth's] Art of Computer Programming....You should definitely send me a resume if you can read the whole thing.'' - Bill Gates  That comment made me do a double take. lol
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
Random Number
« Reply #9 on: September 23, 2004, 12:39:29 PM »
Don't think I'd get past two sentences in that book, by the sound of it :)

Thanks for providing that link, Se7en. I just sat down and changed some of the values in the RandNum function to see if it made any impact. For example, changed it to the values in the link and wrote a small test function on the basis of the TALLYMAX thingie by MP:

Code: [Select]
(defun tallymax (lst / key pair index)
  (foreach key lst
    (setq index
           (if (setq pair (assoc key index))
             (subst (cons key (1+ (cdr pair))) pair index)
             (cons (cons key 1) index))
    )
  )
  (setq key (apply 'max (mapcar 'cdr index)))
  (vl-remove-if-not
    (function (lambda (pair) (eq key (cdr pair))))
    (reverse index)
  )
)

(defun rng (/ modulus multiplier increment random)
  (if (not seed)(setq seed (getvar "DATE")))
  (setq modulus    4294967296.0 multiplier 1664525 increment 1
        seed       (rem (+ (* multiplier seed) increment) modulus)
        random     (/ seed modulus))
)

(defun largelist (/ ret)
  (repeat 1000 (setq ret (cons (fix (* 10 (rng))) ret))))

(defun examine (/ lst ret nlst deviation mean nums variance)
  (setq lst (largelist))
  (while lst
    (setq ret (tallymax lst))
    (foreach n ret
      (setq nlst (cons n nlst))
      (setq lst (vl-remove (car n) lst))
    )
  )
  ;; extract number of occurances
  ;; calc average (will always be 100 .. but oh well)
  (setq nums (mapcar 'cdr nlst) mean (/ (apply '+ nums)(length nums)))
  ;; calc variance (as n-1)
  (setq variance (/ (apply '+ (mapcar '(lambda (n)(expt (- n mean) 2.0)) nums))(1- (length nums))))
  ;; calc standard deviation
  (setq deviation (sqrt variance))
  (mapcar 'princ (list "\nAverage:            " mean
                       "\nVariance:           " variance
                       "\nStandard deviation: " deviation
                       "\nOcurrances:\n"
                       (vl-sort nlst '(lambda (a b)(< (car a) (car b))))))
  (princ)
)


(examine)
Average:            100
Variance:           53.5556
Standard deviation: 7.31817
Ocurrances:
((0 . 100) (1 . 91) (2 . 103) (3 . 94) (4 . 98 ) (5 . 101) (6 . 111) (7 . 103) (8 . 89) (9 . 110))


Changing some of the values in RNG could perhaps fine-tune it.

David Bethel

  • Swamp Rat
  • Posts: 656
Random Number
« Reply #10 on: September 23, 2004, 01:16:37 PM »
stig,

I tried cranking up the largelist repeat to 1,000,000:

Code: [Select]
Average:            100000
Variance:           57931.8
Standard deviation: 240.69
Ocurrances:
((0 . 100230) (1 . 100014) (2 . 99830) (3 . 100010) (4 . 99560) (5 . 100025)
 (6 . 99754) (7 . 100390) (8 . 100180) (9 . 100007))


That's more of I what I thought we should be getting.  It must be that 1,000 element list is still a small sampling.  -David
R12 Dos - A2K

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Random Number
« Reply #11 on: September 23, 2004, 01:25:39 PM »
Ive been doing some reading. I wanted to share with you guys. (Im not done reading it yet, but it looks pretty good.)

Code: [Select]
3.1.2  The Benefits of Introducing Assignment

As we shall see, introducing assignment into our programming language leads us into a thicket of difficult conceptual issues. Nevertheless, viewing systems as collections of objects with local state is a powerful technique for maintaining a modular design. As a simple example, consider the design of a procedure rand that, whenever it is called, returns an integer chosen at random.

It is not at all clear what is meant by ``chosen at random.'' What we presumably want is for successive calls to rand to produce a sequence of numbers that has statistical properties of uniform distribution. We will not discuss methods for generating suitable sequences here. Rather, let us assume that we have a procedure rand-update that has the property that if we start with a given number x1 and form

x2 = (rand-update x1)
x3 = (rand-update x2)

then the sequence of values x1, x2, x3, ..., will have the desired statistical properties.6


We can implement rand as a procedure with a local state variable x that is initialized to some fixed value random-init. Each call to rand computes rand-update of the current value of x, returns this as the random number, and also stores this as the new value of x.

(define rand
  (let ((x random-init))
    (lambda ()
      (set! x (rand-update x))
      x)))

Of course, we could generate the same sequence of random numbers without using assignment by simply calling rand-update directly. However, this would mean that any part of our program that used random numbers would have to explicitly remember the current value of x to be passed as an argument to rand-update. To realize what an annoyance this would be, consider using random numbers to implement a technique called Monte Carlo simulation.

The Monte Carlo method consists of choosing sample experiments at random from a large set and then making deductions on the basis of the probabilities estimated from tabulating the results of those experiments. For example, we can approximate using the fact that 6/2 is the probability that two integers chosen at random will have no factors in common; that is, that their greatest common divisor will be 1.7 To obtain the approximation to , we perform a large number of experiments. In each experiment we choose two integers at random and perform a test to see if their GCD is 1. The fraction of times that the test is passed gives us our estimate of 6/2, and from this we obtain our approximation to .

The heart of our program is a procedure monte-carlo, which takes as arguments the number of times to try an experiment, together with the experiment, represented as a no-argument procedure that will return either true or false each time it is run. Monte-carlo runs the experiment for the designated number of trials and returns a number telling the fraction of the trials in which the experiment was found to be true.

(define (estimate-pi trials)
  (sqrt (/ 6 (monte-carlo trials cesaro-test))))
(define (cesaro-test)
   (= (gcd (rand) (rand)) 1))
(define (monte-carlo trials experiment)
  (define (iter trials-remaining trials-passed)
    (cond ((= trials-remaining 0)
           (/ trials-passed trials))
          ((experiment)
           (iter (- trials-remaining 1) (+ trials-passed 1)))
          (else
           (iter (- trials-remaining 1) trials-passed))))
  (iter trials 0))

Now let us try the same computation using rand-update directly rather than rand, the way we would be forced to proceed if we did not use assignment to model local state:

(define (estimate-pi trials)
  (sqrt (/ 6 (random-gcd-test trials random-init))))
(define (random-gcd-test trials initial-x)
  (define (iter trials-remaining trials-passed x)
    (let ((x1 (rand-update x)))
      (let ((x2 (rand-update x1)))
        (cond ((= trials-remaining 0)  
               (/ trials-passed trials))
              ((= (gcd x1 x2) 1)
               (iter (- trials-remaining 1)
                     (+ trials-passed 1)
                     x2))
              (else
               (iter (- trials-remaining 1)
                     trials-passed
                     x2))))))
  (iter trials 0 initial-x))

While the program is still simple, it betrays some painful breaches of modularity. In our first version of the program, using rand, we can express the Monte Carlo method directly as a general monte-carlo procedure that takes as an argument an arbitrary experiment procedure. In our second version of the program, with no local state for the random-number generator, random-gcd-test must explicitly manipulate the random numbers x1 and x2 and recycle x2 through the iterative loop as the new input to rand-update. This explicit handling of the random numbers intertwines the structure of accumulating test results with the fact that our particular experiment uses two random numbers, whereas other Monte Carlo experiments might use one random number or three. Even the top-level procedure estimate-pi has to be concerned with supplying an initial random number. The fact that the random-number generator's insides are leaking out into other parts of the program makes it difficult for us to isolate the Monte Carlo idea so that it can be applied to other tasks. In the first version of the program, assignment encapsulates the state of the random-number generator within the rand procedure, so that the details of random-number generation remain independent of the rest of the program.

The general phenomenon illustrated by the Monte Carlo example is this: From the point of view of one part of a complex process, the other parts appear to change with time. They have hidden time-varying local state. If we wish to write computer programs whose structure reflects this decomposition, we make computational objects (such as bank accounts and random-number generators) whose behavior changes with time. We model state with local state variables, and we model the changes of state with assignments to those variables.

It is tempting to conclude this discussion by saying that, by introducing assignment and the technique of hiding state in local variables, we are able to structure systems in a more modular fashion than if all state had to be manipulated explicitly, by passing additional parameters. Unfortunately, as we shall see, the story is not so simple.

Exercise 3.5.  Monte Carlo integration is a method of estimating definite integrals by means of Monte Carlo simulation. Consider computing the area of a region of space described by a predicate P(x, y) that is true for points (x, y) in the region and false for points not in the region. For example, the region contained within a circle of radius 3 centered at (5, 7) is described by the predicate that tests whether (x - 5)2 + (y - 7)2< 32. To estimate the area of the region described by such a predicate, begin by choosing a rectangle that contains the region. For example, a rectangle with diagonally opposite corners at (2, 4) and (8, 10) contains the circle above. The desired integral is the area of that portion of the rectangle that lies in the region. We can estimate the integral by picking, at random, points (x,y) that lie in the rectangle, and testing P(x, y) for each point to determine whether the point lies in the region. If we try this with many points, then the fraction of points that fall in the region should give an estimate of the proportion of the rectangle that lies in the region. Hence, multiplying this fraction by the area of the entire rectangle should produce an estimate of the integral.

Implement Monte Carlo integration as a procedure estimate-integral that takes as arguments a predicate P, upper and lower bounds x1, x2, y1, and y2 for the rectangle, and the number of trials to perform in order to produce the estimate. Your procedure should use the same monte-carlo procedure that was used above to estimate . Use your estimate-integral to produce an estimate of by measuring the area of a unit circle.

You will find it useful to have a procedure that returns a number chosen at random from a given range. The following random-in-range procedure implements this in terms of the random procedure used in section 1.2.6, which returns a nonnegative number less than its input.8

(define (random-in-range low high)
  (let ((range (- high low)))
    (+ low (random range))))

Exercise 3.6.  It is useful to be able to reset a random-number generator to produce a sequence starting from a given value. Design a new rand procedure that is called with an argument that is either the symbol generate or the symbol reset and behaves as follows: (rand 'generate) produces a new random number; ((rand 'reset) <new-value>) resets the internal state variable to the designated <new-value>. Thus, by resetting the state, one can generate repeatable sequences. These are very handy to have when testing and debugging programs that use random numbers.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Random Number
« Reply #12 on: September 23, 2004, 01:27:01 PM »
1000 might be considered a small sample ... just imaging if the sample were 10 ....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Random Number
« Reply #13 on: September 23, 2004, 01:45:24 PM »
My head hurts.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Serge J. Gianolla

  • Guest
Random Number
« Reply #14 on: September 28, 2004, 10:03:23 PM »
Not in same category, but with some chopping, massaging etc, may prove to be helpful. Try GUID. Apparently Globally unique identifiers are ... UNIQUE. If you are a DosLIB user, you can generate them with the (Dos_guidgen) command.