Pourquoi le code générant des chaînes aléatoires imprime toujours "hello world" ?
Bonjour à tous ! Je suis un développeur Java débutant et j'ai rencontré un comportement très étrange. J'ai écrit un programme simple qui génère une chaîne aléatoire et vérifie si elle est égale à "hello world". Si c'est le cas, il imprime un message. Mais le programme imprime toujours "hello world", alors que je m'attendais à ce que ce soit un cas rare.
Voici le 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;
}
}
}
static String generateRandomString(Random random, int length) {
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
char c = (char) (ran