数学问题——调整概率
2018-03-31 22:00
351 查看
调整概率
调整[0,x)区间上的数出现的概率【题目】
假设函数Math.random()等概率随机返回一个在[0,1)范围上的
数,那么我们知道,在[0,x)区间上的数出现的概率为x
(0 < x ≤ 1)。给定一个大于0的整数k,并且可以使用
Math.random()函数,请实现一个函数依然返回在[0,1)范围上
的数,但是在[0,x)区间上的数出现的概率为xk(0 < x ≤ 1)。
public class ProbabilityXPowerK { public static double randXPower2() { return Math.max(Math.random(), Math.random()); } public static double randXPowerK(int k) { if (k < 1) { return 0; } double res = -1; for (int i = 0; i != k; i++) { res = Math.max(res, Math.random()); } return res; } public static void main(String[] args) { double range = 0.5; int times = 5000000; int count = 0; for (int i = 0; i != times; i++) { if (randXPowerK(2) < range) { count++; } } double p = (double) count / (double) times;//统计小于某概率的次数除以总次数 System.out.println("range [0," + range + "), probability: " + p); } }
相关文章推荐
- 数学概念——F 概率(经典问题)birthday paradox
- 语音识别概率问题,一段在数学之美了看到的话
- 面试中的概率问题 - 数学期望(2) - 武器升级需要的宝石数
- 数学概率问题
- 面试中的概率问题 - 数学期望(2) - 武器升级需要的宝石数
- 面试题中的概率问题 - 数学期望(3) - 求最小值过程中元素替换次数的期望
- 【数学题】概率问题之-学生的生日
- 数学问题——概率法求π的近似值
- lightoj 1408 Batting Practice (概率问题,求期望,推公式)
- Android中的数学问题
- Android软键盘弹出时布局的调整问题
- 数学问题——素数
- java算法之数学问题(位操作)
- 9.7数学与概率(二)——实现整数的乘法、减法和除法运算,只允许使用加号
- 利用线性筛法解决的数学函数或问题小汇(省选复习)
- 数学回味系列之16 - 爱因斯坦台阶问题
- codeforce Gym 100418K Cards (概率,数学)
- 数学问题——大数运算
- 调整改善Ubunt9.10的字体显示问题
- 算法:字符串消除问题的数学证明