" /> java - Pourquoi ce code utilise-t-il des chaînes aléatoires imprimez "hello world" | 2022 codeprofesseur

java - Pourquoi ce code utilise-t-il des chaînes aléatoires imprimez "hello world"

Mots clés : javastringrandomjava

meilleur 4 Réponses java - Pourquoi ce code utilise-t-il des chaînes aléatoires imprimez "hello world"

vote vote

97

Random r = new Random(-229985452) 
8 5 12 12 15 0 
23 15 18 12 4 0 
8  + 96 = 104 --> h 5  + 96 = 101 --> e 12 + 96 = 108 --> l 12 + 96 = 108 --> l 15 + 96 = 111 --> o  23 + 96 = 119 --> w 15 + 96 = 111 --> o 18 + 96 = 114 --> r 12 + 96 = 108 --> l 4  + 96 = 100 --> d 
vote vote

84

public static void main(String[] args) {     long time = System.currentTimeMillis();     generate("stack");     generate("over");     generate("flow");     generate("rulez");      System.out.println("Took " + (System.currentTimeMillis() - time) + " ms"); }  private static void generate(String goal) {     long[] seed = generateSeed(goal, Long.MIN_VALUE, Long.MAX_VALUE);     System.out.println(seed[0]);     System.out.println(randomString(seed[0], (char) seed[1])); }  public static long[] generateSeed(String goal, long start, long finish) {     char[] input = goal.toCharArray();     char[] pool = new char[input.length];     label:     for (long seed = start; seed < finish; seed++) {         Random random = new Random(seed);          for (int i = 0; i < input.length; i++)             pool[i] = (char) random.nextInt(27);          if (random.nextInt(27) == 0) {             int base = input[0] - pool[0];             for (int i = 1; i < input.length; i++) {                 if (input[i] - pool[i] != base)                     continue label;             }             return new long[]{seed, base};         }      }      throw new NoSuchElementException("Sorry :/"); }  public static String randomString(long i, char base) {     System.out.println("Using base: '" + base + "'");     Random ran = new Random(i);     StringBuilder sb = new StringBuilder();     for (int n = 0; ; n++) {         int k = ran.nextInt(27);         if (k == 0)             break;          sb.append((char) (base + k));     }      return sb.toString(); } 
-9223372036808280701 Using base: 'Z' stack -9223372036853943469 Using base: 'b' over -9223372036852834412 Using base: 'e' flow -9223372036838149518 Using base: 'd' rulez Took 7087 ms 
vote vote

75

#include <iostream> #include <random>   int main() {     std::mt19937 rng(631647094);     std::uniform_int_distribution<char> dist('a', 'z' + 1);       char alpha;     while ((alpha = dist(rng)) != 'z' + 1)     {         std::cout << alpha;     } } 
vote vote

64

import java.lang.*; import java.util.*; import java.io.*;  public class RandomWords {     public static void main (String[] args) {         Set<String> wordSet = new HashSet<String>();         String fileName = (args.length > 0 ? args[0] : "/usr/share/dict/words");         readWordMap(wordSet, fileName);         System.err.println(wordSet.size() + " words read.");         findRandomWords(wordSet);     }      private static void readWordMap (Set<String> wordSet, String fileName) {         try {             BufferedReader reader = new BufferedReader(new FileReader(fileName));             String line;             while ((line = reader.readLine()) != null) {                 line = line.trim().toLowerCase();                 if (isLowerAlpha(line)) wordSet.add(line);             }         }         catch (IOException e) {             System.err.println("Error reading from " + fileName + ": " + e);         }     }      private static boolean isLowerAlpha (String word) {         char[] c = word.toCharArray();         for (int i = 0; i < c.length; i++) {             if (c[i] < 'a' || c[i] > 'z') return false;         }         return true;     }      private static void findRandomWords (Set<String> wordSet) {         char[] c = new char[256];         Random r = new Random();         for (long seed0 = 0; seed0 >= 0; seed0++) {             for (int sign = -1; sign <= 1; sign += 2) {                 long seed = seed0 * sign;                 r.setSeed(seed);                 int i;                 for (i = 0; i < c.length; i++) {                     int n = r.nextInt(27);                     if (n == 0) break;                     c[i] = (char)((int)'a' + n - 1);                 }                 String s = new String(c, 0, i);                 if (wordSet.contains(s)) {                     System.out.println(s + ": " + seed);                     wordSet.remove(s);                 }             }         }     } } 
import java.lang.*; import java.util.*;  public class RandomWordsTest {     public static void main (String[] args) {         long[] a = {-73, -157512326, -112386651, 71425, -104434815,                     -128911, -88019, -7691161, 1115727};         for (int i = 0; i < a.length; i++) {             Random r = new Random(a[i]);             StringBuilder sb = new StringBuilder();             int n;             while ((n = r.nextInt(27)) > 0) sb.append((char)('`' + n));             System.out.println(sb);         }     } } 

Questions similaires