本帖最后由 jdlfjk 于 2013-3-4 16:11 编辑
看看
(RandList 1 100 98 2)
(17 36 37 61 63 16 52 4 78 62 6 40 74 63 58 81 77 90 94 94 22 77 53 43 1 22 27 86 42 74 90 53 47 76 51 54 63 13 87 53 23 57 74 60 24 51 37 56 79 11 54 30 24 1 56 27 84 65 12 84 18 58 76 61 29 62 88 47 43 34 69 35 79 73 43 53 83 57 7 55 60 9 80 34 77 81 64 89 74 5 24 94 52 67 55 92 66 7)
;by jdlfjk @mjtd.com
;Random number generation function - based on the linear congruential
;method as presented in Doug Cooper's book Condensed Pascal
- (defun RandList (MinNum MaxNum Num n / co re x y RetList)
- (defun randnum (/ modulus multiplier increment random)
- (if (not seed)
- (setq seed (getvar "DATE"))
- )
- (setq modulus 65536
- multiplier 25173
- increment 13849
- seed (rem (+ (* multiplier seed) increment) modulus)
- random (/ seed modulus)
- random (fix (+ MinNum (* (- MaxNum MinNum -1) random)))
- )
- )
- (setq co 0
- re 1
- )
- (while (< co Num)
- (setq y (car RetList)
- co (1+ co)
- )
- (if (>= re n)
- (while (= (setq x (randnum)) y))
- (if (= (setq x (randnum)) y)
- (setq re (1+ re))
- (setq re 1)
- )
- )
- (setq RetList (cons x RetList))
- )
- (reverse RetList)
- )
下面是测试平均数的语句,重复500遍地去获取200个1或2的随机数,允许元素无限重复,最后求得到整数1的个数的平均值
- (setq total 0)
- (repeat 500
- (setq co 0)
- (foreach one (RANDLIST 1 2 200 200)
- (if (= one 1)
- (setq co (1+ co))
- )
- )
- (setq total (+ total co))
- )
- (setq average (/ total 500.0))
结果之一:
50148
100.296
说明取的平均数还是趋向平均分布的
|