matlab编程产生伪随机数
MATLAB中rand的用法

MATLAB中rand的用法在MATLAB中,rand是一个用于生成随机数的函数。
它可以生成0到1之间的均匀分布的伪随机数。
rand函数的语法如下:r = randr = rand(n)r = rand(m,n)r = rand([m,n])r = rand(m,n,p,...)其中,r是一个包含随机数的矩阵或数组,n、m、p等是指定随机数矩阵的维度。
下面是rand函数的一些常见用法:1.生成一个0到1之间的随机数:r = rand上述代码将生成一个随机数r,它的取值范围是[0,1)。
2.生成一个包含n个0到1之间的随机数的向量:r = rand(n)这将生成一个大小为n的行向量或列向量,其中的元素是0到1之间的随机数。
3.生成一个m×n大小的包含随机数的矩阵:r = rand(m,n)或者r = rand([m,n])这将生成一个m×n大小的矩阵,其中的元素是0到1之间的随机数。
可以将m和n替换为具体的数值。
4.生成一个m×n×p大小的包含随机数的多维数组:r = rand(m,n,p,...)这将生成一个m×n×p×...大小的多维数组,其中的元素是0到1之间的随机数。
可以根据需要设置更多的维度。
5.生成指定范围内的随机数:r = a + (b-a).*rand这将生成一个指定范围内的随机数,其中a和b是指定的范围边界。
6.生成服从特定概率分布的随机数:r = random('distribution',parameters)distribution可以是各种概率分布,如正态分布、泊松分布等。
parameters是用于指定分布的参数。
例如,生成一个服从均值为mu、标准差为sigma的正态分布的随机数:r = random('normal',mu,sigma)7.设置随机数种子来控制随机数生成:rng(seed)seed是要设置的种子值。
MATLAB实现伪随机码的生成及相关函数的计算

伪随机码的生成及相关函数的计算院系:信息学院专业:通信工程姓名:2013117100 **2013117101 席倩2013117103 薛珈萍(1)函数ms_generator(registers,connections)是m序列的生成函数,其中参数registers给出了移位寄存器的初始状态,connections给出了m序列的发生器代码:function seq=ms_generator(registers,connections)%registers=[0 0 0 0 1];%设置初始状态%connections=[1 0 0 1 0 1];%设置反馈逻辑n=length(connections) ;L=2^(n-1)-1;seq(1)=registers(n-1);for i=2:Lsum=0;for m=1:(n-1)sum=mod(sum+registers(m)*connections(m+1),2);endfor k=(n-1):-1:2;registers(k)=registers(k-1);endregisters(1)=sum;seq(i)=registers(n-1);end调用程序:function seq=ms_generator(registers,connections)clcclearregisters=[0 0 0 0 1];connections=[1 0 0 1 0 1];seq=ms_generator(registers,connections)结果:seq =1 0 0 0 0 1 0 0 1 0 1 1 00 1 1 1 1 1 0 0 0 1 1 0 11 1 0 1 0ans =1 0 0 0 0 1 0 0 1 0 1 1 00 1 1 1 1 1 0 0 0 1 1 0 11 1 0 1 0(2)auto_corr( )代码:function auto_correlation=auto_corr(seq)registers=[1 0 0 0 0];%设置初始状态connections=[1 0 1 0 0 1];%设置反馈逻辑seq=ms_generator(registers,connections);seq=-1*(seq*2-1);%负逻辑映射len=length(seq);temp=[seq seq];for i=0:len-1auto_correlation(i+1)=seq*(temp(i+1:i+len))';endauto_correlationplot(0:len-1,auto_correlation);结果:auto_correlation =31 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1ans =31 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1(3)cross_corr( )代码:function cross_correlation=cross_corr(seq1,seq2) registers1=[0 0 0 0 1];%设置初始状态connections1=[1 0 0 1 0 1];%设置反馈逻辑seq1=ms_generator(registers1,connections1);seq1=-1*(seq1*2-1);%负逻辑映射registers2=[1 1 1 1 1];%设置初始状态connections2=[1 1 1 1 0 1];%设置反馈逻辑seq2=ms_generator(registers2,connections2);seq2=-1*(seq2*2-1);%负逻辑映射temp=[seq2 seq2];len=length(seq2);for i=0:len-1cross_correlation(i+1)=seq1*(temp(i+1:i+len))';endauto_correlation=auto_corr(seq1)plot(0:len-1,auto_correlation,'-.',0:len-1,cross_correlation); 结果:(4)GOLD码代码function gold=gold()registers1=[1 0 0 0 0 0];connections1=[1 1 0 0 0 0 1];seq1=ms_generator (registers1,connections1);registers2=[1 0 0 0 0 0];connections2=[1 1 1 0 0 1 1];seq2=ms_generator(regerters2,connections2);temp=[seq2 seq2];len=length(seq2);for i=1:lengold(i,:)=mod(seq1+temp(i:i+len-1),2);endgold(len+1,:)=seq1;gold(len+2,:)=seq2;plot(0:len-1,auto_corr(gold(1,:)),'-',0:len-1,cross_corr(gold(1.:),gold(2,:)),'-.'); 结果:相关特性:(5)Walsh 函数:代码:function walsh = walsh(L)L=64n=log2(L);temp=[0];temp2=zeros(4,4);for i=1:ntemp2=zeros(2.^n,2.^n);temp1=inverse(temp);temp2=[temp temp;temp temp1];temp=zeros(2.^n,2.^n);temp=temp2;temp1=zeros(2.^n,2.^n);endwalsh=temp2;correlation=(walsh*2-1)*(walsh*2-1)' function result=inverse(temp)len=size(temp);for i=1:lenfor j=1:len;if(temp(i,j)==0)temp(i,j)=1;elsetemp(i,j)=0;endendendresult=temp;结果:因篇幅受限只截取其中一部分。
Matlab随机数产生的问题

Matlab随机数产生的问题2009-02-13 08:13这2天发现这个问题讨论较多,所以就搜索了一些资料。
发现自己之前的理解有些很有问题,同时欢迎大家继续讨论。
先澄清一下几个容易弄错的地方(也不一定全对)(1)用计算机产生的是“伪随机数”。
用投色子计数的方法产生真正的随机数 , 但电脑若也这样做 , 将会占用大量内存 ; 用噪声发生器或放射性物质也可产生真正的随机数 , 但不可重复 . 而用数学方法产生最适合计算机 , 这就是周期有限 , 易重复的” 伪随机数”(2)随机数的产生需要有一个随机的种子,因为用计算机产生的随机数是通过递推的方法得来的,必须有一个初始值。
(3)用同一台电脑,且在初始值和递推方法相同的情况下,可以产生相同的随机序列(由于以前每次使用randn或者rand得到都是不同值,所以曾经误以为相同的seed无法产生相同的序列)[size=2][color=red][b]一 matlab里产生随机数的方法[/b][/color][/size] matlab里和随机数有关的函数:(1) rand:产生均值为0.5、幅度在0~1之间的伪随机数(2) randn:产生均值为0、方差为1的高斯白噪声(3) randperm(n):产生1到n的均匀分布随机序列(4) normrnd(a,b,c,d):产生均值为a、方差为b大小为cXd的随机矩阵还有很多的扩展函数,不再一一列出。
不过他们都调用的是rand或者randn函数,由此可见在matlab里rand和randn是产生随机数的关键所在。
看来只有看他们的源文件了function [varargout] = randn(varargin)%%%help 文档的内容略去%%%if nargout == 0builtin('randn', varargin{:});else[varargout{1:nargout}] = builtin('randn', varargin{:});end从这里也看不出到底是怎么产生的,就只看到builtin。
伪随机序列matlab仿真

MATLAB实验报告姓名:***学号:********班级:14184111实验七伪随机序列的产生与相关特性分析一、实验目的1、了解伪随机序列的相关知识。
2、了解m 序列的相关知识,了解其相关性质。
3、学会用matlab 实现方框图描述的系统,并由此产生m 序列。
二、相关理论知识(1)伪随机序列伪随机序列, 又称伪随机码, 是一种可以预先确定并可以重复地产生和复制, 又具有随机统计特性的二进制码序列。
在现代工程实践中, 伪随机信号在移动通信、导航、雷达和保密通信、通信系统性能的测量等领域中有着广泛的应用。
例如,在连续波雷达中可用作测距信号, 在遥控系统中可用作遥控信号, 在多址通信中可用作地址信号, 在数字通信中可用作群同步信号, 还可用作噪声源以及在保密通信中的加密作用等。
伪随机发生器在测距、通信等领域的应用日益受到人们重视。
伪随机信号与随机信号的区别在于: 随机信号是不可预测的, 它在将来时刻的取值只能从统计意义上去描述;伪随机序列实质上不是随机的, 而是收发双方都知道的确定性周期信号。
之所以称其为伪随机序列, 是因为它表现出白噪声采样序列的统计特性, 在不知其生成方法的侦听者看来像真的随机序列一样。
m 序列作为一种基本的PN 序列,具有很强的系统性、规律性和相关性。
(2)m 序列的产生①线性反馈移位寄存器m 序列发生器的系统框图如图。
其中加法器为“模2 相加”运算,寄存器与反馈的每一位只有1、0 两种状态。
由于带有反馈,因此在移位脉冲作用下,移位寄存器各级的状态将不断变化,通常移位寄存器的最后一级做输出,输出序列为输出序列是一个周期序列。
其特性由移位寄存器的级数、初始状态、反馈逻辑以及时钟速率(决定着输出码元的宽度)所决定。
当移位寄存器的级数及时钟一定时,输出序列就由移位寄存器的初始状态及反馈逻辑完全确定。
当初始状态为全零状态时,移位寄存器输出全0序列。
为了避免这种情况,需设置全0 排除电路。
基于matlab的伪随机序列实现

衡量一个m序列与它的j次移位序列之间的相关 程度,并把它叫做m序列( a1 , a2 , a3 ,, a p)的自相关函 数。记作 R( j ) a a ,当采用二进制数字0和1 A 代表码元的可能取值时,R( j) A D A D D p (其中A表示 a a 0的数目,D表示 ai ai j 1的数目) 由m序列的均衡性和移位相加特性可知: j0 1 R( j ) 1 / p j 1,2,,( p 1)
由上图可以看出,寄存器状态满足: y1(i)=y1(i-1) y4(i-1); y2(i)=y1(i-1); y3(i)=y2(i-1); y4(i)=y3(i-1); 给寄存器组赋予一个非全零的初始状态,y4的输 出即可作为码周期为15的一个m序列。输入 mseq=mseries([1 0 0 1]) 可得 mseq = Columns 1 through 10 0 0 0 1 1 1 1 0 1 0 Columns 11 through 15 1 1 0 0 1
p i 1 i j i
三、伪随机序列的自相关性
i
i j
自相关程序:
a=[ 0 0 0 1 1 1 1 0 1 0 1 1 0 0 1]; b=a; m=length(b); N=100; x=1:N; for k=1:N c=xor(a,b); D=sum(c); A=m-D; R(k)=(A-D)/(A+D); plot(k,R(k),'ro') b=[b(m),b(1:m-1)]; 从图中可以看出在15、30、45、60、75、 hold on 90处得到了最大的相关特性,其他值处自 end 相关值接近于零,此仿真验证了m序列具 plot(x,R,'b') 有良好的自相关特性。 grid on
单片机matlab方式产生伪随机m序列

信息科学与技术学院通信原理课程设计课题名称:伪随机m序列发生器的设计学生姓名:张昕灏2018508087学信息科学与技术学院院:专业年级:电子信息项目2018级指导教师:田敏副教授二O—三年七月十二日完成日期:目录前言1第一章设计内容及要求21.1设计内容21.2设计要求21.3方案选择2第二章m序列的特性分析42.1m序列的原理42.2均衡特性52.3游程分布52.4线性叠加性62.5自相关特性6第三章m序列设计83.1设计流程图83.2特征多项式确定83.3本原多项式确定103.4 m 序列的最终产生<以五阶移位寄存器举例)11 第四章设计成果分析及总结134.1仿真结果分析134.2设计总结14 心得体会15 参考文献16 附录matlab 程序17 附录51 单片机实现方法18电路图18设计说明18结果验证18C51 代码及与对应matlab 代码20 数模转换输出代码:20 反馈链接状态及波形输出控制代码22 使用器件、八、-前言扩展频谱通信是一种不同于常规通信系统的新调制理论和技术,简称扩频通信[1]。
其设计思想是将待传输的信息信号用特定的扩频码扩展频谱后成为宽带信号进行传输,接收时再采用相应的技术手段将频谱压缩,恢复原来待传信息信号的带宽,从而实现通信。
扩频通信具有两个特点:传输信号的带宽远大于原始信息信号的带宽;传输信号的带宽主要有扩频码决定,此扩频码通常是伪随机码。
伪随机码(pseudo randomcode> 简称PN 码,可以人为产生与复制,具有类似白噪声的性质,相关函数具有尖锐的特性,功率谱占据很宽的频带,易于从其他信号或干扰中分离出来,具有优良的抗干扰特性,其特点是:具有尖锐的自相关函数;互相关函数值应足够小;有足够长的码周期,以确保抗侦破与抗干扰的要求;码的数量足够多,以实现码分多址的要求;平衡性好,以满足抗干扰的要求;项目上易于产生、加工、复制与控制[2]。
扩频通信的优势主要来自于伪随机码具有白噪声的统计特性。
matlab产生伪随机数,改变M序列幅值

1.用下式产生伪随机数:clc;clear;p=35;M=2^p;n=7;A=2^n+1;c=1;length=1000for i=2:length;x(1)=3;x(i)=mod(A*x(i-1)+c,M);endy=x/M;plot([1:length],y,'.')这种产生随机数的方法为混合同余法,当M=2^35,n=21,c=1时,相关系数接近于零。
但当n取值过大时,占用机器时间增加。
Coveyou给出的一组参数M=2^35,n=7,c=1,虽n取值不大,却能有效的减小序列的相关系数,且不占用过多的机器时间。
2.①用给出的等式产生M序列clc;clear;x(1)=0;x(2)=1;x(3)=0;x(4)=1;x(5)=0;x(6)=1;x(7)=0;x(8)=1;x(9)=0;length=500for i=10:500;x(i)=xor(x(i-4),x(i-9));endplot(x)分析:首先对x(1)到x(9)随即赋值,系数都为1,随后的变量由等式计算出来,由图可知,该序列的循环周期很长,0和1出现的概率基本相同。
②以此序列为基础产生逆序列clc;clear;x(1)=0;x(2)=1;x(3)=0;x(4)=1;x(5)=0;x(6)=1;x(7)=0;x(8)=1;x(9)=0;length=500for i=10:500;x(i)=xor(x(i-4),x(i-9));endy(i)=mod(i,2)for i=1:500z(i)=xor(x(i),y(i))endplot(z)逆M序列是M序列和方波序列复合结果,也是周期较长的随机序列,0和1出现概率相同。
③并将逆序M的幅值变为-a和+aclc;clear;x(1)=0;x(2)=1;x(3)=0;x(4)=1;x(5)=0;x(6)=1;x(7)=0;x(8)=1;x(9)=0;length=500for i=10:500;x(i)=xor(x(i-4),x(i-9));endy(i)=mod(i,2)for i=1:500z(i)=xor(x(i),y(i))enda=2for i=1:500b(i)=a*(1-2*z(i))endplot(b)分析:取a=2,幅值变为-2和+2。
(完整版)Matlab各种随机数设置

Matlab 各种随机数设置randn(伪随机正态分布数)Normally distributed pseudorandom numbersSyntaxr = randn(n)randn(m,n)randn([m,n])randn(m,n,p,...)randn([m,n,p,...])randn(size(A))r = randn(..., 'double')r = randn(..., 'single')Descriptionr = randn(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard normal distribution. randn(m,n) or randn([m,n]) returns an m-by-n matrix. randn(m,n,p,...) or randn([m,n,p,...]) returns an m-by-n-by-p-by-... array. randn returns a scalar. randn(size(A)) returns an array the same size as A.r = randn(..., 'double') or r = randn(..., 'single') returns an array of normal values of the specified class.Note The size inputs m, n, p, ... should be nonnegative integers. Negative integers are treated as 0.The sequence of numbers produced by randn is determined by the internal state of the uniform pseudorandom number generator that underlies rand, randi, and randn. randn uses one or more uniform values from that default stream to generate each normal value. Control the default stream using its properties and methods.Note In versions of MATLAB prior to 7.7 (R2008b), you controlled the internal state of the random number stream used by randn by calling randn directly with the 'seed' or 'state' keywords.ExamplesGenerate values from a normal distribution with mean 1 and standard deviation 2.r = 1 + 2.*randn(100,1);Generate values from a bivariate normal distribution with specified mean vector and covariance matrix.mu = [1 2];Sigma = [1 .5; .5 2]; R = chol(Sigma);z = repmat(mu,100,1) + randn(100,2)*R;Replace the default stream at MATLAB startup, using a stream whose seed is based on clock, so that randn will return different values in different MATLAB sessions. It is usually not desirable to do this more than once per MATLAB session.RandStream.setDefaultStream ...(RandStream('mt19937ar','seed',sum(100*clock)));randn(1,5)Save the current state of the default stream, generate 5 values, restore the state, and repeat the sequence.defaultStream = RandStream.getDefaultStream;savedState = defaultStream.State;z1 = randn(1,5)defaultStream.State = savedState;z2 = randn(1,5) % contains exactly the same values as z1Normrnd (随机正态分布数)Normal random numbersSyntaxR = normrnd(mu,sigma)R = normrnd(mu,sigma,m,n,...)R = normrnd(mu,sigma,[m,n,...])DescriptionR = normrnd(mu,sigma) generates random numbers from the normal distribution with mean parameter mu and standard deviation parameter sigma. mu and sigma can be vectors, matrices, or multidimensional arrays that have the same size, which is also the size of R. A scalar input for mu or sigma is expanded to a constant array with the same dimensions as the other input.R = normrnd(mu,sigma,m,n,...) or R = normrnd(mu,sigma,[m,n,...]) generates an m-by-n-by-... array. The mu, sigma parameters can each be scalars or arrays of the same size as R.Examplesn1 = normrnd(1:6,1./(1:6))n1 =2.1650 2.31343.02504.0879 4.8607 6.2827n2 = normrnd(0,1,[1 5])n2 =0.0591 1.7971 0.2641 0.8717 -1.4462n3 = normrnd([1 2 3;4 5 6],0.1,2,3)n3 =0.9299 1.9361 2.96404.12465.0577 5.9864randperm (RandStream) (区域内的所有整数的随机分布)Random permutationrandperm(s,n)Descriptionrandperm(s,n) generates a random permutation of the integers from 1 to n. For example, randperm(s,6) might be [2 4 5 6 1 3]. randperm(s,n) uses random values drawn from the random number stream s.betarnd (贝塔分布)贝塔分布是一个作为伯努利分布和二项式分布的共轭先验分布的密度函数SyntaxR = betarnd(A,B)R = betarnd(A,B,m,n,...)R = betarnd(A,B,[m,n,...])DescriptionR = betarnd(A,B) generates random numbers from the beta distribution with parameters specified by A and B. A and B can be vectors, matrices, or multidimensional arrays that have the same size, which is also the size of R. A scalar input for A or B is expanded to a constant array with the same dimensions as the other input.R = betarnd(A,B,m,n,...) or R = betarnd(A,B,[m,n,...]) generates an m-by-n-by-... array containing random numbers from the beta distribution with parameters A and B. A and B can each be scalars or arrays of the same size as R.Examplesa = [1 1;2 2];b = [1 2;1 2];r = betarnd(a,b)r =0.6987 0.61390.9102 0.8067r = betarnd(10,10,[1 5])r =0.5974 0.4777 0.5538 0.5465 0.6327r = betarnd(4,2,2,3)r =0.3943 0.6101 0.57680.5990 0.2760 0.5474Binornd (二项式分布)二项分布(binomial distribution)就是对这类只具有两种互斥结果的离散型随机事件的规律性进行描述的一种概率分布。