Java:0中的随机长数<;=x<;n范围
Random类有一个在给定范围内生成随机整数的方法。例如:Java:0中的随机长数<;=x<;n范围,java,random,range,long-integer,Java,Random,Range,Long Integer,Random类有一个在给定范围内生成随机整数的方法。例如: Random r = new Random(); int x = r.nextInt(100); public long randLong(long min, long max) { return (new java.util.Random().nextLong() % (max - min)) + min; } Random random = new Random(System.currentTimeMillis(
Random r = new Random();
int x = r.nextInt(100);
public long randLong(long min, long max) {
return (new java.util.Random().nextLong() % (max - min)) + min;
}
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
这将生成大于或等于0且小于100的整数。我想对长数字做完全相同的处理
long y = magicRandomLongGenerator(100);
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
Random类只有nextLong(),但它不允许设置范围。在一个范围内生成一个数字的标准方法(没有实用工具方法)是只对该范围使用double:
long range = 1234567L;
Random r = new Random()
long number = (long)(r.nextDouble()*range);
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
将为您提供一个介于0(包含)和范围(独占)之间的长范围。类似地,如果您想要一个介于x和y之间的数字:
long x = 1234567L;
long y = 23456789L;
Random r = new Random()
long number = x+((long)(r.nextDouble()*(y-x)));
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
将为您提供从1234567(含)到123456789(不含)的长途电话
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
注意:检查括号,因为强制转换为long比乘法具有更高的优先级。从Java7(或Android API Level 21=5.0+)开始,您可以直接使用
ThreadLocalRandom.current().nextLong(n)
(用于0≤ xThreadLocalRandom.current().nextLong(m,n)
(对于m≤ x Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
如果您坚持使用Java6(或Android 4.x),您需要使用外部库(例如
org.apache.commons.math3.random.RandomDataGenerator.getRandomGenerator().nextLong(0,n-1)
,请参阅的答案),或者实现您自己的nextLong(n)
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
根据nextInt
实现如下
public int nextInt(int n) {
if (n<=0)
throw new IllegalArgumentException("n must be positive");
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);
int bits, val;
do {
bits = next(31);
val = bits % n;
} while(bits - val + (n-1) < 0);
return val;
}
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
从上的页面:
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
nextLong方法由Random类实现,就像通过:
public long nextLong() {
return ((long)next(32) << 32) + next(32);
}
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
例如,要获得35位范围。使用
r.nextDouble()
的方法应使用:
long number = (long) (rand.nextDouble()*max);
long number = x+(((long)r.nextDouble())*(y-x));
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
上述方法非常有效。如果您使用的是ApacheCommons(org.apache.commons.math.random),请查看RandomData。它有一个方法:nextLong(长下,长上)
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
使用“%”运算符
resultingNumber = (r.nextLong() % (maximum - minimum)) + minimum;
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
通过使用“%”运算符,我们将余数除以最大值。这使我们只剩下从0(包括)到除数(不包括)的数字
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
例如:
Random r = new Random();
int x = r.nextInt(100);
public long randLong(long min, long max) {
return (new java.util.Random().nextLong() % (max - min)) + min;
}
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
//使用系统时间作为种子值以获得一个好的随机数
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
Random Random=new Random(System.currentTimeMillis());
长x;
做{
x=random.nextLong();
}while(xn);
//循环,直到得到一个大于或等于0且小于n的数字
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
他有一个方法
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
如果您需要0以外的原点,则它也具有。传递原点(包含)和边界(独占)
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
具有相同的
nextLong
方法,并允许您选择一个种子,如果您想要一个可复制的数字序列。进一步改进kennytm的答案:考虑Java 8中的实际实现的子类实现将是:
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
public class MyRandom extends Random {
public long nextLong(long bound) {
if (bound <= 0) {
throw new IllegalArgumentException("bound must be positive");
}
long r = nextLong() & Long.MAX_VALUE;
long m = bound - 1L;
if ((bound & m) == 0) { // i.e., bound is a power of 2
r = (bound * r) >> (Long.SIZE - 1);
} else {
for (long u = r; u - (r = u % bound) + m < 0L; u = nextLong() & Long.MAX_VALUE);
}
return r;
}
}
公共类MyRandom扩展了Random{
公共长下长(长绑定){
如果(绑定>(Long.SIZE-1);
}否则{
对于(长u=r;u-(r=u%bound)+m<0L;u=nextLong()&long.MAX_值);
}
返回r;
}
}
这个怎么样:
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
public static long nextLong(@NonNull Random r, long min, long max) {
if (min > max)
throw new IllegalArgumentException("min>max");
if (min == max)
return min;
long n = r.nextLong();
//abs (use instead of Math.abs, which might return min value) :
n = n == Long.MIN_VALUE ? 0 : n < 0 ? -n : n;
//limit to range:
n = n % (max - min);
return min + n;
}
public static long nextLong(@NonNull Random r,long min,long max){
如果(最小值>最大值)
抛出新的IllegalArgumentException(“最小值>最大值”);
如果(最小值==最大值)
返回最小值;
长n=r.nextLong();
//abs(使用而不是Math.abs,它可能返回最小值):
n=n==Long.MIN_值?0:n<0?-n:n;
//限制范围:
n=n%(最大-最小);
返回min+n;
}
?如果您想要在[0,
m
范围内均匀分布的伪随机长序列,请尝试使用模运算符和绝对值方法,并结合nextLong()
方法,如下所示:
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
Math.abs(rand.nextLong()) % m;
其中,rand
是您的随机对象
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
模运算符将两个数字相除,并输出这些数字的剩余部分。例如,3%2
是1
,因为3和2的剩余部分是1
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
由于nextLong()
生成一个均匀分布的伪随机长序列,其范围为[-(2^48),2^48)(或在该范围内的某个地方),因此需要取其绝对值。如果不取,则nextLong()
方法的模有50%的机会返回一个超出范围的负值[0,m
)
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
您最初请求的是[0100]范围内的均匀分布伪随机长序列。以下代码执行此操作:
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
Math.abs(rand.nextLong()) % 100;
下面的方法将返回1000000000到99999999之间的值
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
long min = 1000000000L
long max = 9999999999L
public static long getRandomNumber(long min, long max){
Random random = new Random();
return random.nextLong() % (max - min) + max;
}
从Java8API
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
从API文档中获取实际实现可能更容易
他们正在使用它来生成长流。您的原点可以是问题中的“0”
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
long nextLong(long origin, long bound) {
long r = nextLong();
long n = bound - origin, m = n - 1;
if ((n & m) == 0L) // power of two
r = (r & m) + origin;
else if (n > 0L) { // reject over-represented candidates
for (long u = r >>> 1; // ensure nonnegative
u + m - (r = u % n) < 0L; // rejection check
u = nextLong() >>> 1) // retry
;
r += origin;
}
else { // range not representable as long
while (r < origin || r >= bound)
r = nextLong();
}
return r;
}
long nextLong(长起点、长边界){
长r=nextLong();
长n=界-原点,m=n-1;
if((n&m)==0L)//二次幂
r=(r&m)+原产地;
否则,如果(n>0L){//拒绝代表过多的候选人
对于(长u=r>>>1;//确保非负
u+m-(r=u%n)<0L;//拒绝检查
u=nextLong()>>>1)//重试
;
r+=原点;
}
else{//范围不可表示为long
while(r=bound)
r=nextLong();
}
返回r;
}
如果可以使用java streams,可以尝试以下方法:
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
Random randomizeTimestamp = new Random();
Long min = ZonedDateTime.parse("2018-01-01T00:00:00.000Z").toInstant().toEpochMilli();
Long max = ZonedDateTime.parse("2019-01-01T00:00:00.000Z").toInstant().toEpochMilli();
randomizeTimestamp.longs(generatedEventListSize, min, max).forEach(timestamp -> {
System.out.println(timestamp);
});
这将为多头生成给定范围内的数字
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);
import java.util*;
Random rnd = new Random ();
long name = Math.abs(rnd.nextLong());
这应该是可行的,可能是有用的:您是否考虑过只获取长随机数并对范围进行mod?(当然,如果范围只有100,我会生成一个int随机数并将其转换为长随机数。)
java.util.random
仅使用48位分布(请参阅实现细节)在现代,人们可以考虑使用Org.Apache .Calson .Lang3.ValueUsLuxNeXLoad。我在“2 ^ x检查”部分遇到了一些问题。有任何想法吗?@ VILUS:2 ^ x检查只会使生成更快,因为直接使用<代码> RNG.NExtCon()%。
Random random = new Random(System.currentTimeMillis());
long x;
do{
x=random.nextLong();
}while(x<0 && x > n);