明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: heart1982

怎么产生随机数

  [复制链接]
发表于 2004-10-24 20:08:00 | 显示全部楼层
本帖最后由 作者 于 2004-10-24 20:30:31 编辑

meflying发表于2004-10-24 18:27:00我用如下程序测试: (defun c:test() (repeat 10 (princ \"\n\") (princ (rnd 1 10)) )) 结果如下,试了几次,发现还是不行的,连续...
这是因为计算机运行太快,要让每次取种子数的时间差>0.1s以上,当然还可以改变范围等办法 。 dispbbs.asp?BoardID=3&ID=15667
发表于 2004-10-24 23:38:00 | 显示全部楼层
随机数程序:加载sjsh.fas,运行sjsh。 随机数个数为30的结果: 12
15
25
42
60
78
13
28
47
81
88
50
63
24
40
2
18
80
19
74
8
60
92
11
52
77
91
96
97
22 不知是否有规律,再修改后公布源程序。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2004-10-25 08:43:00 | 显示全部楼层
请10楼和11楼再看清楚前面的帖子,现在讨论的内容已经转到"连续"产生随机数了,楼上的这个虽然连续产生了,不过速度太慢,其实是程序已经将连续内容断开成不连续的了...而且提供FAS在这里好象没有什么意义... 结果也不尽如人意: Command: sjsh
随机数个数: 100 92
6
17
31
53
8
67
99
8
36
45
69
92
16
15
15
15
16
24
24
23
23
25
25
25
25
25
25
25
25
25
25
25
25
25
27
27
27
26
27
26
26
27
27
26
28
28
28
28
28
28
28
28
28
28
28
28
3
29
31
31
31
31
32
32
31
31
33
33
33
32
33
33
33
33
33
33
33
33
33
33
34
34
34
34
34
34
34
34
34
36
36
36
36
36
36
36
36
36
36
发表于 2004-10-25 08:58:00 | 显示全部楼层
也可以用vc编一个程序,将连续产生的随机数写在注册表里或记事本里,再用VL读出来。


用start命令调用VC程序。


直接用VL连续产生随机数确实有点困难。
发表于 2004-10-25 10:17:00 | 显示全部楼层
(defun c:t1( / a)
(setq a (tls-vbs))
(repeat 10
(print (vbs-eval a 'rnd '()))
)
) (defun tls-join(chrs fchr / pstr)
(setq pstr "")
(foreach i Chrs (setq pstr (strcat pstr i fchr)))
(substr pstr 1 (- (strlen pstr) (strlen fchr)))
) (defun tls-vbs()
(setq Tls-VBScript (vlax-create-Object "ScriptControl"))
(vlax-put-property Tls-VBScript "language" "vbs")
Tls-VBScript
) (defun vbs-eval(vbs funname args)
(vla-eval
vbs
(strcat
(vl-prin1-to-string funname)
"("
(tls-join
(mapcar 'vl-prin1-to-string args)
","
)
")"
)
)
)
发表于 2004-10-25 14:13:00 | 显示全部楼层
这个结果出数较均匀,接近4/5。 命令: test
20
28
32
40
44
2
9
11
21
25
33
40
44
15
6
14
17
25
29
37
19
49
6
10
18
22
23
37
41
49
3
11
27
22
26
34
42
46
3
30
15
19
27
34
38
34
50
8
15
1919
命令: (defun c:test()
(repeat 50
(princ "\n")
(princ (rnd 1 50))
)
)
(defun rnd (n1 n2)
(setq ra 66791
rb 89;此值113也可得到较好的结果。
nn 66791
)
(setq imin (expt 2 31)
imax (1- imin)
)
(if (> n1 n2)
(setq n3 n1
n1 n2
n2 n3
)
)
(setq rn (atoi (substr (rtos (getvar "cdate") 2 7) 14)))
(repeat nn
(setq rn (+ (* rn ra) rb))
(if (minusp rn)
(setq rn (- rn imin))
)
)
(setq rn (rem rn imax))
(setq rn (/ rn imax 1.0))
(setq rn (+ (fix (* rn (1+ (- n2 n1)))) n1))
)
发表于 2004-10-25 14:33:00 | 显示全部楼层
看看我13楼的话,有部分是针对你这样的情况的...


15楼的方法应该是比较好的了
发表于 2007-1-3 01:25:00 | 显示全部楼层
这个办法不好
  1. ;;---------------------------------------------------------------------------;;
  2. ;; initialize the global
  3. ;;---------------------------------------------------------------------------;;
  4. (setq *SeedRand* nil)
  5. ;;---------------------------------------------------------------------------;;
  6. ;; the base function for random
  7. ;;---------------------------------------------------------------------------;;
  8. (defun rand16 (/ s)
  9.   ;; when this is used for the first time, initialize the seed
  10.   ;; from the system clock.
  11.   (if (null *SeedRand*)
  12.     (progn
  13.       (setq s (getvar "date"))      
  14.       (setq *SeedRand* (fix (* 86400 (- s (fix s)))))
  15.     )
  16.     ;; progn
  17.   )
  18.   ;; if
  19.   ;; To generate a psudo-sandom number sequence
  20.   ;; I use the routine described in Kernighan and Ritchie's
  21.   ;; "C Programming Language" second edition, p46
  22.   (setq *SeedRand* (+ (* *SeedRand* 1103515245) 12345))
  23.   ;; trim off the bits left of the 16th bits      
  24.   (logand (/ *SeedRand* 65536) 32767)
  25. )
  26. ;;---------------------------------------------------------------------------;;
  27. ;; generates a random number between min and max.                            ;;
  28. ;; min and max must be a non-negative integer smaller than 32678.            ;;
  29. ;;---------------------------------------------------------------------------;;
  30. (defun rand (umin umax / r16 range quotient remainder result)
  31.   (setq r16 (rand16))
  32.   ;; random number smaller than 32678
  33.   (setq range (+ 1 (- umax umin)))
  34.   ;; number of integers to be produced
  35.   (setq quotient (/ r16 range))
  36.   ;; result in non-neg. integer
  37.   (setq remainder (- r16 (* quotient range)))
  38.   (setq result (+ umin remainder))
  39.   result
  40. )
我用了上面的代码,比刚才稍微好一点,但也不行,郁闷啊
Autolisp做随机都不行
发表于 2007-1-3 12:39:00 | 显示全部楼层
本帖最后由 作者 于 2007-1-3 16:04:56 编辑

  1. ;;;晓东空间上讨论的结果。
  2. ;;; ==================================================================
  3. ;;; 求随机数(0~1)--by 狂刀
  4. (defun th-rnd ()                       ; 随机数种子
  5.   (* (rem (getvar "cputicks") 1e3) 1e-3)
  6. )
  7. ;;; ==================================================================
  8. ;;; 求n个a~b随机数列表----by 狂刀
  9. (defun th-rndx1-list (a b n  / c lst)   ; 随机数表
  10.   (setq c (- b a))
  11.   (repeat n
  12.     (setq lst (cons (+ a (* (th-rnd) c)) lst))
  13.   )
  14. )
  15. ;;; ==================================================================
  16. ;;; 返回在a~b內的n個雜檆整數的列表
  17. (defun th-rndx2-list (a b n  / lst c)   ; 随机整数表
  18.   (setq c (- b a))
  19.   (repeat n
  20.     (setq lst (cons (fix (+ a (* (th-rnd) c))) lst))
  21.   )
  22. )
  23. ;;; ==================================================================
  24. (defun th-rnd-single (a b)             ; 单个随机整数
  25.   (car (th-rndx2-list a b 1))
  26. )
  27. ;;; ==================================================================
发表于 2007-1-3 12:40:00 | 显示全部楼层
  1. ;;;晓东上讨论的结果
  2. ;;; ==================================================================
  3. ;;; 求随机数(0~1)--by 狂刀
  4. (defun th-rnd ()                       ; 随机数种子
  5.   (* (rem (getvar "cputicks") 1e3) 1e-3)
  6. )
  7. ;;; ==================================================================
  8. ;;; 求n个a~b随机数列表----by 狂刀
  9. (defun th-rndx1-list (a b n / c lst)   ; 随机数表
  10.   (setq c (- b a))
  11.   (repeat n
  12.     (setq lst (cons (+ a (* (th-rnd) c)) lst))
  13.   )
  14. )
  15. ;;; ==================================================================
  16. ;;; 返回在a~b內的n個雜檆整數的列表
  17. (defun th-rndx2-list (a b n / lst c)   ; 随机整数表
  18.   (setq c (- b a))
  19.   (repeat n
  20.     (setq lst (cons (fix (+ a (* (th-rnd) c))) lst))
  21.   )
  22. )
  23. ;;; ==================================================================
  24. (defun th-rnd-single (a b)             ; 单个随机整数
  25.   (car (th-rndx2-list a b 1))
  26. )
  27. ;;; ==================================================================
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-5-17 19:20 , Processed in 0.185530 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表