예제 #1
0
 public MersenneTwister(MersenneTwisterData data)
     : base()
 {
     SetData(data);
 }
예제 #2
0
 /// <exception cref="Kirikiri.Tjs2.TJSException"></exception>
 /// <exception cref="Kirikiri.Tjs2.VariantException"></exception>
 public virtual void Randomize(Variant[] param)
 {
     if (param.Length == 0)
     {
         // parametor not given
         if (mRandomBits128 != null)
         {
             // another random generator is given
             //tjs_uint8 buf[32];
             //unsigned long tmp[32];
             ByteBuffer buf = ByteBuffer.AllocateDirect(32);
             mRandomBits128.GetRandomBits128(buf, 0);
             mRandomBits128.GetRandomBits128(buf, 16);
             int[] tmp = new int[32];
             for (int i = 0; i < 32; i++)
             {
                 long num = (long)buf.Get(i) + ((long)buf.Get(i) << 8) + ((long)buf.Get(1) << 16)
                     + ((long)buf.Get(i) << 24);
                 tmp[i] = (int)(num > int.MaxValue ? num - unchecked((long)(0x100000000L)) : num);
             }
             if (mGenerator != null)
             {
                 mGenerator = null;
             }
             mGenerator = new MersenneTwister(tmp);
         }
         else
         {
             if (mGenerator != null)
             {
                 mGenerator = null;
             }
             mGenerator = new MersenneTwister(Runtime.CurrentTimeMillis());
         }
     }
     else
     {
         if (param.Length >= 1)
         {
             if (param[0].IsObject())
             {
                 MersenneTwisterData data = null;
                 try
                 {
                     // may be a reconstructible information
                     VariantClosure clo = param[0].AsObjectClosure();
                     if (clo.mObject == null)
                     {
                         throw new TJSException(Error.NullAccess);
                     }
                     string state;
                     Variant val = new Variant();
                     data = new MersenneTwisterData();
                     // get state array
                     //TJSThrowFrom_tjs_error
                     int hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "state", val, null);
                     if (hr < 0)
                     {
                         Error.ThrowFrom_tjs_error(hr, null);
                     }
                     state = val.AsString();
                     if (state.Length != MT_N * 8)
                     {
                         throw new TJSException(Error.NotReconstructiveRandomizeData);
                     }
                     int p = 0;
                     for (int i = 0; i < MT_N; i++)
                     {
                         long n = 0;
                         int tmp;
                         for (int j = 0; j < 8; j++)
                         {
                             int c = state[p + j];
                             tmp = -1;
                             if (c >= '0' && c <= '9')
                             {
                                 n = c - '0';
                             }
                             else
                             {
                                 if (c >= 'a' && c <= 'f')
                                 {
                                     n = c - 'a' + 10;
                                 }
                                 else
                                 {
                                     if (c >= 'A' && c <= 'F')
                                     {
                                         n = c - 'A' + 10;
                                     }
                                 }
                             }
                             if (tmp == -1)
                             {
                                 throw new TJSException(Error.NotReconstructiveRandomizeData);
                             }
                             else
                             {
                                 n <<= 4;
                                 n += tmp;
                             }
                         }
                         p += 8;
                         data.state.Put(i, n & unchecked((long)(0xffffffffL)));
                     }
                     // get other members
                     hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "left", val, null);
                     if (hr < 0)
                     {
                         Error.ThrowFrom_tjs_error(hr, null);
                     }
                     data.left = val.AsInteger();
                     hr = clo.PropGet(Interface.MEMBERMUSTEXIST, "next", val, null);
                     data.next = val.AsInteger();
                     if (mGenerator != null)
                     {
                         mGenerator = null;
                     }
                     mGenerator = new MersenneTwister(data);
                 }
                 catch (VariantException)
                 {
                     data = null;
                     throw new TJSException(Error.NotReconstructiveRandomizeData);
                 }
                 catch (TJSException)
                 {
                     data = null;
                     throw new TJSException(Error.NotReconstructiveRandomizeData);
                 }
                 data = null;
             }
             else
             {
                 // 64bitじゃなくて、32bit にしてしまっている。实用上问题あれば修正。
                 int n = param[0].AsInteger();
                 int[] tmp = new int[1];
                 tmp[0] = n;
                 if (mGenerator != null)
                 {
                     mGenerator = null;
                 }
                 mGenerator = new MersenneTwister(tmp);
             }
         }
     }
 }
예제 #3
0
 public virtual void SetData(MersenneTwisterData rhs)
 {
     // copy
     ByteBuffer src = rhs.state.Duplicate();
     src.Position(0);
     src.Limit(MT_N);
     ByteBuffer buff = ByteBuffer.AllocateDirect(MT_N * 8);
     buff.Order(ByteBuffer.NativeOrder());
     state = buff.AsLongBuffer();
     state.Clear();
     state.Put(src);
     next = rhs.next;
     left = rhs.left;
 }