fork(2) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. // Генерация последовательности с помощью MersenneTwister по определенному seed
  6. class SequenceCreation {
  7.  
  8. // вход в нашу программу
  9. public static void main(String[] args) throws java.lang.Exception {
  10.  
  11. // Считываем указанное пользователем смещение (seed) из stdin.
  12. // Берется из текстовой презентации последовательности в игре.
  13. long seed = readStdIn();
  14.  
  15. // Создаем наш генератор с указанным смещением
  16. MersenneTwister генератор = new MersenneTwister(seed);
  17.  
  18. // Генерируем 135 пар заров в цикле
  19. for (int счётчик = 1; счётчик <= 135; счётчик++) {
  20.  
  21. // вызов "генератор.nextInt(6)" — выдает нам из последовательности
  22. // следующее целое число от 0 до 5, соответственно к результату прибавляем единицу
  23. int кубик1 = генератор.nextInt(6) + 1; // генерируем первый кубик
  24. int кубик2 = генератор.nextInt(6) + 1; // генерируем второй кубик
  25.  
  26. // выводим на экран разделяя оба кубика двоеточием и ставя запятую с пробелом после
  27. System.out.print(кубик1 + ":" + кубик2 + ", ");
  28. }
  29.  
  30. } // конец
  31.  
  32.  
  33.  
  34. public static long readStdIn() {
  35. try {
  36. byte[] buf = new byte[System.in.available()];
  37. System.in.read(buf);
  38. return Long.parseLong(new String(buf));
  39. } catch (Exception e) {
  40. System.out.println("Неверно задано смещение");
  41. System.exit(0);
  42. return 0;
  43. }
  44. }
  45.  
  46. // Все что ниже, это сам Вихрь Мерссена
  47. // взят отсюда _https://c...content-available-to-author-only...u.edu/~sean/research/
  48. // код _https://c...content-available-to-author-only...u.edu/~sean/research/mersenne/MersenneTwister.java
  49. // ec.util.MersenneTwister;
  50. /**
  51.  * <h3>MersenneTwister and MersenneTwisterFast</h3>
  52.  * <p><b>Version 20</b>, based on version MT199937(99/10/29)
  53.  * of the Mersenne Twister algorithm found at
  54.  * <a href="http://w...content-available-to-author-only...c.jp/matumoto/emt.html">
  55.  * The Mersenne Twister Home Page</a>, with the initialization
  56.  * improved using the new 2002/1/26 initialization algorithm
  57.  * By Sean Luke, October 2004.
  58.  *
  59.  * <p><b>MersenneTwister</b> is a drop-in subclass replacement
  60.  * for java.util.Random. It is properly synchronized and
  61.  * can be used in a multithreaded environment. On modern VMs such
  62.  * as HotSpot, it is approximately 1/3 slower than java.util.Random.
  63.  *
  64.  * <p><b>MersenneTwisterFast</b> is not a subclass of java.util.Random. It has
  65.  * the same public methods as Random does, however, and it is
  66.  * algorithmically identical to MersenneTwister. MersenneTwisterFast
  67.  * has hard-code inlined all of its methods directly, and made all of them
  68.  * final (well, the ones of consequence anyway). Further, these
  69.  * methods are <i>not</i> synchronized, so the same MersenneTwisterFast
  70.  * instance cannot be shared by multiple threads. But all this helps
  71.  * MersenneTwisterFast achieve well over twice the speed of MersenneTwister.
  72.  * java.util.Random is about 1/3 slower than MersenneTwisterFast.
  73.  *
  74.  * <h3>About the Mersenne Twister</h3>
  75.  * <p>This is a Java version of the C-program for MT19937: Integer version.
  76.  * The MT19937 algorithm was created by Makoto Matsumoto and Takuji Nishimura,
  77.  * who ask: "When you use this, send an email to: matumoto@math.keio.ac.jp
  78.  * with an appropriate reference to your work". Indicate that this
  79.  * is a translation of their algorithm into Java.
  80.  *
  81.  * <p><b>Reference. </b>
  82.  * Makato Matsumoto and Takuji Nishimura,
  83.  * "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
  84.  * Pseudo-Random Number Generator",
  85.  * <i>ACM Transactions on Modeling and. Computer Simulation,</i>
  86.  * Vol. 8, No. 1, January 1998, pp 3--30.
  87.  *
  88.  * <h3>About this Version</h3>
  89.  *
  90.  * <p><b>Changes since V19:</b> nextFloat(boolean, boolean) now returns float,
  91.  * not double.
  92.  *
  93.  * <p><b>Changes since V18:</b> Removed old final declarations, which used to
  94.  * potentially speed up the code, but no longer.
  95.  *
  96.  * <p><b>Changes since V17:</b> Removed vestigial references to &= 0xffffffff
  97.  * which stemmed from the original C code. The C code could not guarantee that
  98.  * ints were 32 bit, hence the masks. The vestigial references in the Java
  99.  * code were likely optimized out anyway.
  100.  *
  101.  * <p><b>Changes since V16:</b> Added nextDouble(includeZero, includeOne) and
  102.  * nextFloat(includeZero, includeOne) to allow for half-open, fully-closed, and
  103.  * fully-open intervals.
  104.  *
  105.  * <p><b>Changes Since V15:</b> Added serialVersionUID to quiet compiler warnings
  106.  * from Sun's overly verbose compilers as of JDK 1.5.
  107.  *
  108.  * <p><b>Changes Since V14:</b> made strictfp, with StrictMath.log and StrictMath.sqrt
  109.  * in nextGaussian instead of Math.log and Math.sqrt. This is largely just to be safe,
  110.  * as it presently makes no difference in the speed, correctness, or results of the
  111.  * algorithm.
  112.  *
  113.  * <p><b>Changes Since V13:</b> clone() method CloneNotSupportedException removed.
  114.  *
  115.  * <p><b>Changes Since V12:</b> clone() method added.
  116.  *
  117.  * <p><b>Changes Since V11:</b> stateEquals(...) method added. MersenneTwisterFast
  118.  * is equal to other MersenneTwisterFasts with identical state; likewise
  119.  * MersenneTwister is equal to other MersenneTwister with identical state.
  120.  * This isn't equals(...) because that requires a contract of immutability
  121.  * to compare by value.
  122.  *
  123.  * <p><b>Changes Since V10:</b> A documentation error suggested that
  124.  * setSeed(int[]) required an int[] array 624 long. In fact, the array
  125.  * can be any non-zero length. The new version also checks for this fact.
  126.  *
  127.  * <p><b>Changes Since V9:</b> readState(stream) and writeState(stream)
  128.  * provided.
  129.  *
  130.  * <p><b>Changes Since V8:</b> setSeed(int) was only using the first 28 bits
  131.  * of the seed; it should have been 32 bits. For small-number seeds the
  132.  * behavior is identical.
  133.  *
  134.  * <p><b>Changes Since V7:</b> A documentation error in MersenneTwisterFast
  135.  * (but not MersenneTwister) stated that nextDouble selects uniformly from
  136.  * the full-open interval [0,1]. It does not. nextDouble's contract is
  137.  * identical across MersenneTwisterFast, MersenneTwister, and java.util.Random,
  138.  * namely, selection in the half-open interval [0,1). That is, 1.0 should
  139.  * not be returned. A similar contract exists in nextFloat.
  140.  *
  141.  * <p><b>Changes Since V6:</b> License has changed from LGPL to BSD.
  142.  * New timing information to compare against
  143.  * java.util.Random. Recent versions of HotSpot have helped Random increase
  144.  * in speed to the point where it is faster than MersenneTwister but slower
  145.  * than MersenneTwisterFast (which should be the case, as it's a less complex
  146.  * algorithm but is synchronized).
  147.  *
  148.  * <p><b>Changes Since V5:</b> New empty constructor made to work the same
  149.  * as java.util.Random -- namely, it seeds based on the current time in
  150.  * milliseconds.
  151.  *
  152.  * <p><b>Changes Since V4:</b> New initialization algorithms. See
  153.  * (see <a href="http://w...content-available-to-author-only...c.jp/matumoto/MT2002/emt19937ar.html"</a>
  154.  * http://w...content-available-to-author-only...c.jp/matumoto/MT2002/emt19937ar.html</a>)
  155.  *
  156.  * <p>The MersenneTwister code is based on standard MT19937 C/C++
  157.  * code by Takuji Nishimura,
  158.  * with suggestions from Topher Cooper and Marc Rieffel, July 1997.
  159.  * The code was originally translated into Java by Michael Lecuyer,
  160.  * January 1999, and the original code is Copyright (c) 1999 by Michael Lecuyer.
  161.  *
  162.  * <h3>Java notes</h3>
  163.  *
  164.  * <p>This implementation implements the bug fixes made
  165.  * in Java 1.2's version of Random, which means it can be used with
  166.  * earlier versions of Java. See
  167.  * <a href="http://w...content-available-to-author-only...t.com/products/jdk/1.2/docs/api/java/util/Random.html">
  168.  * the JDK 1.2 java.util.Random documentation</a> for further documentation
  169.  * on the random-number generation contracts made. Additionally, there's
  170.  * an undocumented bug in the JDK java.util.Random.nextBytes() method,
  171.  * which this code fixes.
  172.  *
  173.  * <p> Just like java.util.Random, this
  174.  * generator accepts a long seed but doesn't use all of it. java.util.Random
  175.  * uses 48 bits. The Mersenne Twister instead uses 32 bits (int size).
  176.  * So it's best if your seed does not exceed the int range.
  177.  *
  178.  * <p>MersenneTwister can be used reliably
  179.  * on JDK version 1.1.5 or above. Earlier Java versions have serious bugs in
  180.  * java.util.Random; only MersenneTwisterFast (and not MersenneTwister nor
  181.  * java.util.Random) should be used with them.
  182.  *
  183.  * <h3>License</h3>
  184.  *
  185.  * Copyright (c) 2003 by Sean Luke. <br>
  186.  * Portions copyright (c) 1993 by Michael Lecuyer. <br>
  187.  * All rights reserved. <br>
  188.  *
  189.  * <p>Redistribution and use in source and binary forms, with or without
  190.  * modification, are permitted provided that the following conditions are met:
  191.  * <ul>
  192.  * <li> Redistributions of source code must retain the above copyright notice,
  193.  * this list of conditions and the following disclaimer.
  194.  * <li> Redistributions in binary form must reproduce the above copyright notice,
  195.  * this list of conditions and the following disclaimer in the documentation
  196.  * and/or other materials provided with the distribution.
  197.  * <li> Neither the name of the copyright owners, their employers, nor the
  198.  * names of its contributors may be used to endorse or promote products
  199.  * derived from this software without specific prior written permission.
  200.  * </ul>
  201.  * <p>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  202.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  203.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  204.  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR CONTRIBUTORS BE
  205.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  206.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  207.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  208.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  209.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  210.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  211.  * POSSIBILITY OF SUCH DAMAGE.
  212.  *
  213.  @version 20
  214. */
  215.  
  216. static class MersenneTwister extends java.util.Random implements Serializable, Cloneable
  217. {
  218. // Serialization
  219. private static final long serialVersionUID = -4035832775130174188L; // locked as of Version 15
  220.  
  221. // Period parameters
  222. private static final int N = 624;
  223. private static final int M = 397;
  224. private static final int MATRIX_A = 0x9908b0df; // private static final * constant vector a
  225. private static final int UPPER_MASK = 0x80000000; // most significant w-r bits
  226. private static final int LOWER_MASK = 0x7fffffff; // least significant r bits
  227.  
  228. // Tempering parameters
  229. private static final int TEMPERING_MASK_B = 0x9d2c5680;
  230. private static final int TEMPERING_MASK_C = 0xefc60000;
  231.  
  232. private int mt[]; // the array for the state vector
  233. private int mti; // mti==N+1 means mt[N] is not initialized
  234. private int mag01[];
  235.  
  236. // a good initial seed (of int size, though stored in a long)
  237. //private static final long GOOD_SEED = 4357;
  238.  
  239. /* implemented here because there's a bug in Random's implementation
  240.   of the Gaussian code (divide by zero, and log(0), ugh!), yet its
  241.   gaussian variables are private so we can't access them here. :-( */
  242.  
  243. private double __nextNextGaussian;
  244. private boolean __haveNextNextGaussian;
  245.  
  246. /* We're overriding all internal data, to my knowledge, so this should be okay */
  247. public Object clone()
  248. {
  249. try
  250. {
  251. MersenneTwister f = (MersenneTwister)(super.clone());
  252. f.mt = (int[])(mt.clone());
  253. f.mag01 = (int[])(mag01.clone());
  254. return f;
  255. }
  256. catch (CloneNotSupportedException e) { throw new InternalError(); } // should never happen
  257. }
  258.  
  259. public boolean stateEquals(Object o)
  260. {
  261. if (o==this) return true;
  262. if (o == null || !(o instanceof MersenneTwister))
  263. return false;
  264. MersenneTwister other = (MersenneTwister) o;
  265. if (mti != other.mti) return false;
  266. for(int x=0;x<mag01.length;x++)
  267. if (mag01[x] != other.mag01[x]) return false;
  268. for(int x=0;x<mt.length;x++)
  269. if (mt[x] != other.mt[x]) return false;
  270. return true;
  271. }
  272.  
  273. /** Reads the entire state of the MersenneTwister RNG from the stream */
  274. public void readState(DataInputStream stream) throws IOException
  275. {
  276. int len = mt.length;
  277. for(int x=0;x<len;x++) mt[x] = stream.readInt();
  278.  
  279. len = mag01.length;
  280. for(int x=0;x<len;x++) mag01[x] = stream.readInt();
  281.  
  282. mti = stream.readInt();
  283. __nextNextGaussian = stream.readDouble();
  284. __haveNextNextGaussian = stream.readBoolean();
  285. }
  286.  
  287. /** Writes the entire state of the MersenneTwister RNG to the stream */
  288. public void writeState(DataOutputStream stream) throws IOException
  289. {
  290. int len = mt.length;
  291. for(int x=0;x<len;x++) stream.writeInt(mt[x]);
  292.  
  293. len = mag01.length;
  294. for(int x=0;x<len;x++) stream.writeInt(mag01[x]);
  295.  
  296. stream.writeInt(mti);
  297. stream.writeDouble(__nextNextGaussian);
  298. stream.writeBoolean(__haveNextNextGaussian);
  299. }
  300.  
  301.  
  302. /**
  303.   * Constructor using the default seed.
  304.   */
  305. public MersenneTwister()
  306. {
  307. this(System.currentTimeMillis());
  308. }
  309.  
  310. /**
  311.   * Constructor using a given seed. Though you pass this seed in
  312.   * as a long, it's best to make sure it's actually an integer.
  313.   */
  314. public MersenneTwister(long seed)
  315. {
  316. super(seed); /* just in case */
  317. setSeed(seed);
  318. }
  319.  
  320. /**
  321.   * Constructor using an array of integers as seed.
  322.   * Your array must have a non-zero length. Only the first 624 integers
  323.   * in the array are used; if the array is shorter than this then
  324.   * integers are repeatedly used in a wrap-around fashion.
  325.   */
  326. public MersenneTwister(int[] array)
  327. {
  328. super(System.currentTimeMillis()); /* pick something at random just in case */
  329. setSeed(array);
  330. }
  331.  
  332. /**
  333.   * Initalize the pseudo random number generator. Don't
  334.   * pass in a long that's bigger than an int (Mersenne Twister
  335.   * only uses the first 32 bits for its seed).
  336.   */
  337.  
  338. synchronized public void setSeed(long seed)
  339. {
  340. // it's always good style to call super
  341. super.setSeed(seed);
  342.  
  343. // Due to a bug in java.util.Random clear up to 1.2, we're
  344. // doing our own Gaussian variable.
  345. __haveNextNextGaussian = false;
  346.  
  347. mt = new int[N];
  348.  
  349. mag01 = new int[2];
  350. mag01[0] = 0x0;
  351. mag01[1] = MATRIX_A;
  352.  
  353. mt[0]= (int)(seed & 0xffffffff);
  354. mt[0] = (int) seed;
  355. for (mti=1; mti<N; mti++)
  356. {
  357. mt[mti] =
  358. (1812433253 * (mt[mti-1] ^ (mt[mti-1] >>> 30)) + mti);
  359. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  360. /* In the previous versions, MSBs of the seed affect */
  361. /* only MSBs of the array mt[]. */
  362. /* 2002/01/09 modified by Makoto Matsumoto */
  363. // mt[mti] &= 0xffffffff;
  364. /* for >32 bit machines */
  365. }
  366. }
  367.  
  368.  
  369. /**
  370.   * Sets the seed of the MersenneTwister using an array of integers.
  371.   * Your array must have a non-zero length. Only the first 624 integers
  372.   * in the array are used; if the array is shorter than this then
  373.   * integers are repeatedly used in a wrap-around fashion.
  374.   */
  375.  
  376. synchronized public void setSeed(int[] array)
  377. {
  378. if (array.length == 0)
  379. throw new IllegalArgumentException("Array length must be greater than zero");
  380. int i, j, k;
  381. setSeed(19650218);
  382. i=1; j=0;
  383. k = (N>array.length ? N : array.length);
  384. for (; k!=0; k--)
  385. {
  386. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >>> 30)) * 1664525)) + array[j] + j; /* non linear */
  387. // mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */
  388. i++;
  389. j++;
  390. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  391. if (j>=array.length) j=0;
  392. }
  393. for (k=N-1; k!=0; k--)
  394. {
  395. mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >>> 30)) * 1566083941)) - i; /* non linear */
  396. // mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */
  397. i++;
  398. if (i>=N)
  399. {
  400. mt[0] = mt[N-1]; i=1;
  401. }
  402. }
  403. mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  404. }
  405.  
  406.  
  407.  
  408. /**
  409.   * Returns an integer with <i>bits</i> bits filled with a random number.
  410.   */
  411. synchronized protected int next(int bits)
  412. {
  413. int y;
  414.  
  415. if (mti >= N) // generate N words at one time
  416. {
  417. int kk;
  418. final int[] mt = this.mt; // locals are slightly faster
  419. final int[] mag01 = this.mag01; // locals are slightly faster
  420.  
  421. for (kk = 0; kk < N - M; kk++)
  422. {
  423. y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
  424. mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1];
  425. }
  426. for (; kk < N-1; kk++)
  427. {
  428. y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
  429. mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  430. }
  431. y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
  432. mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
  433.  
  434. mti = 0;
  435. }
  436.  
  437. y = mt[mti++];
  438. y ^= y >>> 11; // TEMPERING_SHIFT_U(y)
  439. y ^= (y << 7) & TEMPERING_MASK_B; // TEMPERING_SHIFT_S(y)
  440. y ^= (y << 15) & TEMPERING_MASK_C; // TEMPERING_SHIFT_T(y)
  441. y ^= (y >>> 18); // TEMPERING_SHIFT_L(y)
  442.  
  443. return y >>> (32 - bits); // hope that's right!
  444. }
  445.  
  446. /* If you've got a truly old version of Java, you can omit these
  447.   two next methods. */
  448.  
  449. private synchronized void writeObject(ObjectOutputStream out)
  450. throws IOException
  451. {
  452. // just so we're synchronized.
  453. out.defaultWriteObject();
  454. }
  455.  
  456. private synchronized void readObject (ObjectInputStream in)
  457. {
  458. // just so we're synchronized.
  459. in.defaultReadObject();
  460. }
  461.  
  462. /** This method is missing from jdk 1.0.x and below. JDK 1.1
  463.   includes this for us, but what the heck.*/
  464. public boolean nextBoolean() {return next(1) != 0;}
  465.  
  466. /** This generates a coin flip with a probability <tt>probability</tt>
  467.   of returning true, else returning false. <tt>probability</tt> must
  468.   be between 0.0 and 1.0, inclusive. Not as precise a random real
  469.   event as nextBoolean(double), but twice as fast. To explicitly
  470.   use this, remember you may need to cast to float first. */
  471.  
  472. public boolean nextBoolean (float probability)
  473. {
  474. if (probability < 0.0f || probability > 1.0f)
  475. throw new IllegalArgumentException ("probability must be between 0.0 and 1.0 inclusive.");
  476. if (probability==0.0f) return false; // fix half-open issues
  477. else if (probability==1.0f) return true; // fix half-open issues
  478. return nextFloat() < probability;
  479. }
  480.  
  481. /** This generates a coin flip with a probability <tt>probability</tt>
  482.   of returning true, else returning false. <tt>probability</tt> must
  483.   be between 0.0 and 1.0, inclusive. */
  484.  
  485. public boolean nextBoolean (double probability)
  486. {
  487. if (probability < 0.0 || probability > 1.0)
  488. throw new IllegalArgumentException ("probability must be between 0.0 and 1.0 inclusive.");
  489. if (probability==0.0) return false; // fix half-open issues
  490. else if (probability==1.0) return true; // fix half-open issues
  491. return nextDouble() < probability;
  492. }
  493.  
  494. /** This method is missing from JDK 1.1 and below. JDK 1.2
  495.   includes this for us, but what the heck. */
  496.  
  497. public int nextInt(int n)
  498. {
  499. if (n<=0)
  500. throw new IllegalArgumentException("n must be positive, got: " + n);
  501.  
  502. if ((n & -n) == n)
  503. return (int)((n * (long)next(31)) >> 31);
  504.  
  505. int bits, val;
  506. do
  507. {
  508. bits = next(31);
  509. val = bits % n;
  510. }
  511. while(bits - val + (n-1) < 0);
  512. return val;
  513. }
  514.  
  515. /** This method is for completness' sake.
  516.   Returns a long drawn uniformly from 0 to n-1. Suffice it to say,
  517.   n must be > 0, or an IllegalArgumentException is raised. */
  518.  
  519. public long nextLong(long n)
  520. {
  521. if (n<=0)
  522. throw new IllegalArgumentException("n must be positive, got: " + n);
  523.  
  524. long bits, val;
  525. do
  526. {
  527. bits = (nextLong() >>> 1);
  528. val = bits % n;
  529. }
  530. while(bits - val + (n-1) < 0);
  531. return val;
  532. }
  533.  
  534.  
  535. /** A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes
  536.   this for us, but what the heck. */
  537. public double nextDouble()
  538. {
  539. return (((long)next(26) << 27) + next(27))
  540. / (double)(1L << 53);
  541. }
  542.  
  543. /** Returns a double in the range from 0.0 to 1.0, possibly inclusive of 0.0 and 1.0 themselves. Thus:
  544.  
  545.   <p><table border=0>
  546.   <th><td>Expression<td>Interval
  547.   <tr><td>nextDouble(false, false)<td>(0.0, 1.0)
  548.   <tr><td>nextDouble(true, false)<td>[0.0, 1.0)
  549.   <tr><td>nextDouble(false, true)<td>(0.0, 1.0]
  550.   <tr><td>nextDouble(true, true)<td>[0.0, 1.0]
  551.   </table>
  552.  
  553.   <p>This version preserves all possible random values in the double range.
  554.   */
  555. public double nextDouble(boolean includeZero, boolean includeOne)
  556. {
  557. double d = 0.0;
  558. do
  559. {
  560. d = nextDouble(); // grab a value, initially from half-open [0.0, 1.0)
  561. if (includeOne && nextBoolean()) d += 1.0; // if includeOne, with 1/2 probability, push to [1.0, 2.0)
  562. }
  563. while ( (d > 1.0) || // everything above 1.0 is always invalid
  564. (!includeZero && d == 0.0)); // if we're not including zero, 0.0 is invalid
  565. return d;
  566. }
  567.  
  568. /** A bug fix for versions of JDK 1.1 and below. JDK 1.2 fixes
  569.   this for us, but what the heck. */
  570.  
  571. public float nextFloat()
  572. {
  573. return next(24) / ((float)(1 << 24));
  574. }
  575.  
  576.  
  577.  
  578. /** Returns a float in the range from 0.0f to 1.0f, possibly inclusive of 0.0f and 1.0f themselves. Thus:
  579.  
  580.   <p><table border=0>
  581.   <th><td>Expression<td>Interval
  582.   <tr><td>nextFloat(false, false)<td>(0.0f, 1.0f)
  583.   <tr><td>nextFloat(true, false)<td>[0.0f, 1.0f)
  584.   <tr><td>nextFloat(false, true)<td>(0.0f, 1.0f]
  585.   <tr><td>nextFloat(true, true)<td>[0.0f, 1.0f]
  586.   </table>
  587.  
  588.   <p>This version preserves all possible random values in the float range.
  589.   */
  590. public float nextFloat(boolean includeZero, boolean includeOne)
  591. {
  592. float d = 0.0f;
  593. do
  594. {
  595. d = nextFloat(); // grab a value, initially from half-open [0.0f, 1.0f)
  596. if (includeOne && nextBoolean()) d += 1.0f; // if includeOne, with 1/2 probability, push to [1.0f, 2.0f)
  597. }
  598. while ( (d > 1.0f) || // everything above 1.0f is always invalid
  599. (!includeZero && d == 0.0f)); // if we're not including zero, 0.0f is invalid
  600. return d;
  601. }
  602.  
  603.  
  604.  
  605. /** A bug fix for all versions of the JDK. The JDK appears to
  606.   use all four bytes in an integer as independent byte values!
  607.   Totally wrong. I've submitted a bug report. */
  608.  
  609. public void nextBytes(byte[] bytes)
  610. {
  611. for (int x=0;x<bytes.length;x++) bytes[x] = (byte)next(8);
  612. }
  613.  
  614. /** For completeness' sake, though it's not in java.util.Random. */
  615.  
  616. public char nextChar()
  617. {
  618. // chars are 16-bit UniCode values
  619. return (char)(next(16));
  620. }
  621.  
  622. /** For completeness' sake, though it's not in java.util.Random. */
  623.  
  624. public short nextShort()
  625. {
  626. return (short)(next(16));
  627. }
  628.  
  629. /** For completeness' sake, though it's not in java.util.Random. */
  630.  
  631. public byte nextByte()
  632. {
  633. return (byte)(next(8));
  634. }
  635.  
  636.  
  637. /** A bug fix for all JDK code including 1.2. nextGaussian can theoretically
  638.   ask for the log of 0 and divide it by 0! See Java bug
  639.   <a href="http://d...content-available-to-author-only...n.com/developer/bugParade/bugs/4254501.html">
  640.   http://d...content-available-to-author-only...n.com/developer/bugParade/bugs/4254501.html</a>
  641.   */
  642.  
  643. synchronized public double nextGaussian()
  644. {
  645. if (__haveNextNextGaussian)
  646. {
  647. __haveNextNextGaussian = false;
  648. return __nextNextGaussian;
  649. }
  650. else
  651. {
  652. double v1, v2, s;
  653. do
  654. {
  655. v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0
  656. v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0
  657. s = v1 * v1 + v2 * v2;
  658. } while (s >= 1 || s==0 );
  659. double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s);
  660. __nextNextGaussian = v2 * multiplier;
  661. __haveNextNextGaussian = true;
  662. return v1 * multiplier;
  663. }
  664. }
  665. }
  666.  
  667. }
Success #stdin #stdout 0.1s 321536KB
stdin
Удалите эту строку и вставьте сюда Mersenne Twister seed взятый из последовательности
stdout
Неверно задано смещение