如何产生大于等于0小于等于1的随机浮点数?

PDD123 2014-07-09 07:32:27
请问,如何产生大于等于0小于等于1的随机浮点数
Math.random()产生的是大于等于0小于1的随机浮点数,有什么算法可以得到我想要的结果么?
...全文
469 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-07-10
  • 打赏
  • 举报
回复
引用 8 楼 PDD123 的回复:
我有个笨办法:
		double res;
		do
		{
			res=Math.random();
		}while(res>0.999/*任何小于1.0的数*/);
		res=res/0.999;
也可以。就是手动加入1
PDD123 2014-07-10
  • 打赏
  • 举报
回复
我有个笨办法:
		double res;
		do
		{
			res=Math.random();
		}while(res>0.999/*任何小于1.0的数*/);
		res=res/0.999;
PDD123 2014-07-10
  • 打赏
  • 举报
回复
引用 4 楼 u012872817 的回复:
连续区间随机到定值的概率为0,这个闭和开又有什么区别... 就算是离散的浮点,取定值概率也小于1E-19,确定要为了这点概率去搞算法?
总觉得有点不完美.....
PDD123 2014-07-10
  • 打赏
  • 举报
回复
引用 5 楼 englishbage 的回复:
Random random = new Random();
		random.nextFloat();
这个好像也是小于1.0的。
itfenbao 2014-07-10
  • 打赏
  • 举报
回复
Random random = new Random();
		random.nextFloat();
wwzhzh 2014-07-10
  • 打赏
  • 举报
回复
连续区间随机到定值的概率为0,这个闭和开又有什么区别... 就算是离散的浮点,取定值概率也小于1E-19,确定要为了这点概率去搞算法?
PDD123 2014-07-10
  • 打赏
  • 举报
回复
也就是说没有办法转换成两边都是闭区间的平均分布的集合?
tony4geek 2014-07-10
  • 打赏
  • 举报
回复
/** * Returns a <code>double</code> value with a positive sign, greater * than or equal to <code>0.0</code> and less than <code>1.0</code>. * Returned values are chosen pseudorandomly with (approximately) * uniform distribution from that range. * * <p>When this method is first called, it creates a single new * pseudorandom-number generator, exactly as if by the expression * <blockquote><pre>new java.util.Random</pre></blockquote> This * new pseudorandom-number generator is used thereafter for all * calls to this method and is used nowhere else. * * <p>This method is properly synchronized to allow correct use by * more than one thread. However, if many threads need to generate * pseudorandom numbers at a great rate, it may reduce contention * for each thread to have its own pseudorandom-number generator. * * @return a pseudorandom <code>double</code> greater than or equal * to <code>0.0</code> and less than <code>1.0</code>. * @see java.util.Random#nextDouble() */ 等于1 得自己判断。
tony4geek 2014-07-10
  • 打赏
  • 举报
回复
Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值,是Java语言常用代码。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧