R软件一个显著的优点是它提供了丰富的随机数发生器,比SAS、Matlab方面很多,比Excel更不知方便到哪里去了。这无疑为统计学、工程学以及寿险精算学提供了很大的方便,比如我们要产生200个服从正态分布的随机数,在R里面用只需要键入“rnorm(200)”即可得到。操作如下:

>rnorm(200)

[1] -0.12071839  0.05257376 -0.23671186 -0.71413741  2.17362525 -0.25102710
[7] -1.41124223  1.78816754  0.95697026 -1.73518078 -0.71141662  1.27847876
[13] -1.50472436 -0.55267534  0.46788900  0.32977779  0.37641009 -1.22411512
[19] -1.82099431 -1.50026288……

rnorm()函数更通用的用法是rnorm(n,mean,sd),其中n为要产生随机数的个数,mean为所产生随机数的总体所具有的均值,sd是所产生的随机数的总体所具有的标准差。

要产生服从均值为2,标准差为3的随机数100个,在R中:

>rnorm(n=100,mean=2,sd=3)

当然也可以更简便:

>rnorm(100,2,3)


类似的还可以在stat包里得到更多分布的随机数,一些对应函数如下:

rexp The Exponential Distribution

rf The F Distribution

rgamma The Gamma Distribution

rgeom The Geometric Distribution

rhyper The Hypergeometric Distribution

rlnorm The Log Normal Distribution

rlogis The Logistic Distribution

rmultinom The Multinomial Distribution

rnbinom The Negative Binomial Distribution

rnorm The Normal Distribution

rpois The Poisson Distribution

rsignrank Distribution of the Wilcoxon Signed Rank Statistic

rt The Student t Distribution

runif The Uniform Distribution

rweibull The Weibull Distribution

rwilcox Distribution of the Wilcoxon Rank Sum Statistic

rexp The Exponential Distribution

rf The F Distribution

rgamma The Gamma Distribution

rgeom The Geometric Distribution

rhyper The Hypergeometric Distribution

rlnorm The Log Normal Distribution

rlogis The Logistic Distribution

rmultinom The Multinomial Distribution

rnbinom The Negative Binomial Distribution

rnorm The Normal Distribution

rpois The Poisson Distribution

rsignrank Distribution of the Wilcoxon Signed Rank Statistic

rt The Student t Distribution

runif The Uniform Distribution

rweibull The Weibull Distribution

rwilcox Distribution of the Wilcoxon Rank Sum Statistic

用法大致一样。


卡方呢??

解答:

Pearson (χ2)
用这个函数rchisq(n, df)

rnorm(n,mean=x,sd=y)
n:生成n个随机数
mean:随机数的均值
sd:标准差
 
rnorm(10,mean=0,sd=1)   #产生10个平均值为0,方差为1的随机数
 [1]  0.08934727 -0.95494386 -0.19515038  0.92552126  0.48297852 -0.59631064 -2.18528684
[8] -0.67486594 -2.11906119 -1.26519802
 
runif(10,min=3,max=11)  #产生10个最小值为3,最大值为11
 [1]  3.604562  7.275414  8.130853  7.205915  3.314251  7.366879  5.982105 10.690419
 [9]  5.058733  4.663613

r 随机数的更多相关文章

  1. R语言:常用函数【转】

    数据结构 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 data.frame:数据框 c:连接为向量或列表len ...

  2. 简单介绍一下R中的几种统计分布及常用模型

    统计学上分布有很多,在R中基本都有描述.因能力有限,我们就挑选几个常用的.比较重要的简单介绍一下每种分布的定义,公式,以及在R中的展示. 统计分布每一种分布有四个函数:d――density(密度函数) ...

  3. R9—R常用函数分类汇总

    数据结构 一.数据管理 vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量 list:列表 data.frame:数据框 c:连接为向量或列表 ...

  4. R语言编程艺术(4)R对数据、文件、字符串以及图形的处理

    本文对应<R语言编程艺术> 第8章:数学运算与模拟: 第10章:输入与输出: 第11章:字符串操作: 第12章:绘图 =================================== ...

  5. 【R】R语言常用函数

    R语言常用函数 基本 一.数据管理vector:向量 numeric:数值型向量 logical:逻辑型向量character:字符型向量 list:列表 data.frame:数据框c:连接为向量或 ...

  6. R中基本函数学习[转载]

    转自:https://www.douban.com/note/511740050/ 1.数据管理 numeric:数值型向量 logical:逻辑型向量 character:字符型向量list:列表 ...

  7. R 基本函数总结

    基本一.数据管理 vector:向量 numeric:数值型向量 logical:逻辑型向量 character:字符型向量 list:列表 data.frame:数据框 c:连接为向量或列表 len ...

  8. R语言实战(第二版)-part 1笔记

    说明: 1.本笔记对<R语言实战>一书有选择性的进行记录,仅用于个人的查漏补缺 2.将完全掌握的以及无实战需求的知识点略去 3.代码直接在Rsudio中运行学习 R语言实战(第二版) pa ...

  9. Xdebug文档(四)函数跟踪

    Xdebug能让你把所有函数调用,包括参数和返回值以不同的格式记录到文件中. 这些号称“函数跟踪”功能能帮助你面对一个新应用程序,亦或者在程序运行时你想弄清楚它在做什么.函数跟踪功能可以选择性地显示函 ...

随机推荐

  1. struts2中的session、request 、和action往页面中传值的方法

    ActionContext.getContext().put("list", list); ActionContext.getContext().getValueStack().p ...

  2. ssm数据库中文乱码问题

    (1)详解Spring中的CharacterEncodingFilter--forceEncoding为true    <a href="http://www.cnblogs.com/ ...

  3. git 修改文件内容

    在安装Git和创建版本库的时候,我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容  [root@node1 gi ...

  4. Java 对象的序列化和反序列化

    先创建一个实现了Serializable接口的对象 import java.io.Serializable; /** * 可序列化Person对象. * @author Ramer * Sep 18, ...

  5. Linux命令Find实例

    转自: http://www.tecmint.com/35-practical-examples-of-linux-find-command/ 35 Practical Examples of Lin ...

  6. JAVA4种线程池的使用

    Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...

  7. android:AlertDialog控件

    AlertDialog 可以在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素之上 的,能够屏蔽掉其他控件的交互能力,因此一般 AlertDialog 都是用于提示一些非常重要的 内容或者警告 ...

  8. SharePoint 多行文本字段设置默认值

    前言 最近有这样一个需求,创建一个表单,里面有多行文本字段,但是要求内容默认带一个表格,这样用户新建项目的时候,就可以直接填表格了. 好吧,这样的需求我们可以通过JavaScript实现. 1.默认的 ...

  9. how to use boost program options

    From:  http://www.radmangames.com/programming/how-to-use-boost-program_options If it so happens that ...

  10. 微软BI 之SSRS 系列 - 如何在 MDX 查询中获取有效的 MEMBER 成员属性作为参数传递

    这篇小文章的来源是 天善问答,比如在报表中要根据点击某一个成员名称然后作为参数传递给自身报表或者下一张报表,这个在普通的 SQL 查询中没有任何问题.但是在 MDX 中查询是有区别的,比如在 MDX ...