简体   繁体   English

Java Math.random

[英]Java Math.random

I been searching for the logic of Math.random through book, however I can't get to understand 我一直在通过书本搜索Math.random的逻辑,但是我无法理解

((char)('A' + Math.random() * 27))

What does 27 means? 27是什么意思? I tried to replace with 100, the results is it generates some special characters like !@#$%^&*(( I'd appreciate if you could give me more info about this. TQ 我尝试将其替换为100,结果是它会生成一些特殊字符,如!@#$%^&*((如果您能给我更多有关此信息,我将不胜感激。TQ

27 is one greater than the number of letters in the English alphabet (26). 27比英文字母(26)中的字母数大1。

Math.random() * 27 generates a number between 0 (inclusive) and 27 (exclusive). Math.random() * 27生成一个介于0(含)和27(不含)之间的数字。

Assuming the intention of the code is to generate an uppercase letter, it has an off-by-one error: it can produce ((char)('A' + 26)) , which is '[' . 假设代码的目的是生成一个大写字母,那么它会有一个错误的错误:它可以产生((char)('A' + 26)) ,即'['

Math random returns a random value between 0 and 1. 27 is a multiplier that make it possibil to get a random value between 0 and 27. 数学随机数返回0到1之间的随机值。27是一个乘法器,使它有可能获得0到27之间的随机值。

Form the java doc 形成Java文档

public static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. public static double random()返回一个带有正号的大于等于0.0且小于1.0的double值。 Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range. 从该范围(近似)均匀分布伪随机地选择返回值。 When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression 首次调用此方法时,它会创建一个新的伪随机数生成器,就像通过表达式

new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. new java.util.Random()此新的伪随机数生成器此后用于对该方法的所有调用,并且在其他任何地方均不使用。 This method is properly synchronized to allow correct use by more than one thread. 此方法已正确同步,以允许多个线程正确使用。 However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator. 但是,如果许多线程需要以很高的速率生成伪随机数,则可以减少每个线程拥有自己的伪随机数生成器的竞争。 Returns:a pseudorandom double greater than or equal to 0.0 and less than 1.0.See Also:Random.nextDouble() 返回:大于或等于0.0且小于1.0的伪随机双精度值。另请参见:Random.nextDouble()

Math.random() generates a value between 0.0 to 0.99 Math.random()生成介于0.0到0.99之间的值

Multiplying it with a no. 将其乘以否。 increases it's range 增加范围

For ex: 例如:

System.out.println(Math.random()*100);

multiplying it with 100 will give a value between 0.000.. to 99.999.. 将其乘以100将得到介于0.000 ..至99.999 ..之间的值。

Similarly: 类似地:

System.out.println((char)('A'+ Math.random()*27));

Will first generate a no. 首先会产生一个编号。 between 0.0 to 26.99.. then add it to the ASCII value to 'A' and then typecast it to char displaying a char value which has the ASCII value equal to the result. 介于0.0 to 26.99..之间0.0 to 26.99..然后将其添加到ASCII值“ A”中,然后将其类型转换为char显示一个ASCII值等于结果的char值。

The output will lie from AZ and [ ie characters having ASCII values from 65-91 输出将位于AZ[即具有65-91的ASCII值的字符

After replacing 27 with 100 you get special characters because the range of ASCII value has now increased to 65-165 100代替27 ,您会得到特殊字符,因为ASCII值的范围现在已增加到65-165

you may see the ASCII codes for symbols you are getting here. 您可能会看到到达此处的符号的ASCII码

Math.random() returns a result between 0.0 (inclusive) and 1.0 (exclusive). Math.random()返回介于0.0(含)和1.0( Math.random()之间的结果。

Math.random() * 27 returns a result between 0.0 and 27.0. Math.random() * 27返回0.0到27.0之间的结果。

((char)('A' + Math.random() * 27)) returns a result between ((char)('A' + 0)) and ((char)('A' + 26)) - in other words, a char between 'A' and 'Z' (0 to 25) or '[' (26). ((char)('A' + Math.random() * 27))返回((char)('A' + 0))((char)('A' + 26)) -在其他单词,介于“ A”和“ Z”(0到25)或“ [”(26)之间的字符。

As other answers state the constant 27 represent the range of random integers. 如其他答案所述,常数27代表随机整数的范围。

char is encoded number that specification was standardized as ASCII . char是规范标准化为ASCII的编码数字。

This mean when you type in your code 'A', program can interpret this as number 65. 这意味着当您输入代码“ A”时,程序可以将其解释为数字65。

In ASCII tables, the letters are consequently described. 因此,在ASCII表中描述了字母。 This mean 这个意思

char c = 'A' + 1;

The c will have value B. c值为B。

The random function will return value from 0 to 0.9999. 随机函数将返回0到0.9999之间的值。

This result multiplied by 27 will result with values from 0 to 26.997... 该结果乘以27将得到0到26.997之间的值...

In final result when add to 65 (witch is A) value from previous result. 在最终结果中,将先前结果加到65(女巫为A)值。

This mean then pool of numbers we generate is from 65 to 91. 这意味着我们生成的数字池从65到91。

At the end we convert this number into char. 最后,我们将此数字转换为char。

When we go to ASCII table as check what numbers are between 65 and 92 we will notice that those numbers are letters from 'A' to 'Z' and '['. 当我们进入ASCII表检查65和92之间的数字时,我们会注意到这些数字是从'A'到'Z'和'['的字母。

It can be that the reason why someone choose the 27 constant is because he wanted to generate random capitalized letter. 可能有人选择27常数的原因是因为他想生成随机的大写字母。 The reason why is 27 not 26, it can be an calculation error made by developer. 为什么不是27而不是26,可能是开发人员的计算错误。

Math.random() returns a random double value between 0 (inclusive) and 1 (exclusive). Math.random()返回介于0 (含)和1 (不含)之间的随机double值。 This means that, when multiplied by n , the result, cast to an integer value (eg char ), is from 0 to n . 这意味着,当乘以n时 ,转换为整数值(例如char )的结果是从0n Therefore, your result will be between A and [ ( AZ and [ ). 因此,您的结果将在A[AZ[ )之间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM