返回具有特定分布的整数的函数 [英] Function that returns integer with specific distribution

查看:99
本文介绍了返回具有特定分布的整数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个分布或一个函数,该函数将返回特定范围内的整数,且该数字越大,概率越低。

I am looking for a distribution or rather a function that returns an integer in a specific range with decreasing probability the higher the number.

我们说范​​围是从1到5。

Lets say the range is from 1 to 5.

85% of the time the function should return 1
8% of the time the function should return 2
4% of the time the function should return 3
2% of the time the function should return 4
1% of the time the function should return 5

另外,如果概率是按照集合分布表示的,例如正态分布或指数分布,那就太好了。

Additionally it would be great if the probabilities are according to a set distribution say normal distributed or exponentially distributed.

这样的函数是什么样的?

What would a function like this look like?

推荐答案

使用

sample.int(n, size = 1, prob = p) 

其中的概率可以使用

p <- exp(-(1:n))

或使用标准正态分布

p <- dnorm(1:n)

编辑
对于您的特定示例,使用

Edit For your specific example use

n <- 5
p <- c(0.85, 0.08, 0.04, 0.02, 0.01)

这篇关于返回具有特定分布的整数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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