小编Ush*_*sha的帖子

不使用任何外部函数生成随机数

这是我最近参加的一次访谈中提出的问题.

据我所知,两个数字之间的随机数可以生成如下

public static int rand(int low, int high) {
    return low + (int)(Math.random() * (high - low + 1));
}
Run Code Online (Sandbox Code Playgroud)

但是在这里我使用Math.random()来生成0到1之间的随机数,并使用它来帮助我在低和高之间生成.有没有其他方法可以在不使用外部功能的情况下直接进行?

c c++ java algorithm data-structures

17
推荐指数
3
解决办法
2万
查看次数

标签 统计

algorithm ×1

c ×1

c++ ×1

data-structures ×1

java ×1