Why does code with random string generation always print "hello world"?
Hi everyone! I'm a beginner Java developer and I've encountered very strange behavior. I wrote a simple program that generates a random string and checks if it equals "hello world". If it does, it prints a message. But the program always prints "hello world", although I expected this to be a rare case.
Here's the code:
```java
import java.util.Random;
public class StrangeHelloWorld {
public static void main(String[] args) {
Random random = new Random();
String target = "hello world";
while (true) {
String randomString = generateRandomString(random, target.length());
if (randomString.equals(target)) {
System.out.println("hello world");
break;
}
}
}
```
No answers yet.
Log in to leave an answer