matlab产生随机数

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就是差,不过要满足某种分布还不知道如何做