Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Dice roller  (Read 2147 times)
0 Members and 1 Guest are viewing this topic.
Offline Roquen
« Posted 2012-07-28 13:04:13 »

Everyone's first program:

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  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344  
345  
346  
347  
348  
349  
350  
351  
352  
353  
354  
355  
356  
357  
import java.util.concurrent.atomic.AtomicLong;


/**
 * <b>Coding comments</b>
 * <p>
 * Threading. All methods are reentrant.  Technically a given instance is
 * not thread safe. From a practical standpoint, calling from multiple
 * threads will simply make the instance non-deterministic. Heavy access
 * from multiple threads is undesirable as each call will cause cache
 * line invalidations in all other actively accessing threads (assuming
 * on different cores). Specifically it's possible for the sequence to
 * appear to rewind some number of positions.
 */


public class Dice
{
  private static AtomicLong mix = new AtomicLong();
 
  private int seed;
 
  // these are proven reasonable number from L'Ecuyer
 private static final int RNG_M = 0xac549d55;
  private static final int RNG_A = 54321;
 
  public Dice(int seed)
  {
    this.seed = seed;
  }
 
  public Dice()
  {
    this((int)(mix.getAndDecrement() ^ System.nanoTime()));
  }
 
  /**
   * Simple 32-bit LCG.  Roughly the top 14-16 bits are
   * reasonable.
   */

  private final int rng()
  {
    int r = seed;
     
    r    *= RNG_M;
    r    += RNG_A;
    seed = r;
   
    return seed;
  }
 
  /**
   * almost uniform random on [0,0xFFFF]
   */

  private final int rng16()
  {
    return rng() >>> 16;
  }
 
  /**
   * almost uniform random on [-,]
   */

  @SuppressWarnings("unused")
  private final int rng16s()
  {
    return rng() >> 16;
  }
 
  // 32-bit random number
 private final int rng32()
  {
    int r;
   
    r  = (rng() >>> 16);
    r |= (rng() & 0xFFFF0000);
   
    return r;
  }
 
 
  /**
   * Generates a (nearly) uniform random number on the
   * range [0, range).
   * <p>
   */

  private final int rng(int range)
  {
    return ((rng()>>>15) * range) >>> 17;
  }
 
  /**
   *
   */

  public final void setSeed(int seed)
  {
    this.seed = seed;
  }
 
  public final int getSeed()
  {
    return seed;
  }
 
 
  /**
   * Simulates flipping 'n' coins.
   * <p>
   * The legal range of 'n' is [1, 16]
   */

  public final int flip16(int n)
  {
    return Integer.bitCount(rng() >>> (32-n));
  }
 
  /**
   * Simulates flipping 'n' coins.
   * <p>
   * The legal range of 'n' is [1, 32].
   */

  public final int flip32(int n)
  {
    return Integer.bitCount(rng32() >>> (32-n));
  }
 
  /**
   * Simulates flipping 16 coins.
   * <p>
   * Returns a random number on the range [0, 16].
   * <p>
   * @see #flip16(int)
   * @see #flip32()
   */

  public final int flip16()
  {
    return Integer.bitCount(rng16());
  }
 
  /**
   * Simulates flipping 32 coins.
   * <p>
   * Returns a random number on the range [0, 32]
   */

 
  public final int flip32()
  {
    return Integer.bitCount(rng32());
  }
 
  /** Flips a coin. Result is 1 or 2. */
  public final int d2()
  {
    return 1 + (rng() >>> 31);
  }
 
  /** Simulates the sum of <i>n</i> coin tosses. */
  public final int d2(int n)
  {
    int r = n;

    while (n >= 32) {
      r += flip32();
      n -= 32;
    }
   
    if (n >= 16) {
      r += flip16();
      n -= 16;
    }
   
    if (n != 0)
      r += flip16(n);
   
    return r;
  }
 
  /** Simulates a roll of a four sided die. */
  public final int d4()
  {
    return  1 + (rng() >>> 30);
  }
 
  /** Simulates the sum of <i>n</i> four sided dice rolls.*/
  public final int d4(int n)
  {
    int r = n;

    for(int i=0; i<n; i++)
      r += (rng() >>> 30);

    return r;
  }
 
  /** Simulates a roll of a six sided die. */
  public final int d6()
  {
    // 6 = 3*2 = (2+1)*2
   int r = rng16();
   
    r = ((r<<1)+r) >>> 15;
   
    return r+1;
  }
 
  /** Simulates the sum of <i>n</i> six sided dice rolls.*/
  public final int d6(int n)
  {
    int r = 0;

    for(int i=0; i<n; i++)
      r += d6();
   
    return r;
  }
 
  /** Simulates a roll of an eight sided die. */
  public final int d8()
  {
    return 1 + (rng() >>> 29);
  }
 
  /** Simulates the sum of <i>n</i> eight sided dice rolls.*/
  public final int d8(int n)
  {
    int r = n;

    for(int i=0; i<n; i++)
      r += (rng() >>> 29);
   
    return r;
  }
 
  /** Simulates a roll of a ten sided die. */
  public final int d10()
  {
    // 10 = 5*2 = (4+1)*2
   int r = rng16();
   
    r = ((r<<2)+r) >>> 15;
   
    return r+1;
  }
 
  /** Simulates the sum of <i>n</i> ten sided dice rolls.*/
  public final int d10(int n)
  {
    int r = 0;

    for(int i=0; i<n; i++)
      r += d10();
   
    return r;
  }
 
  /** Simulates a roll of a twelve sided die. */
  public final int d12()
  {
    // 12 = 3*4 = (2+1)*4
   int r = rng16();
   
    r = ((r<<1)+r) >>> 14;
   
    return r+1;
  }
 
  /** Simulates the sum of <i>n</i> twelve sided dice rolls.*/
  public final int d12(int n)
  {
    int r = 0;

    for(int i=0; i<n; i++)
      r += d12();
   
    return r;
  }
 
  /** Simulates a roll of a twenty sided die. */
  public final int d20()
  {
    // 20 = 5*4 = (4+1)*4
   int r = rng16();
   
    r = ((r<<2)+r) >>> 14;
   
    return r+1;
  }
 
  /** Simulates the sum of <i>n</i> twenty sided dice rolls.*/
  public final int d20(int n)
  {
    int r = 0;

    for(int i=0; i<n; i++)
      r += d20();
   
    return r;
  }
 
  /** Simulates a roll of a one-hundred sided die. */
  public final int d100()
  {
    // 100 = 25*4 = (3*8+1)*4
   int r = rng() >>> 14;
    int t = (r << 1)+r;
   
    r = ((t<<3)+r) >>> 16;
   
    return r+1;
  }
 
  /** Simulates the sum of <i>n</i> one-hundred sided dice rolls.*/
  public final int d100(int n)
  {
    int r = 0;

    for(int i=0; i<n; i++)
      r += d100();
   
    return r;
  }
 
  /**
   * Returns a random number on [0, 0xFFFF] with a
   * triangular distribution.
   */

  public final int triangle()
  {
    int r0 = rng16();
    int r1 = rng16();
   
    return (r0+r1+1)>>1;
  }
 
  /**
   * Returns a random number on [0, range) with a
   * triangular distribution.
   */

  public final int triangle(int range)
  {
    return (triangle() * range) >>> 16;
  }
 
  /**
   * Returns a random number with a triangle distribution.
   * <p>
   * [0, a+b)
   * <p>
   * a 'steps' of linear increase/decreasing on the edges
   */

  public final int triangle(int a, int b)
  {
    // complete comments
   // isn't it really a+b-1
   a = rng(a);
    b = rng(b);
   
    return a+b;
  }
}
Offline Rorkien
« Reply #1 - Posted 2012-07-28 17:27:56 »



Ehm, i guess i find the usual Random class good enough  persecutioncomplex
You running a lotto house?
Offline Roquen
« Reply #2 - Posted 2012-07-28 18:52:48 »

This nerd shit is that most people get it horrible wrong so they change to using something like the Mersenne twister and their results are just as wrong but they're to ignorant to know better.  Questions?  BTW: All of computer science is nerd shit, so maybe you should take up something like "shin kicking" or something as a hobby.

Seriously this is about as fast as you're going to get in Java and still statistically correct.  Fast for small number of dice per roll (say 10ish).  It gets a little more complicated for rolling many dice assuming you still want to be statistically correct.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2012-07-28 18:59:54 »

Perhaps you could explain a bit what the usages are and also the caracteristics this functions. Perhaps also compare them to the standard Math.random().

I heard a bit about pseudo random number generation 1-2 years back in university, but don't remeber that much. And becauset here are a lot beginners, a bit of inside would be helpful.
Offline Rorkien
« Reply #4 - Posted 2012-07-28 19:55:41 »

This nerd shit is that most people get it horrible wrong so they change to using something like the Mersenne twister and their results are just as wrong but they're to ignorant to know better.  Questions?  BTW: All of computer science is nerd shit, so maybe you should take up something like "shin kicking" or something as a hobby.

Seriously this is about as fast as you're going to get in Java and still statistically correct.  Fast for small number of dice per roll (say 10ish).  It gets a little more complicated for rolling many dice assuming you still want to be statistically correct.

Chill, i didn't say that in a mean way
It was just a thing that occurred in my mind when i saw the code, since i am really not into Probability and Statistics  Roll Eyes
Offline Roquen
« Reply #5 - Posted 2012-07-28 19:57:37 »

@Danny02: There's already a thread that covers some basics in performance tuning.  Basically the core generator here and in Random the same family and perfectly fine for most AI usages, dice rolling, etc.  What people screw up for dice rolling is that the distribution changes based on the number of dice rolled.  Uniform for 1 die (equal probability).  Triangular for 2 and each additional moves it closer and closer to gaussian.

@Rorkien: The first part of my reply was a "your mother is sooo stupid" kinda response...the "shin kicking" and seriously part were intended to give that away.
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!