php随机产生4乘4矩阵,matlab函数rand:产生均匀分布的随机数或矩阵的函数

rand

均匀分布的随机数或矩阵

语法

Y = rand(n)

Y = rand(m,n)

Y = rand([m n])

Y = rand(m,n,p,...)

Y = rand([m n p...])

Y = rand(size(A))

rand

s = rand('state')

描述

rand函数产生由在(0, 1)之间均匀分布的随机数组成的数组。

Y = rand(n) 返回一个n x n的随机矩阵。如果n不是数量,则返回错误信息。

Y = rand(m,n) 或 Y = rand([m n]) 返回一个m x n的随机矩阵。

Y = rand(m,n,p,...) 或 Y = rand([m n p...]) 产生随机数组。

Y = rand(size(A)) 返回一个和A有相同尺寸的随机矩阵。

【jink2005选译】----------------------------------------------------------

rand

Uniformly distributed random numbers and arrays

Syntax

Y = rand(n)

Y = rand(m,n)

Y = rand([m n])

Y = rand(m,n,p,...)

Y = rand([m n p...])

Y = rand(size(A))

rand

s = rand('state')

Description

The rand function generates arrays of random numbers whose elements are uniformly distributed in the interval (0,1).

Y = rand(n) returns an n-by-n matrix of random entries. An error message appears if n is not a scalar.

Y = rand(m,n) or Y = rand([m n]) returns an m-by-n matrix of random entries.

Y = rand(m,n,p,...) or Y = rand([m n p...]) generates random arrays.

Y = rand(size(A)) returns an array of random entries that is the same size as A.

rand, by itself, returns a scalar whose value changes each time it's referenced.

s = rand('state') returns a 35-element vector containing the current state of the uniform generator. To change the state of the generator: rand('state',s)Resets the state to s. rand('state',0)Resets the generator to its initial state. rand('state',j)For integer j, resets the generator to its j-th state. rand('state',sum(100*clock))Resets it to a different state each time.

Examples

Example 1.

R = rand(3,4) may produce  R =

0.2190    0.6793    0.5194    0.0535

0.0470    0.9347    0.8310    0.5297

0.6789    0.3835    0.0346    0.6711

This code makes a random choice between two equally probable alternatives.  if rand < .5

'heads'

else

'tails'

end

Example 2. Generate a uniform distribution of random numbers on a specified interval [a,b].

To do this, multiply the output of rand by (b-a) then add a. For example, to generate a 5-by-5 array of uniformly distributed random numbers on the interval [10,50] a = 10; b = 50;

x = a + (b-a) * rand(5)

x =

18.1106   10.6110   26.7460   43.5247   30.1125

17.9489   39.8714   43.8489   10.7856   38.3789

34.1517   27.8039   31.0061   37.2511   27.1557

20.8875   47.2726   18.1059   25.1792   22.1847

17.9526   28.6398   36.8855   43.2718   17.5861

See Also randn, randperm, sprand, sprandn

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值