Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Yet another PRNG with some bits & bobs.  (Read 1819 times)
0 Members and 1 Guest are viewing this topic.
Offline Roquen
« Posted 2012-10-26 13:14:24 »

Here's a small set of some useful stuff for random number generation.  Included is a base 32-bit XorShift (or XorWOW) generator.  Tossed together on a whim in relation to DrHalfway's post of a 64-bit XorShift found here.  It's a miracle if there aren't any bugs.  


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
import java.util.concurrent.atomic.AtomicLong;


// Implementation notes:
//  Without Weyl generator: SmallCrush failures [13,17,5]:
//    1  BirthdaySpacings    eps  
//    2  Collision           1 - eps1
//    6  MaxOft              6.7e-16
//    8  MatrixRank          eps  
//   10  RandomWalk1 H       5.7e-7
// Adding in the counter will make it pass all
// SmallCrush tests.  You don't care. I promise.

/***
 * Basic XorShift (32-bit) generator.  This specific generator
 * predates generalized XorShifts and was previously called SHR3.
 * (This is one component of the once popular KISS generator)
 * The default configuration has a period of 2<sup>32</sup>-1,
 * although paranoid programmer may have extended it by adding
 * a Weyl generator (making it an XorWOW) with a period of
 * 2<sup>64</sup>-2<sup>32</sup> ~= 2<sup>64</sup>
 * <p>
 * All ranges are shown in American notation: square bracket
 * is inclusive and paren is exclusive.
 * <p>
 * References:
 * <list><li>"Xorshift RNGs", George Marsaglia, 2003.</li>
 * <li>"Some long-period random number generators
 * using shifts and xors", Richard P. Brent, 2006.</li></list>
 */

public final class XorShift32
{
  /** state data of xorshift */
  private int data;

  //*********************** For the paranoid section (start)
 
  /** If this is true, then we're XorWOW and paranoid ;) */
  public static final boolean PARANOID = true;
 
  // just in case we happen to have two threads on different
  // processors make two non-seeded generators within the
  // same nanosecond (currently CPU tick) and that somehow
  // really matters.  Or we're in the future of this writing
  // and computers are really fast and we succeed in creating
  // two non-seeded generators in a row within the same
  // nanosecond and it matters.  You never know. (Seriously,
  // why not...doesn't cost you anything)
  private static AtomicLong mix = new AtomicLong();

  // counter (weyl) stuff...only for the paranoid.
  /** state data of Weyl */
  private int weyl;
 
  /**
   * Weyl increment.  This value must be odd, but some
   * choices are much better than others.
   * The choice here is from Richard Brent (see paper)
   * <p>
   * 2<sup>31</sup>(3-sqrt(5)) = 0x61c88647
   */

  private static final int WEYL = 0x61c88647;

  //*********************** For the paranoid section (end)

  public XorShift32()
  {
    setSeed((mix.getAndDecrement() ^ System.nanoTime()));
  }

  public XorShift32(int seed)
  {
    setSeed(seed);
  }

  public XorShift32(long seed)
  {
    setSeed(seed);
  }

  /**
   * Sets the current set of the generator. Some
   * seeds set the generator into an identical
   * state.
   */

  public final void setSeed(long seed)
  {
    int lo = (int)(seed);

    // xorshift state must never be zero.
    if (lo==0) lo = 0x92d68ca2;
   
    data = lo;

    if (PARANOID) {
      int hi = (int)(seed >>> 32);
      weyl = hi|1; // must be odd
    }
  }

  /**
   * Returns the current state data of the generator.
   */

  public final long getSeed()
  {
    long r = data;

    if (PARANOID)
      r |= weyl << 32;
   
    return r;
  }
 
  /** Returns a uniform 32-bit integer. */
  public final int nextInt()
  {
    data ^= (data <<  13);
    data ^= (data >>> 17);
    data ^= (data <<   5);
   
    if (PARANOID) {
      weyl += WEYL;
      return data + weyl;
    }
    else
      return data;
  }
 
  //***************************************
  //** Derived stuff all below this point
  //** some assumptions based on 32-bit
  //** base generator.
  //***************************************

  /** Maps a 32-bit integer to [0,1) single. */
  public static final float mapToZO(int n)
  {
    // max value is 1-ulp(1)
    return (n >>> 8) * 0x1p-24f;
  }

  /** Maps a 64-bit integer to [0,1) double. */
  public static final double mapToZO(long n)
  {
    // max value is 1-ulp(1)
    return (n >>> 12) * 0x1p-52;
  }

  /** Maps a 32-bit integer to [-1,1] single. */
  public static final float mapToPMO(int n)
  {
    return ((n >> 8)+0.5f) * 0x1.000002p-23f;
  }

  /** Maps a 64-bit integer to [-1,1] double. */
  public static final double mapToPMO(long n)
  {
    return ((n >> 12)+0.5)*0x1.0000000000001p-51;
  }


  /** Returns a uniform boolean */
  public final boolean nextBoolean()
  {
    return nextInt() < 0;
  }

  /**
   * Returns a uniform integer on [0, n).
   * <p>
   * Range 'n' legal on [0, 0x8000].
    */

  public final int nextInt(int n)
  {
    // not quite uniform..you shouldn't care.
    // If you're really paranoid change to a
    // rejection method.
    return ((nextInt()>>>15) * n) >>> 17;
  }

  /** Returns a uniform integer on [0, n). */
  public final int nextIntBig(int n)
  {
    // See notes in nextInt. This is
    // (on average) a better choice for
    // 64-bit VMs.
    long r = nextInt() >>> 1;

    // sign doesn't matter here
    r = (r * n) >> 31;

    return (int)r;
  }

  /** Returns a uniform 64-bit integer. */
  public final long nextLong()
  {
    long r = nextInt();
    return (r<<32) | nextInt();
  }

  /**
   * Returns a uniform float on [0, 1) = [0, 1-ulp(1)]
   * <p>
   * @see #mapToZO(int)
   */

  public final float nextFloat()
  {
    return mapToZO(nextInt());
  }

  /**
   * Returns a uniform single on [min,max)
   */

  public final float nextFloat(float min, float max)
  {
    // NOTE: Not the soundest method..you shouldn't care.
    return min+nextFloat()*(max-min);
  }

  /**
   * Returns a uniform double on [0, 1) = [0, 1-ulp(1)]
   * <p>
   * @see #mapToZO(long)
   */

  public final double nextDouble()
  {
    return mapToZO(nextLong());
  }

  /**
   * Returns a uniform double on [min,max)
   */

  public final double nextDouble(double min, double max)
  {
    // NOTE: Not the soundest method..you shouldn't care.
    return min+nextDouble()*(max-min);
  }

 
  /**
   * Returns a 'n' bit random number, [0, 2<sup>n</sup>)
   */

  public final int nextBits(int n)
  {
    return nextInt() >>> (32-n);
  }

  /**
   * Returns a 'n' bit random number, [2<sup>n/2</sup>, 2<sup>n/2</sup>)
   */

  public final int nextSignedBits(int n)
  {
    return nextInt() >> (32-n);
  }
 }
Offline tyeeeee1
« Reply #1 - Posted 2012-12-12 01:40:50 »

This may be a bit stupid for my first post (sorry) but can't you just use Math.random to get a random number?
Offline BoBear2681

JGO Coder


Medals: 19



« Reply #2 - Posted 2012-12-12 05:01:40 »

Normal folks like us use Math.random() or Random.nextXXX().  Smart people like Roquen like to dabble with rolling their own, to make something faster, or with a more uniform distribution, or some other desirable property.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2012-12-12 06:09:40 »

And really, really smart people never worry about this stuff..except perhaps a couple of rules of thumb.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-12-12 18:24:25 »

This may be a bit stupid for my first post (sorry) but can't you just use Math.random to get a random number?

Oh... you are just getting to know Roquen Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #5 - Posted 2012-12-12 20:58:28 »

This may be a bit stupid for my first post (sorry) but can't you just use Math.random to get a random number?

Oh... you are just getting to know Roquen Smiley
Since you're Konata-chan, you can say whatever you want without sounding stupid. Cute is justice.

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2012-12-12 21:51:13 »

This may be a bit stupid for my first post (sorry) but can't you just use Math.random to get a random number?

Oh... you are just getting to know Roquen Smiley
Since you're Konata-chan, you can say whatever you want without sounding stupid. Cute is justice.
I was just soooo confused, because I tought you were talking about me. Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #7 - Posted 2012-12-12 22:15:38 »

Wait, what?

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (43 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (60 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!