如何在R中创建一个和为1的随机向量?


为了创建一个和为1的随机向量,我们可以使用均匀分布。需要注意的是,我们应该在使用随机生成的均匀分布值的同时,包含0在向量中。查看下面的例子来了解如何做到这一点。

示例1

在线演示

> x1<-diff(c(0,runif(9),1))
> x1

输出

[1] 0.45490995 0.23826247 -0.07338489 -0.33361362 0.26125094 -0.45243689
[7] 0.05967125 0.43007076 0.04069027 0.37457976

示例

> sum(x1)

输出

[1] 1

示例2

在线演示

> x2<-diff(c(0,runif(9,1,2),1))
> x2

输出

[1] 1.84330339 -0.11622911 -0.15001654 0.07803346 -0.17353612 0.23651847
[7] -0.21121933 -0.30938763 0.44503222 -0.64249881

示例

> sum(x2)

输出

[1] 1

示例3

在线演示

> x3<-diff(c(0,runif(50,2,5),1))
> x3

输出

[1] 2.63249755 1.17230387 -0.28068787 0.58040911 -1.48530836 -0.04894802
[7] 0.66718009 0.13504265 -0.18253891 -0.49757615 1.63580429 -2.31002917
[13] 2.66256899 -2.40636756 2.03789127 0.13579276 -0.75107129 -0.54910795
[19] 1.83531581 -2.63140715 2.62522465 -0.52869058 -2.25062543 1.60028461
[25] -1.05655531 -0.59050668 0.62369564 -0.45469172 2.64448405 -2.65903604
[31] -0.27137574 0.20360002 -0.03487276 2.04351544 -0.49295944 -0.78713432
[37] 0.95944019 -1.17824997 1.03319126 -1.47786102 2.24848417 -0.68023815
[43] -0.12639009 0.26796950 0.38995293 -1.36491199 0.04071521 -0.12052314
[49] 1.67278419 -1.09555495 -2.53492849

示例

> sum(x3)

输出

[1] 1

示例4

在线演示

> x4<-diff(c(0,runif(50,1,5),1))
> x4

输出

[1] 4.76111110 -0.10304726 -0.58606251 -1.27143938 -0.75037696 1.61807009
[7] -2.09986035 -0.21588435 1.45329979 0.00682381 2.02509262 -0.89741854
[13] -0.67985485 -0.17887487 1.09860149 -0.09221847 -1.68477052 1.92539453
[19] -2.64247170 -0.50664218 3.01584790 -0.73581233 0.18149599 0.56270595
[25] -1.83958091 0.11440161 -1.25337550 3.72135514 -3.64378537 0.29757264
[31] 2.43338920 -2.58526912 0.22123174 1.13430237 -0.90486651 0.13180413
[37] -0.68087708 2.62999981 -0.80622446 1.71732123 -1.29277776 0.98628418
[43] -1.22927287 1.30083055 -3.62456305 1.55097654 0.68811226 -1.86982597
[49] 3.57758084 -1.99823149 -1.98022115

示例

> sum(x4)

输出

[1] 1

示例5

在线演示

> x5<-diff(c(0,runif(80,1,10),1))
> x5

输出

[1] 8.42562071 0.15856836 0.88732204 -4.16135064 1.19261643 1.68502334
[7] 0.50863537 -1.06085726 -6.45915220 2.89672655 4.20936999 -2.99844355
[13] -0.71929354 -1.66257319 -0.22478306 6.70140573 -5.66271131 4.49979543
[19] -1.01361957 2.61717049 -7.95695211 -0.50971643 2.58844952 5.03882897
[25] -1.27612368 -0.32153883 -2.21693088 -1.17715349 1.43755823 -0.02458623
[31] -1.52616809 3.34595533 2.39122173 -1.75032710 -6.71081024 3.41028158
[37] -2.75855553 -0.28433170 7.97386331 0.03722132 -8.18198672 0.81689544
[43] 0.25930702 5.34772736 1.11725974 -6.94093987 2.99125673 2.59722539
[49] -5.17587012 5.59641984 -6.34470593 6.42665678 -1.19258203 0.60336763
[55] -0.34540016 1.35909426 -3.51332010 2.95949805 -4.45082208 5.45244493
[61] -1.57022479 -2.99899633 -0.73599075 1.95336717 -2.88379483 1.48336521
[67] 5.12479224 -5.74638879 1.67282536 1.65580779 0.76914379 1.66121819
[73] 0.53169494 0.05074533 -7.54465584 3.37371464 -3.78883201 0.21710469
[79] 4.80254923 1.72896821 -7.66759541

示例

> sum(x5)

输出

[1] 1

示例6

在线演示

> x6<-diff(c(0,runif(120,1,1),1))
> x6

输出

[1]  1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[112] 0 0 0 0 0 0 0 0 0 0

示例

> sum(x6)

输出

[1] 1

示例7

在线演示

> x7<-diff(c(0,runif(80,2,10),1))
> x7

输出

[1]  9.38930592 -3.47836710 1.66644332 1.85283294 -4.59317154 0.95271721
[7]  -2.98216333 2.30263251 -2.96475176 1.80527139 4.80892105 -1.88803314
[13] 1.04420379 -1.49476968 -2.40857094 3.33276971 -3.98422295 4.79583164
[19] -5.87384649 4.89456969 0.33427197 0.85188895 -5.27203727 0.05409481
[25] -1.03443788 3.95668591 0.57953035 0.89713338 -0.74238113 0.85412622
[31] -5.49856754 5.53836292 0.70367813 -2.58521516 -2.59814581 6.22141925
[37] -5.27522813 -1.65110939 4.73533721 1.76802011 -0.34546355 0.98941401
[43] -6.09796223 2.54621105 1.76102782 1.73287870 -2.84811515 -3.14592800
[49] 6.21975073 -0.96255503 -6.04617256 3.77899990 -0.69299288 0.74624754
[55] -2.63900951 3.21006127 -2.83023180 3.76672022 -5.52671515 0.44058700
[61] 5.20367065 -2.63006809 -0.06715442 0.56237018 -3.39816867 6.28399892
[67] -1.85355878 -2.94284468 -0.29064851 3.12208329 0.32627920 1.96917001
[73] -4.17003788 2.37122068 0.22403718 -3.30387504 -0.19663146 -1.40730631
[79] 0.48329741 1.80690243 -4.16451766

示例

> sum(x7)

输出

[1] 1

示例8

在线演示

> x8<-diff(c(0,runif(60,2,10),1))
> x8

输出

[1] 9.21931621 0.58294577 -7.44135711 5.19907023 0.01369924 1.77189553
[7] -3.72101173 -2.79998882 3.68132082 -2.83486417 0.15357442 -0.90296071
[13] 6.75958760 -7.13870139 3.60469244 1.78479504 -3.19186756 0.14938226
[19] 4.96604995 -3.45172960 -2.39151157 3.17429835 -2.83346072 1.68768901
[25] -0.22962519 2.56437969 -2.22698581 -1.24785240 1.15321009 -2.47703167
[31] 0.61720063 3.12853185 0.06960331 2.59051673 -5.50960650 2.77217185
[37] -2.66614835 2.87751390 -1.26944648 3.64930427 -6.23307678 5.31184090
[43] -0.63337168 -5.76564089 4.21782562 -4.28152588 4.28033860 -2.69495876
[49] 1.00648568 -2.90099876 4.16414423 -0.28637033 0.87173942 -3.04191393
[55] 3.66901495 -2.29551228 1.28253756 -3.55208379 1.45253454 4.87162486
[61] -8.27923266

示例

> sum(x8)

输出

[1] 1

更新于:2020年11月23日

514 次浏览

启动你的职业生涯

通过完成课程获得认证

开始学习
广告