Bearbeiten von „Klassen:Javakurs“
Zur Navigation springen
Zur Suche springen
Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird bei Bearbeitungen öffentlich sichtbar. Melde dich an oder erstelle ein Benutzerkonto, damit Bearbeitungen deinem Benutzernamen zugeordnet werden.
Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version | Dein Text | ||
Zeile 32: | Zeile 32: | ||
this.seed = System.currentTimeMillis(); | this.seed = System.currentTimeMillis(); | ||
} | } | ||
Random(long seed){ | |||
this.seed = seed; | this.seed = seed; | ||
} | } | ||
long next() { | |||
this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989); | this.seed = Math.abs(this.seed * 0x20041991 + 0x11111989); | ||
return this.seed; | return this.seed; | ||
} | } | ||
long next(long min, long max){ | |||
long rc = next(); | long rc = next(); | ||
rc = min == max ? min : min + rc % (max - min); | rc = min == max ? min : min + rc % (max - min); | ||
return rc; | return rc; | ||
} | } | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Zeile 56: | Zeile 52: | ||
} | } | ||
class RealRandom extends Random { | class RealRandom extends Random { | ||
double next(double min, double max){ | |||
double rc = next(0, 1000000000) / 1000000000.0; | double rc = next(0, 1000000000) / 1000000000.0; | ||
rc = min == max ? min : min + rc * (max - min); | rc = min == max ? min : min + rc * (max - min); | ||
Zeile 62: | Zeile 58: | ||
} | } | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Random random = new RealRandom(); | |||
for (int ix = 0; ix < 10; ix++) | for (int ix = 0; ix < 10; ix++) | ||
System.out.print(String.format("%.3f ", random.next(-1.0, 1.0)); | System.out.print(String.format("%.3f ", random.next(-1.0, 1.0)); | ||
Zeile 68: | Zeile 64: | ||
} | } | ||
} | } | ||
</pre> | </pre> |