+ 1

Why the code below produces in JStudio a number with 3 digits? It really does.

Random Gen = new Random(); int numerKarty = 0; for (int i=0; i<=4; i++){ int nextNumber = Gen.nextInt(8)+1; numerKarty+=nextNumber*(10^i); } System.out.println(numerKarty);

20th Apr 2021, 8:00 PM
Stiffo
Stiffo - avatar
1 Answer
+ 1
Because your math will produce a final number anywhere in the range of 52-416 That is your random number in the range of 1-8 * 10^i where i is 0-4 and each successive result of the xor will be 10^0 = 10 10^1 = 11 10^2 = 8 10^3 = 9 10^4 = 14 With all random numbers being the lowest possible value of 1 you get; 10+11+8+9+14 = 52 With all random values being the highest possible value of 8 you get; 80+88+64+72+112 = 416 Or 52 * 8 = 416 Giving you a possible range of any number from 52 to 416
20th Apr 2021, 11:52 PM
ChaoticDawg
ChaoticDawg - avatar