当前位置: 首页 > 知识库问答 >
问题:

如何生成带小数点的随机数

端木飞
2023-03-14
I would like to generate random numbers between 1 & 10, with a decimal place of 1 
(e.g. 1.5, 9.4, 6.3, 2.9)

I would like to generate random numbers between 1 & 10, with 2 decimal places
(e.g. 1.51, 9.46, 6.32, 2.93)

I would like to generate random numbers between 1 & 10, with 3 decimal places

    (e.g. 1.512, 9.346, 6.329, 2.935)

一种解决方案是使用一个仅用于小数点的随机生成器,然后与数字连接。有没有更简单的方法来实现这一点?

共有3个答案

呼延才俊
2023-03-14

一个明显的建议是生成一个整数,并将其适当地除以。因此,对于3DPs,您可以生成一个介于1000和9999之间的数字(或10000,取决于您想要的确切界限),然后将其除以10000。

我建议你在实际除法中使用BigDecimal,以精确地保留小数。

扶誉
2023-03-14

1)
(int)((Math.random()*90) 10) / 10.0
2)
(int)((Math.random()*900) 100) / 100.03)
(int)((Math.random()*9000) 1000) / 1000.0

陶瀚玥
2023-03-14

虽然BigDecimal的解决方案更灵活,但这里有一个使用int和双精度的更简单的解决方案。这个方法接受一个Random对象,一个上限和下限以及一些小数位数,并返回一个相应格式化的字符串:

/**
 * Generate a decimal string representation of a random number within the
 * supplied bounds.
 * 
 * @param random
 *            the random object (if null, a new one will be created)
 * @param lowerBound
 *            the lower bound, inclusive
 * @param upperBound
 *            the upper bound, inclusive
 * @param decimalPlaces
 *            the decimal places of the result
 * @return the formatted string
 */
public static String getRandomValue(final Random random,
    final int lowerBound,
    final int upperBound,
    final int decimalPlaces){

    if(lowerBound < 0 || upperBound <= lowerBound || decimalPlaces < 0){
        throw new IllegalArgumentException("Put error message here");
    }

    final double dbl =
        ((random == null ? new Random() : random).nextDouble() //
            * (upperBound - lowerBound))
            + lowerBound;
    return String.format("%." + decimalPlaces + "f", dbl);

}

测试代码:

final Random rnd = new Random();

for(int decpl = 0; decpl < 3; decpl++){
    for(int low = 0; low < 2; low++){
        for(int high = low + 1; high < low + 3; high++){
            System.out.println("Random Value between " + low + " and "
                + high + " with " + decpl + " decimal places:");
            System.out.println(getRandomValue(rnd, low, high, decpl));
        }
    }
}

输出:

Random Value between 0 and 1 with 0 decimal places:
0
Random Value between 0 and 2 with 0 decimal places:
1
Random Value between 1 and 2 with 0 decimal places:
1
Random Value between 1 and 3 with 0 decimal places:
3
Random Value between 0 and 1 with 1 decimal places:
0.6
Random Value between 0 and 2 with 1 decimal places:
1.5
Random Value between 1 and 2 with 1 decimal places:
1.1
Random Value between 1 and 3 with 1 decimal places:
1.9
Random Value between 0 and 1 with 2 decimal places:
0.52
Random Value between 0 and 2 with 2 decimal places:
0.82
Random Value between 1 and 2 with 2 decimal places:
1.75
Random Value between 1 and 3 with 2 decimal places:
1.10
 类似资料:
  • 我想从randomInts(int-num,int-start,int-end)中获取随机数,并将这些数放入数组。 我试着做

  • 本文向大家介绍如何生成一个随机数?相关面试题,主要包含被问及如何生成一个随机数?时的应答技巧和注意事项,需要的朋友参考一下  

  • random 生成随机数包 文档:https://www.npmjs.com/package/random 安装:npm install --save random 封装代码: app / extend / context.js // 导入 jwt const jwt = require('jsonwebtoken') // 导入随机数包 const random = require('rando

  • 问题 你需要生成在一定范围内的随机数。 解决方案 使用 JavaScript 的 Math.random() 来获得浮点数,满足 0<=X<1.0 。使用乘法和 Math.floor 得到在一定范围内的数字。 probability = Math.random() 0.0 <= probability < 1.0 # => true # 注意百分位数不会达到 100。从 0 到 100 的范围实

  • 问题内容: 在java中如何生成随机数? 问题答案: 在Java 1.7或更高版本中,执行此操作的标准方法如下: 请参阅相关的JavaDoc。这种方法的优点是不需要显式初始化java.util.Random实例,如果使用不当,可能会引起混乱和错误。 但是,相反,没有办法明确设置种子,因此在有用的情况下(例如测试或保存游戏状态或类似情况),很难重现结果。在这种情况下,可以使用下面显示的Java 1.

  • 我想用骰子做一个游戏,我需要在其中加入随机数(以模拟骰子的侧面。我知道如何在1到6之间进行)。使用 不能很好地工作,因为当我运行程序几次时,我得到的输出是: 所以我想要一个每次都会生成不同随机数的命令,而不是连续5次生成相同的随机数。是否有命令可以执行此操作?