Java 保证的 6 位随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23623098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assured 6 digit random number
提问by Govind Singh
I have to Generate a 6 digit Random Number. The below is the Code that I have done so far. It works fine but some time its giving 7 digitsin place of 6 digits.
The main question is why?
How do I generate an assured 6 digit random number?
我必须生成一个 6 位随机数。以下是我到目前为止所做的代码。它工作正常,但有时它会给出7 位数字而不是6 位数字。
主要问题是为什么?
如何生成一个有保证的 6 位随机数?
val ran = new Random()
val code= (100000 + ran.nextInt(999999)).toString
采纳答案by merlin2011
If ran.nextInt()
returns a number larger than 900000
, then the sum will be a 7 digit number.
如果ran.nextInt()
返回大于 的数字900000
,则总和将为 7 位数字。
The fix is to make sure this does not happen. Since Random.nextInt(n)
returns a number that is less than n
, the following will work.
解决方法是确保不会发生这种情况。由于Random.nextInt(n)
返回一个小于 的数字,n
以下将起作用。
val code= (100000 + ran.nextInt(900000)).toString()
回答by Dmitry Zagorulkin
It's because nextInt()
Returns a pseudorandom, uniformly distributed int
value between 0 (inclusive)and the specified value (exclusive)
这是因为nextInt()
返回一个伪随机的、均匀分布int
在0(含)和指定值(不含)之间的值
You have to decrease your right border on one.
你必须减少你的右边框之一。
回答by shakim
min=100000
max=999999
ans=rand()%(max-min)+min
回答by Ulan
val code= (100000 + ran.nextInt(999999)).toString
The problem is ran.nextInt(999999)
might return number greater than 899999, which would result in 7-digit-number together if you add 100000.
问题是ran.nextInt(999999)
可能返回大于 899999 的数字,如果添加 100000,这将导致 7 位数字。
Try change it to
尝试将其更改为
val code= (100000 + ran.nextInt(899999)).toString
This will ensure your random number to be more than or equal to 100000 and less than or equal to 999999.
这将确保您的随机数大于或等于 100000 且小于或等于 999999。
回答by elm
Another approach, for
另一种方法,对于
import scala.util.Random
val rand = new Random()
consider a vector of 6
random digits,
考虑一个6
随机数字向量,
val randVect = (1 to 6).map { x => rand.nextInt(10) }
Then, cast the vector onto an integral value,
然后,将向量转换为整数值,
randVect.mkString.toLong
This proceeding is general enough to cope with any number of digits. If Long
cannot represent the vector, consider BigInt
.
这个过程足够通用,可以处理任意数量的数字。如果Long
不能表示向量,考虑BigInt
。
Update
更新
Moreover, wrap it into an implicit class, noting that the first digit ought not be zero,
此外,将其包装成一个隐式类,注意第一个数字不应为零,
implicit class RichRandom(val rand: Random) extends AnyVal {
def fixedLength(n: Int) = {
val first = rand.nextInt(9)+1
val randVect = first +: (1 until n).map { x => rand.nextInt(10) }
BigInt(randVect.mkString)
}
}
so it can be used as
所以它可以用作
scala> rand.fixedLength(6)
res: scala.math.BigInt = 689305
scala> rand.fixedLength(15)
res: scala.math.BigInt = 517860820348342
回答by Peter
If you want a random number which can start with zeros, consider this:
如果您想要一个可以以零开头的随机数,请考虑:
import scala.util.Random
val r = new Random()
(1 to 6).map { _ => r.nextInt(10).toString }.mkString
回答by Praveen R
import scala.util.Random
math.ceil(Random.nextFloat()*1E6).toInt
回答by Xavier Guihot
Starting Scala 2.13
, scala.util.Random
provides:
开始Scala 2.13
,scala.util.Random
提供:
def between(minInclusive: Int, maxExclusive: Int): Int
def between(minInclusive: Int, maxExclusive: Int): Int
which used as follow, generates a 6-digit Int
(between 100_000
(included) and 1_000_000
(excluded)):
其用法如下,生成一个 6 位数字Int
(介于100_000
(包括)和1_000_000
(排除)之间):
import scala.util.Random
Random.between(100000, 1000000) // in [100000, 1000000[