class expgen
{
var arr : ulong[];//this will be 624 long
var pos : int=0;
var mid : int=397;
var sed : ulong=423324;
public function expgen()
{
arr=new ulong[624];
mid=397;
sed=423324;
seed(sed);
}
private function generate()
{
for(var scan=0;scan<pos(pos-mid);++scan)
arr[scn]=arr[scan+mid] ^ mix(arr[scan],arr[scan]+1]);
for(scan=pos-mid;scan<(pos-1);++scan)
arr[scan]=arr[scan+mid-pos] ^ mix(arr[scn],arr[scan+1);
arr[pos-1]=arr[mid-1] ^ mix(arr[pos-1],arr[0]);
pos=0;
}
public function seed(se : ulong)
{
sed=se;
arr[0]=se&0xffffffff;
for(var scan=1;scan<arr.Length;scan++)
{
arr[scan]=1812433253* (arr[scan-1 ^ (arr[scan-1] >> 30)) + scan;
arr[scn] &= 0xffffffff;
pos=arr.Length;
generate();
}
private function mix(x : ulong,y : ulong)
{
return (((x&0x80000000) | (y&0x7fffffff))>>1) ^ ((y&1)*0x9908B0DF);
}
public function getrandom()
{
var x : ulong;
x=arr[pos];
x^= (x>>1);
x^=(x<<7) & 0x9D2C5680;
x^=(x<<15)&0xEFC60000;
x=x^(x>>18);
pos++;
if(pos>=arr.Length-1)
{
generate();
}
return r;
}
public function getrange(l : ulong,h : ulong)
{
var r : ulong= h-l;
var gr : ulong=getrandom();
r= gr%(r+1);
r+=l;
return r;
}
}
};
There could have been some typo’s in there as I had to type it in, not copy and paste.
The generator code I got had a license agreement with it, it is posed below.
// mtrand.h
// C++ include file for MT19937, with initialization improved 2002/1/26.
// Coded by Takuji Nishimura and Makoto Matsumoto.
// Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/ ).
// The generators returning floating point numbers are based on
// a version by Isaku Wada, 2002/01/09
// …
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
// All rights reserved.
// …
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// …
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// …
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// …
// 3. The names of its contributors may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.
// …
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// …
// Any feedback is very welcome.
// http://www.math.keio.ac.jp/matumoto/emt.html
// email: matumoto@math.keio.ac.jp
// …
// Feedback about the C++ port should be sent to Jasper Bedaux,
// see http://www.bedaux.net/mtrand/ for e-mail address and info.
Cool, is this using MT19937?
lordofduct:
this post is 5 years old, not sure if OP will still be active enough to come and respond
the license from the C++ code that OP used to convert to unityscript says it’s MT19937
This is in unityscript which is no longer actively supported. So you’d end up having to port it again… or you could just go find a C# implementation that already exists. Like here:
https://www.codeproject.com/Articles/5147/A-C-Mersenne-Twister-class
Thanks.
Yeah, I was also looking into this one right now.