生成3个随机数,它们在R中的总和为1 [英] Generate 3 random number that sum to 1 in R

查看:212
本文介绍了生成3个随机数,它们在R中的总和为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建3个(非负)准随机数,它们加起来等于1,并一遍又一遍地重复.

I am hoping to create 3 (non-negative) quasi-random numbers that sum to one, and repeat over and over.

基本上,我试图在许多试验中将某物分成三个随机部分.

Basically I am trying to partition something into three random parts over many trials.

虽然我知道

a = runif(3,0,1)

我本以为可以在下一个runif中将1-a用作最大值,但这似乎很混乱.

I was thinking that I could use 1-a as the max in the next runif, but it seems messy.

但是这些当然不等于一.有什么想法,哦,明智的stackoverflow-ers?

But these of course don't sum to one. Any thoughts, oh wise stackoverflow-ers?

推荐答案

仅从(0,1)中随机抽取2位数字,如果假设其ab,则得到:

just random 2 digits from (0, 1) and if assume its a and b then you got:

rand1 = min(a, b)
rand2 = abs(a - b)
rand3 = 1 - max(a, b)

这篇关于生成3个随机数,它们在R中的总和为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆