选择具有概率的特定数字 [英] Choose specific number with probability

查看:12
本文介绍了选择具有概率的特定数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择具有特定概率的数字p?

How can one choose a number with a specific probability p?

假设我们必须在 {0, 1} 之间进行选择,而 p 代表选择 1 的概率.

Say we must choose between {0, 1} and the probability p stands for choosing 1.

所以当 p=0.8 我们选择 1 80% 和 0 20%.

So when p=0.8 we choose 1 with 80% and 0 with 20%.

R 中有一个简单的解决方案吗?

Is there a simple solution in R for this?

推荐答案

看看sample 函数.

> set.seed(1)
> sample(c(0,1), size=10, replace=TRUE, prob=c(0.2,0.8))
 [1] 1 1 1 0 1 0 0 1 1 1

您可以从帮助文件中阅读:

From the helpfile you can read:

samplex 的元素中获取指定大小的样本,使用替换或不替换.

sample takes a sample of the specified size from the elements of x using either with or without replacement.

sample 中的参数 prob 充当 ...

and the argument prob in sample acts as ...

一个概率权重向量,用于获取被采样向量的元素.

A vector of probability weights for obtaining the elements of the vector being sampled.

这篇关于选择具有概率的特定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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