简体   繁体   中英

Java Math.random

I been searching for the logic of Math.random through book, however I can't get to understand

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

What does 27 means? 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

27 is one greater than the number of letters in the English alphabet (26).

Math.random() * 27 generates a number between 0 (inclusive) and 27 (exclusive).

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 '[' .

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.

Form the java doc

public static double random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. 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. 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()

Math.random() generates a value between 0.0 to 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..

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.

The output will lie from AZ and [ ie characters having ASCII values from 65-91

After replacing 27 with 100 you get special characters because the range of ASCII value has now increased to 65-165

you may see the ASCII codes for symbols you are getting here.

Math.random() returns a result between 0.0 (inclusive) and 1.0 (exclusive).

Math.random() * 27 returns a result between 0.0 and 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).

As other answers state the constant 27 represent the range of random integers.

char is encoded number that specification was standardized as ASCII .

This mean when you type in your code 'A', program can interpret this as number 65.

In ASCII tables, the letters are consequently described. This mean

char c = 'A' + 1;

The c will have value B.

The random function will return value from 0 to 0.9999.

This result multiplied by 27 will result with values from 0 to 26.997...

In final result when add to 65 (witch is A) value from previous result.

This mean then pool of numbers we generate is from 65 to 91.

At the end we convert this number into 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 '['.

It can be that the reason why someone choose the 27 constant is because he wanted to generate random capitalized letter. The reason why is 27 not 26, it can be an calculation error made by developer.

Math.random() returns a random double value between 0 (inclusive) and 1 (exclusive). This means that, when multiplied by n , the result, cast to an integer value (eg char ), is from 0 to n . Therefore, your result will be between A and [ ( AZ and [ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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