在java中,(int)(Math.random())和Math.random()之间有什么区别?

时间:2022-01-11 03:40:23

What exactly does the (int) before the (Math.random()) do to it? I know it has to do with getting an integer out of the Math.random() but I can't really understand how it does that. I can't understand how this (int) works. I mean, for example, how do I use it in other ways?

(Math.random())之前的(int)到底是做什么的?我知道它与从Math.random()中获取一个整数有关,但我无法理解它是如何做到的。我无法理解这个(int)是如何工作的。我的意思是,例如,我如何以其他方式使用它?

4 个解决方案

#1


4  

The (int) casts the double result of Math.random() to an integer. So (int) Math.random() always is zero, because random() returns a number greater than or equal to 0.0 and less than 1.0

(int)将Math.random()的double结果转换为整数。所以(int)Math.random()总是为零,因为random()返回一个大于或等于0.0且小于1.0的数字

int i = (int)Math.random();
double d =  Math.random();

System.out.println(i);
System.out.println(d);

If you need integer randoms, you need first to multyply the result of random

如果你需要整数randoms,你首先需要多次随机的结果

int factor = 100;
i = (int)(Math.random() * factor);
System.out.println(i);

#2


0  

This syntax is a cast. It's a way of directly converting between types; the most usual use case is to convert a value of a particular type to a specific subtype when you know that the particular instance is really that subtype, e.g.:

此语法是强制转换。这是一种直接在不同类型之间转换的方式;最常见的用例是当您知道特定实例确实是该子类型时,将特定类型的值转换为特定子类型,例如:

abstract class Parent {...}
class Child extends Parent {...}

Parent myObject = ... //we know it's *really* a Child
Child myObject2 = (Child) myObject;

Note that this is an unsafe operation: if myObject is actually a different subclass of Parent, it will fail at runtime (by throwing ClassCastException). For this reason casts should usually be avoided. While the compiler may refuse to compile some provably wrong casts, most of the time you can override the type system, casting anything to anything, and failures will only happen at runtime.

请注意,这是一个不安全的操作:如果myObject实际上是Parent的不同子类,它将在运行时失败(通过抛出ClassCastException)。因此,通常应避免使用演员阵容。虽然编译器可能拒绝编译一些可证明错误的强制类型转换,但大多数情况下,您可以覆盖类型系统,将任何内容转换为任何内容,并且失败只会在运行时发生。

Casts between primitive types are special-cased, and safer (although I would still advise avoiding them where possible, since they look like unsafe code; Double.valueOf(Math.random()).intValue() obtains the same result in a visibly safe way, and the performance cost, though real, is irrelevant to most programs). See section 5.1.3 of the JLS.

原始类型之间的转换是特殊的,更安全(尽管我仍然建议尽可能避免使用它们,因为它们看起来像不安全的代码; Double.valueOf(Math.random())。intValue()在可见的情况下获得相同的结果安全的方式,性能成本,虽然是真实的,与大多数程序无关)。请参阅JLS的5.1.3节。

#3


0  

The signature of Math.random() is public static double random() meaning it returns a double value.

Math.random()的签名是public static double random(),意味着它返回一个double值。

If for some reason you want an integer then you should cast this value to an int. To make it brief it cuts any digit after the point part in your double.

如果由于某种原因你想要一个整数,那么你应该将这个值转换为int。为了使它简洁,它会在你的双点中的点部分之后剪切任何数字。

e.g. 1.2 would become 1
2.45 would become 2 etc

例如1.2将变为1 2.45将变为2等

P.S. you should really read about casting to get the general idea how and when to use it.

附:你应该真正阅读关于转换的内容,以便大致了解如何以及何时使用它。

Edit:

like @FlorentBayle commented casting the number generated by double() will always return 0 since it's range is [0,1) 0 inclusive, 1 exclusive that is. So, you must multiply it with an int which is equal to the max number of your desired range:

像@FlorentBayle评论的那样,由double()生成的数字将始终返回0,因为它的范围是[0,1)0(包括1),1是独占的。所以,你必须将它乘以一个int,它等于你想要的范围的最大数量:

e.g. to get int into range [0,5) you should use:

例如要获得int到范围[0,5]你应该使用:

int i = (int) (Math.random()*5);

#4


0  

Math.Random() is just a wrapper around java.util.Random();

Math.Random()只是java.util.Random()的一个包装器;

So instead, make a

所以相反,做一个

Random rnd = new java.util.Random(); //costly operation so use only once
rnd.nextInt();                       //keep using nextwhatever for further operations  

#1


4  

The (int) casts the double result of Math.random() to an integer. So (int) Math.random() always is zero, because random() returns a number greater than or equal to 0.0 and less than 1.0

(int)将Math.random()的double结果转换为整数。所以(int)Math.random()总是为零,因为random()返回一个大于或等于0.0且小于1.0的数字

int i = (int)Math.random();
double d =  Math.random();

System.out.println(i);
System.out.println(d);

If you need integer randoms, you need first to multyply the result of random

如果你需要整数randoms,你首先需要多次随机的结果

int factor = 100;
i = (int)(Math.random() * factor);
System.out.println(i);

#2


0  

This syntax is a cast. It's a way of directly converting between types; the most usual use case is to convert a value of a particular type to a specific subtype when you know that the particular instance is really that subtype, e.g.:

此语法是强制转换。这是一种直接在不同类型之间转换的方式;最常见的用例是当您知道特定实例确实是该子类型时,将特定类型的值转换为特定子类型,例如:

abstract class Parent {...}
class Child extends Parent {...}

Parent myObject = ... //we know it's *really* a Child
Child myObject2 = (Child) myObject;

Note that this is an unsafe operation: if myObject is actually a different subclass of Parent, it will fail at runtime (by throwing ClassCastException). For this reason casts should usually be avoided. While the compiler may refuse to compile some provably wrong casts, most of the time you can override the type system, casting anything to anything, and failures will only happen at runtime.

请注意,这是一个不安全的操作:如果myObject实际上是Parent的不同子类,它将在运行时失败(通过抛出ClassCastException)。因此,通常应避免使用演员阵容。虽然编译器可能拒绝编译一些可证明错误的强制类型转换,但大多数情况下,您可以覆盖类型系统,将任何内容转换为任何内容,并且失败只会在运行时发生。

Casts between primitive types are special-cased, and safer (although I would still advise avoiding them where possible, since they look like unsafe code; Double.valueOf(Math.random()).intValue() obtains the same result in a visibly safe way, and the performance cost, though real, is irrelevant to most programs). See section 5.1.3 of the JLS.

原始类型之间的转换是特殊的,更安全(尽管我仍然建议尽可能避免使用它们,因为它们看起来像不安全的代码; Double.valueOf(Math.random())。intValue()在可见的情况下获得相同的结果安全的方式,性能成本,虽然是真实的,与大多数程序无关)。请参阅JLS的5.1.3节。

#3


0  

The signature of Math.random() is public static double random() meaning it returns a double value.

Math.random()的签名是public static double random(),意味着它返回一个double值。

If for some reason you want an integer then you should cast this value to an int. To make it brief it cuts any digit after the point part in your double.

如果由于某种原因你想要一个整数,那么你应该将这个值转换为int。为了使它简洁,它会在你的双点中的点部分之后剪切任何数字。

e.g. 1.2 would become 1
2.45 would become 2 etc

例如1.2将变为1 2.45将变为2等

P.S. you should really read about casting to get the general idea how and when to use it.

附:你应该真正阅读关于转换的内容,以便大致了解如何以及何时使用它。

Edit:

like @FlorentBayle commented casting the number generated by double() will always return 0 since it's range is [0,1) 0 inclusive, 1 exclusive that is. So, you must multiply it with an int which is equal to the max number of your desired range:

像@FlorentBayle评论的那样,由double()生成的数字将始终返回0,因为它的范围是[0,1)0(包括1),1是独占的。所以,你必须将它乘以一个int,它等于你想要的范围的最大数量:

e.g. to get int into range [0,5) you should use:

例如要获得int到范围[0,5]你应该使用:

int i = (int) (Math.random()*5);

#4


0  

Math.Random() is just a wrapper around java.util.Random();

Math.Random()只是java.util.Random()的一个包装器;

So instead, make a

所以相反,做一个

Random rnd = new java.util.Random(); //costly operation so use only once
rnd.nextInt();                       //keep using nextwhatever for further operations