刘玉刚:matlab产生随机数
潘华山的回答:
a=[2 3 3 4 5 6 7 8 9 11]; b=a(randperm(numel(a)); b就是随机生成的数据。
石康康的回答:
a=[2 3 3 4 5 6 7 8 9 11]; b=a(randperm(numel(a)); b就是随机生成的数据。
让爱从来的回答:
matlab用命令rand可以产生0-1的随机数,用线性变换即可, y=ax+b x=0,y=15 x=1,y=30 a=15,b=15, 如果你要的是整数,取整,用round, >> rand(1,50) ans = columns 1 through 11 0.9134 0.6324 0.0975 0.2785 0.5469 0.9575 0.9649 0.1576 0.9706 0.9572 0.4854 columns 12 through 22 0.8003 0.1419 0.4218 0.9157 0.7922 0.9595 0.6557 0.0357 0.8491 0.9340 0.6787 columns 23 through 33 0.7577 0.7431 0.3922 0.6555 0.1712 0.7060 0.0318 0.2769 0.0462 0.0971 0.8235 columns 34 through 44 0.6948 0.3171 0.9502 0.0344 0.4387 0.3816 0.7655 0.7952 0.1869 0.4898 0.4456 columns 45 through 50 0.6463 0.7094 0.7547 0.2760 0.6797 0.6551 >> ans*15+15 ans = columns 1 through 10 28.7006 24.4854 16.4631 19.1775 23.2032 29.3626 29.4733 17.3642 29.5589 29.3575 columns 11 through 20 22.2806 27.0042 17.1283 21.3264 28.7360 26.8831 29.3924 24.8361 15.5357 27.7369 columns 21 through 30 29.0099 25.1810 26.3661 26.1470 20.8834 24.8322 17.5678 25.5907 15.4775 19.1538 columns 31 through 40 15.6926 16.4570 27.3519 25.4224 19.7565 29.2533 15.5167 21.5812 20.7234 26.4828 columns 41 through 50 26.9280 17.8031 22.3465 21.6838 24.6947 25.6405 26.3203 19.1404 25.1955 24.8265 >> round(ans) ans = columns 1 through 18 29 24 16 19 23 29 29 17 30 29 22 27 17 21 29 27 29 25 columns 19 through 36 16 28 29 25 26 26 21 25 18 26 15 19 16 16 27 25 20 29 columns 37 through 50 16 22 21 26 27 18 22 22 25 26 26 19 25 25 当然,这个方法的随机数分布不是均匀的,边界有问题, 你如说你要生成的随机数有15中可能 rand产生一个你判断它是否小于1/15*i,i=1,2,3,...15大于1/15*(i-1),是的话,这个数就是i 没看清题,不好意思,同楼上,x、y没有约束,如果x、y也是正整数,你随机出来,x+y的值,然后,x就是1到x+y在随机一次,y就是差,不过要满足某种分布还不知道如何做