Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  Sobol sequence 2D/3D  (Read 2987 times)
0 Members and 1 Guest are viewing this topic.
Offline Roquen
« Posted 2014-01-07 14:44:29 »

Mostly working Sobol sequences in two and three dimensions.  The non-working part is that they don't wrap properly...so you can "only" create 232 values.

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  
public final class Sobol2D
{
  // state data
  private int i,d0,d1;

  // direction table
  private static final int[] D;
 
  static {
    D = new int[33];
   
    int c = D[0] = 1 << 31;
   
     for(int i=1; i<32; i++) {
       c = c ^ (c >>> 1);
       D[i] = c;
     }
     
     // TODO: what about -1? index 32
  }
 
  private final void updateState()
  {
    int c = Integer.numberOfTrailingZeros(~i);
    d1 ^= D[c];
    d0 ^= 0x80000000 >>> c;
    i  += 1;    
  }
 
  /** Return current index into the stream. */
  public final int getPos() { return i; }
 
  /** */
  public final void seek(int num)
  {
    int n = i + num;
    int a = i ^ (i >>> 1);
    int b = n ^ (n >>> 1);
    int d = a ^ b;
   
    int c = 0;
   
    while(d != 0) {
      if ((d & 1) != 0) {
        d0 ^= 0x80000000 >>> c;
        d1 ^= D[c];
      }
      d >>>= 1;
      c   += 1;
    }
   
    i = n;    
  }
 
 
  public Sobol2D() { seed(0,0); }
 
  public Sobol2D(int seedX, int seedY)
  {
    seed(seedX, seedY);
  }
 
  public final void seed(int s0, int s1)
  {
    d0 = s0;
    d1 = s1;
    i  = 0;
  }
 
  public final void next(float[] v, int off)
  {
    v[off++] = (d0 >>> 8) * 0x1p-24f;
    v[off++] = (d1 >>> 8) * 0x1p-24f;
    updateState();
  }
 
  public final void next(float[] v) { next(v, 0); }
 
  public final void next(java.nio.FloatBuffer fb)
  {
    fb.put((d0 >>> 8) * 0x1p-24f);
    fb.put((d1 >>> 8) * 0x1p-24f);
    updateState();
  }
}

public final class Sobol3D
{
  // state data for the sequence
  private int i,d0,d1,d2;

  // direction table
  private static final int[] D;
 
  // build the direction tables
  static {
    // TODO: 'i' = -1 is not handled
   
    D = new int[65];

    // second dimension
    int c = 1 << 31;
   
    D[0] = c;
   
     for(int i=2; i<64; i+=2) {
       c = c ^ (c >>> 1);
       D[i] = c;
     }
     
     // third dimension
     int s0,s1,t;
     
     s0 = D[1] = D[0];
     s1 = D[3] = D[2];
     
     for (int i=4; i<64; i+=2) {
       t   = s1;
       s1 ^= s0 ^ (s0 >>> 2);
       s0  = t;
       
       D[i+1] = s1;
     }
  }
 
  /** */
  public Sobol3D() { d0=d1=d2=i=0; }
 
  /** */
  public final void seed(int s0, int s1, int s2)
  {
    d0 = s0;
    d1 = s1;
    d2 = s2;
    i  = 0;
  }

  /** Return current index into the stream. */
  public final int getPos() { return i; }
 
  /** */
  private final void updateState()
  {
    int c = Integer.numberOfTrailingZeros(~i);
    int o = c+c;
   
    d0 ^= 0x80000000 >>> c;
    d1 ^= D[o];
    d2 ^= D[o+1];
    i  += 1;
  }
 
  /** */
  public final void seek(int num)
  {
    int n = i + num;
    int a = i ^ (i >>> 1);
    int b = n ^ (n >>> 1);
    int d = a ^ b;
   
    int c = 0;
   
    while(d != 0) {
      if ((d & 1) != 0) {
        d0 ^= 0x80000000 >>> c;
        d1 ^= D[c+c];
        d2 ^= D[c+c+1];
      }
     
      d >>>= 1;
      c   += 1;
    }
   
    i = n;    
  }

 
  /** */
  public final void next(float[] v, int off)
  {
    v[off++] = (d0 >>> 8) * 0x1p-24f;
    v[off++] = (d1 >>> 8) * 0x1p-24f;
    v[off++] = (d2 >>> 8) * 0x1p-24f;
   
    updateState();
  }
 
  /** */
  public final void next(float[] v) { next(v,0); }
 
  public final void next(java.nio.FloatBuffer fb)
  {
    fb.put((d0 >>> 8) * 0x1p-24f);
    fb.put((d1 >>> 8) * 0x1p-24f);
    fb.put((d2 >>> 8) * 0x1p-24f);
    updateState();
  }
}
Offline Roquen
« Reply #1 - Posted 2014-01-27 09:50:55 »

Updated version now live here: https://github.com/roquendm/JGO-Grabbag/tree/master/src/roquen/math/lds
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.

trollwarrior1 (27 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
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!